From 70b351ef75a9a2299c5c3f9a8a3684a89ff10d0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yannik=20R=C3=B6del?= Date: Tue, 28 Sep 2021 13:06:38 +0200 Subject: [PATCH] Add production build --- .gitignore | 2 ++ README.md | 13 ++++++-- flake.nix | 94 ++++++++++++++++++++++++++++++++---------------------- 3 files changed, 69 insertions(+), 40 deletions(-) diff --git a/.gitignore b/.gitignore index f3a63aa..9d92510 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,5 @@ node_modules/ # Environment .env +# `nix build` output +/result diff --git a/README.md b/README.md index 095f4bc..b1dfb09 100644 --- a/README.md +++ b/README.md @@ -26,5 +26,14 @@ for changes. This repository also contains a [Nix flake](https://nixos.wiki/wiki/Flakes) which contains the full development environment. `nix develop` will open a shell with all required tools – you don't need to run `npm install` anymore. -When node dependencies get updated (which changes `package.json`), make sure to -run `./_nix/update.sh` to update the npm lockfile and the Nix environment. + +When Node dependencies are updated (this will change `package.json`), make sure +to run `./_nix/update.sh` to update the npm lockfile and the Nix environment. + +The flake also contains a second package for building the production output: + +```shell +nix build .#angestoepselt-site +``` + +The final derivation contains a `www` folder that can be served. diff --git a/flake.nix b/flake.nix index 22c2bbc..b8e9c5b 100644 --- a/flake.nix +++ b/flake.nix @@ -17,47 +17,65 @@ dontNpmInstall = true; }; in { - # This package isn't actually the fully-built site, but rather a derivation - # that contains the relevant programs (with correctly set up environment) - # to develop and build the site. It can either be used with `nix develop` – - # see the repository's readme for details – or compiled with `nix build`. - # The latter will output a folder which contains node and npm binaries that - # can be used in an IDE. - packages.${system}.angestoepselt-site-dev = pkgs.runCommand "angestoepselt-site-dev" { - buildInputs = [ nodejs pkgs.makeWrapper ]; + packages.${system} = { + angestoepselt-site = pkgs.stdenv.mkDerivation { + name = "angestoepselt-site"; + src = self; - shellHook = '' - export NODE_PATH=${nodeDependencies}/lib/node_modules - export PATH="${nodeDependencies}/bin:$PATH" + buildInputs = [ nodejs nodeDependencies ]; - echo "" - echo " To start editing content, run:" - echo "" - echo "npm run build:styles" - echo "npm run dev:site" - echo "" - echo " The site will be available under http://localhost:8080/ for" - echo " local development and rebuilds automatically when content" - echo " changes." - echo "" + buildPhase = '' + npm run build + ''; + + installPhase = '' + mkdir -p "$out" + cp -r _site "$out/www" + ''; + }; + + # This package isn't actually the fully-built site, but rather a + # derivation that contains the relevant programs (with correctly set up + # environment) to develop and build the site. It can either be used with + # `nix develop` – see the repository's readme for details – or compiled + # with `nix build`. The latter will output a folder which contains node + # and npm binaries that can be used in an IDE. + angestoepselt-site-dev = pkgs.runCommand "angestoepselt-site-dev" { + buildInputs = [ nodejs pkgs.makeWrapper ]; + + shellHook = '' + export NODE_PATH=${nodeDependencies}/lib/node_modules + export PATH="${nodeDependencies}/bin:$PATH" + + echo "" + echo " To start editing content, run:" + echo "" + echo "npm run build:styles" + echo "npm run dev:site" + echo "" + echo " The site will be available under http://localhost:8080/ for" + echo " local development and rebuilds automatically when content" + echo " changes." + echo "" + ''; + } '' + mkdir -p "$out" + + wrap() { + makeWrapper "${nodejs}/bin/$1" "$out/bin/$1" \ + --prefix PATH : "${nodejs}/bin" \ + --prefix PATH : "${nodeDependencies}/bin" \ + --set-default NODE_PATH "${nodeDependencies}/lib/node_modules" + } + + wrap node + wrap npm + wrap npx + + unset -f wrap ''; - } '' - mkdir -p "$out" + }; - wrap() { - makeWrapper "${nodejs}/bin/$1" "$out/bin/$1" \ - --prefix PATH : "${nodejs}/bin" \ - --prefix PATH : "${nodeDependencies}/bin" \ - --set-default NODE_PATH "${nodeDependencies}/lib/node_modules" - } - - wrap node - wrap npm - wrap npx - - unset -f wrap - ''; - - defaultPackage.${system} = self.packages.${system}.angestoepselt-site; + defaultPackage.${system} = self.packages.${system}.angestoepselt-site-dev; }; }