Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support building via nix #4821

Merged
merged 11 commits into from
Feb 2, 2024
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ node_modules

## Build generated
build/
dist/
DerivedData
editor/build
editor/node_modules
Expand Down
1 change: 1 addition & 0 deletions release.nix
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ let
# Hash obtained using `nix-prefetch-url --unpack <url>`
sha256 = "11w3wn2yjhaa5pv20gbfbirvjq6i3m7pqrq2msf0g7cv44vijwgw";
}) { inherit config; };

in
{
pkgs = pkgs;
Expand Down
1 change: 1 addition & 0 deletions server/package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ dependencies:
- filepath
- free
- generic-lens
- generic-lens-core
- github ==0.27
- hashable
- hoauth2
Expand Down
2 changes: 2 additions & 0 deletions server/utopia-web.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ executable utopia-web
, filepath
, free
, generic-lens
, generic-lens-core
, github ==0.27
, hashable
, hoauth2
Expand Down Expand Up @@ -244,6 +245,7 @@ test-suite utopia-web-test
, filepath
, free
, generic-lens
, generic-lens-core
, github ==0.27
, hashable
, hedgehog
Expand Down
126 changes: 63 additions & 63 deletions shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,23 @@ let
release = (import ./release.nix {});
pkgs = release.pkgs;
lib = pkgs.lib;
node = pkgs.nodejs-16_x;
node = pkgs.nodejs-18_x;
postgres = pkgs.postgresql_13;
stdenv = pkgs.stdenv;
pnpm = node.pkgs.pnpm;
yarn = pkgs.yarn;
yarn = pkgs.yarn.override { nodejs = node; };

nodePackages = [
node
pnpm
(yarn.override { nodejs = node; })
yarn
];

cabal = pkgs.haskellPackages.cabal-install;
# Slightly kludgy because the zlib Haskell package is a pain in the face.
ghc = pkgs.haskell.packages.${compiler}.ghcWithPackages (hpkgs: with hpkgs; [
zlib
magic
]);

baseEditorScripts = [
Expand Down Expand Up @@ -224,25 +225,63 @@ let
(pkgs.writeScriptBin "update-vscode-build-extension" ''
#!/usr/bin/env bash
set -e
build-utopia-vscode-extension
cd $(${pkgs.git}/bin/git rev-parse --show-toplevel)/vscode-build
yarn
yarn run pull-utopia-extension
nix-shell --run update-vscode-build-extension-inner
'')
(pkgs.writeScriptBin "build-vscode" ''
#!/usr/bin/env bash
set -e
cd $(${pkgs.git}/bin/git rev-parse --show-toplevel)/vscode-build
rm -rf ./dist ./node_modules
yarn
yarn run build
nix-shell --run build-vscode-inner
'')
(pkgs.writeScriptBin "build-vscode-with-extension" ''
#!/usr/bin/env bash
set -e
build-utopia-vscode-extension
build-vscode
'')
(pkgs.writeScriptBin "check-tool-versions" ''
#! /usr/bin/env nix-shell
#! nix-shell -p "haskellPackages.ghcWithPackages (pkgs: with pkgs; [async process])" -i runhaskell

import Control.Concurrent.Async
import Control.Monad
import Data.Foldable
import Data.List
import Data.Monoid
import System.Exit
import System.Process

expectedToolVersions :: [(String, [String], [String])]
expectedToolVersions =
[ ("node", ["--version"], ["v18.12.1"])
, ("pnpm", ["--version"], ["7.14.2"])
, ("yarn", ["--version"], ["1.22.19"])
, ("ghc", ["--version"], ["The Glorious Glasgow Haskell Compilation System, version 9.0.2"])
, ("cabal", ["--version"], ["cabal-install version 3.8.1.0", "compiled using version 3.8.1.0 of the Cabal library "])
]

checkVersion :: (String, [String], [String]) -> IO All
checkVersion (executable, arguments, expectedOutput) = do
let commandToRun = unwords (executable : arguments)
output <- readProcess "nix-shell" ["--run", commandToRun] ""
let actualOutput = lines output
let correctVersion = actualOutput == expectedOutput
unless correctVersion $ do
putStrLn ("Error when checking the version of " <> executable)
putStrLn "Expected:"
traverse_ putStrLn expectedOutput
putStrLn "Received:"
traverse_ putStrLn actualOutput
pure $ All correctVersion

main :: IO ()
main = do
results <- mapConcurrently checkVersion expectedToolVersions
let result = getAll $ mconcat results
when result $ putStrLn "All tools are the correct version."
if result then exitSuccess else exitFailure
'')
];

withBaseEditorScripts = lib.optionals includeEditorBuildSupport baseEditorScripts;
Expand Down Expand Up @@ -465,8 +504,7 @@ let
#!/usr/bin/env bash
set -e
cd $(${pkgs.git}/bin/git rev-parse --show-toplevel)/vscode-build
${yarn}/bin/yarn
${yarn}/bin/yarn run make-patch
nix-shell --run update-vscode-patch-inner
'')
(pkgs.writeScriptBin "watch-utopia-vscode-common" ''
#!/usr/bin/env bash
Expand All @@ -486,7 +524,7 @@ let
#!/usr/bin/env bash
set -e
cd $(${pkgs.git}/bin/git rev-parse --show-toplevel)/vscode-build
${yarn}/bin/yarn run pull-utopia-extension
nix-shell --run pull-extension-inner
'')
(pkgs.writeScriptBin "watch-vscode-build-extension-only" ''
#!/usr/bin/env bash
Expand All @@ -498,47 +536,6 @@ let

# For the useful scripts in our dev environments
customDevScripts = [
(pkgs.writeScriptBin "check-tool-versions" ''
#! /usr/bin/env nix-shell
#! nix-shell -p "haskellPackages.ghcWithPackages (pkgs: with pkgs; [async process])" -i runhaskell

import Control.Concurrent.Async
import Control.Monad
import Data.Foldable
import Data.List
import Data.Monoid
import System.Exit
import System.Process

expectedToolVersions :: [(String, [String], [String])]
expectedToolVersions =
[ ("pnpm", ["--version"], ["7.14.2"])
, ("yarn", ["--version"], ["1.22.19"])
, ("ghc", ["--version"], ["The Glorious Glasgow Haskell Compilation System, version 9.0.2"])
, ("cabal", ["--version"], ["cabal-install version 3.8.1.0", "compiled using version 3.8.1.0 of the Cabal library "])
]

checkVersion :: (String, [String], [String]) -> IO All
checkVersion (executable, arguments, expectedOutput) = do
let commandToRun = unwords (executable : arguments)
output <- readProcess "nix-shell" ["--run", commandToRun] ""
let actualOutput = lines output
let correctVersion = actualOutput == expectedOutput
unless correctVersion $ do
putStrLn ("Error when checking the version of " <> executable)
putStrLn "Expected:"
traverse_ putStrLn expectedOutput
putStrLn "Received:"
traverse_ putStrLn actualOutput
pure $ All correctVersion

main :: IO ()
main = do
results <- mapConcurrently checkVersion expectedToolVersions
let result = getAll $ mconcat results
when result $ putStrLn "All tools are the correct version."
if result then exitSuccess else exitFailure
'')
(pkgs.writeScriptBin "stop-dev" ''
#!/usr/bin/env bash
# Kill nodemon because it just seems to keep running.
Expand Down Expand Up @@ -629,15 +626,21 @@ let

withCustomDevScripts = withServerRunScripts ++ (lib.optionals includeRunLocallySupport customDevScripts);

# TODO Come back to these when trying to use nix to build a docker container - https://stackoverflow.com/questions/58421505/how-do-i-apply-a-nix-shell-config-in-a-docker-image
releaseScripts = [
(pkgs.writeScriptBin "build-editor" ''
(pkgs.writeScriptBin "build-editor-production" ''
#!/usr/bin/env bash
set -e
cd $(${pkgs.git}/bin/git rev-parse --show-toplevel)/editor
${pnpm}/bin/pnpm install --unsafe-perm
${pnpm}/bin/pnpm run production
'')
(pkgs.writeScriptBin "build-editor-staging" ''
#!/usr/bin/env bash
set -e
cd $(${pkgs.git}/bin/git rev-parse --show-toplevel)/editor
${pnpm}/bin/pnpm install --unsafe-perm
${pnpm}/bin/pnpm run staging
'')
# CRA for whatever reason will automatically fail on CI for any warnings, so we need to prefix with `CI=false`. Urgh. https://github.com/facebook/create-react-app/issues/3657
(pkgs.writeScriptBin "build-website" ''
#!/usr/bin/env bash
Expand All @@ -649,20 +652,17 @@ let
(pkgs.writeScriptBin "build-server" ''
#!/usr/bin/env bash
set -e
cabal-update
seanparsons marked this conversation as resolved.
Show resolved Hide resolved
rebuild-cabal
cd $(${pkgs.git}/bin/git rev-parse --show-toplevel)/server
${cabal}/bin/cabal new-build utopia-web
cp --verbose $(${pkgs.haskellPackages.cabal-plan}/bin/cabal-plan list-bin exe:utopia-web) .
'')
(pkgs.writeScriptBin "build-all" ''
#!/usr/bin/env bash
set -e
build-editor
build-website
build-server
'')
];

scripts = withCustomDevScripts; # ++ (if needsRelease then releaseScripts else []);
withReleaseScripts = withCustomDevScripts ++ (lib.optionals includeReleaseSupport releaseScripts);

scripts = withReleaseScripts;

linuxOnlyPackages = lib.optionals stdenv.isLinux [ pkgs.xvfb_run pkgs.xlibsWrapper pkgs.xorg.libxkbfile ];
macOSOnlyPackages = lib.optionals stdenv.isDarwin (with pkgs.darwin.apple_sdk.frameworks; [
Expand Down
60 changes: 60 additions & 0 deletions vscode-build/shell.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
let
release = (import ../release.nix {});
pkgs = release.pkgs;
node = pkgs.nodejs-16_x;
stdenv = pkgs.stdenv;
pnpm = node.pkgs.pnpm;
yarn = pkgs.yarn.override { nodejs = node; };

nodePackages = [
node
pnpm
yarn
];

scripts = [
(pkgs.writeScriptBin "update-vscode-patch-inner" ''
#!/usr/bin/env bash
set -e
cd $(${pkgs.git}/bin/git rev-parse --show-toplevel)/vscode-build
${yarn}/bin/yarn
${yarn}/bin/yarn run make-patch
'')
(pkgs.writeScriptBin "pull-extension-inner" ''
#!/usr/bin/env bash
set -e
cd $(${pkgs.git}/bin/git rev-parse --show-toplevel)/vscode-build
${yarn}/bin/yarn
${yarn}/bin/yarn run pull-utopia-extension
'')
(pkgs.writeScriptBin "update-vscode-build-extension-inner" ''
#!/usr/bin/env bash
set -e
build-utopia-vscode-extension
cd $(${pkgs.git}/bin/git rev-parse --show-toplevel)/vscode-build
${yarn}/bin/yarn
${yarn}/bin/yarn run pull-utopia-extension
'')
(pkgs.writeScriptBin "build-vscode-inner" ''
#!/usr/bin/env bash
set -e
cd $(${pkgs.git}/bin/git rev-parse --show-toplevel)/vscode-build
rm -rf ./dist ./node_modules
${yarn}/bin/yarn
${yarn}/bin/yarn run build
'')
];

in pkgs.mkShell {
buildInputs = [
(pkgs.stdenv.mkDerivation {
name = "scripts";
phases = "installPhase";
installPhase = ''
mkdir -p $out/bin
'' + (builtins.concatStringsSep "" (builtins.map (script: ''
for f in $(ls -d ${script}/bin/*); do ln -s $f $out/bin; done
'') scripts));
})
] ++ nodePackages;
}
Loading