make working on websites nicer

(since every time we have to change anything on these I get annoyed at
having to remember how to build these. Now you can just use `nix run`!)
This commit is contained in:
stuebinm 2023-02-24 17:33:48 +01:00
parent 7fd1c9ff80
commit a6d21f4fd9
8 changed files with 78 additions and 32 deletions

View file

@ -36,6 +36,23 @@ nix build .#nixosConfigurations.parsons.config.system.build.toplevel
(but you might have trouble deploying it) (but you might have trouble deploying it)
## Working on websites
Websites are exposed as flake outputs: if you're working on a website & want to
check it in a browser, do e.g.
~~~shell
nix run .#\"muc.hacc.earth\"
~~~
to start a local http server (note that some of our websites need a directory
to be built in; these use `/tmp/hacc-website`).
To add a new website, add a new subdirectory to `websites`; nix will generate a
vhost config based on that directory's name. Add a `default.nix` in your directory
describing how to build the website, and give its derivation a `watch` attribute
to make the `nix run` setup work.
## I don't want to build this long dependency / want a cached version! ## I don't want to build this long dependency / want a cached version!
If it's still available on parsons from a previous deploy, do: If it's still available on parsons from a previous deploy, do:

View file

@ -85,6 +85,9 @@
checks = builtins.mapAttrs checks = builtins.mapAttrs
(system: deployLib: deployLib.deployChecks self.deploy) (system: deployLib: deployLib.deployChecks self.deploy)
deploy-rs.lib; deploy-rs.lib;
packages.x86_64-linux =
self.nixosConfigurations.parsons.config.hacc.websites.builders;
}; };
} }

View file

@ -21,33 +21,42 @@ in
default = []; default = [];
description = "subdirectories that shouldn't be published"; description = "subdirectories that shouldn't be published";
}; };
builders = mkOption {
type = types.lazyAttrsOf types.package;
default = {};
description = "exposes website builders, for use with nix run";
};
}; };
config = mkIf cfg.enable { config = let
services.nginx = {
enable = true;
virtualHosts =
let
subdirs = subdirs =
let dirAttrs = filterAttrs let dirAttrs = filterAttrs
(n: v: v == "directory" || lists.elem n cfg.ignore) (n: v: v == "directory" || lists.elem n cfg.ignore)
(builtins.readDir cfg.directory); (builtins.readDir cfg.directory);
in mapAttrsToList (n: v: n) dirAttrs; in mapAttrsToList (n: v: n) dirAttrs;
mkWebsiteDrv = subdir:
mkWebsite = subdir: { pkgs.callPackage "${cfg.directory}/${subdir}" {};
mkWebsiteVHost = subdir: {
name = subdir; name = subdir;
# the nginx virtualhost config (for all sites) goes in here # the nginx virtualhost config (for all sites) goes in here
value = { value = {
enableACME = true; enableACME = true;
forceSSL = true; forceSSL = true;
# naive string interpolation is safe here since nix will always immediately
# resolve relative paths to absolute paths; it's not lazy about that.
locations."/".root = locations."/".root =
(pkgs.callPackage "${cfg.directory}/${subdir}" {}).outPath; (mkWebsiteDrv subdir).outPath;
}; };
}; };
in listToAttrs (map mkWebsite subdirs); in mkIf cfg.enable {
services.nginx = {
enable = true;
virtualHosts =
listToAttrs (map mkWebsiteVHost subdirs);
}; };
hacc.websites.builders =
listToAttrs (map (subdir: {
name = subdir;
value = if (mkWebsiteDrv subdir) ? watch then (mkWebsiteDrv subdir).watch else null;
}) subdirs);
}; };
} }

View file

@ -1,6 +1,6 @@
{ stdenvNoCC }: { stdenvNoCC, sfz, writeScriptBin }:
stdenvNoCC.mkDerivation { stdenvNoCC.mkDerivation rec {
name = "hacc.earth-static"; name = "hacc.earth-static";
src = ./.; src = ./.;
@ -13,4 +13,7 @@ stdenvNoCC.mkDerivation {
rm $out/default.nix rm $out/default.nix
''; '';
watch = writeScriptBin "watch" ''
${sfz}/bin/sfz -r ${src} "$@"
'';
} }

View file

@ -1,6 +1,6 @@
{ stdenvNoCC }: { stdenvNoCC, sfz, writeScriptBin }:
stdenvNoCC.mkDerivation { stdenvNoCC.mkDerivation rec {
name = "muc.hacc.earth-static"; name = "muc.hacc.earth-static";
src = ./.; src = ./.;
@ -13,4 +13,7 @@ stdenvNoCC.mkDerivation {
rm $out/default.nix rm $out/default.nix
''; '';
watch = writeScriptBin "watch" ''
${sfz}/bin/sfz -r ${src}
'';
} }

View file

@ -1,6 +1,6 @@
{ jekyll, stdenvNoCC }: { jekyll, stdenvNoCC, writeScriptBin }:
stdenvNoCC.mkDerivation { stdenvNoCC.mkDerivation rec {
name = "infra4future.de-static"; name = "infra4future.de-static";
src = ./.; src = ./.;
@ -11,4 +11,8 @@ stdenvNoCC.mkDerivation {
${jekyll}/bin/jekyll build -d $out --disable-disk-cache ${jekyll}/bin/jekyll build -d $out --disable-disk-cache
''; '';
watch = writeScriptBin "watch" ''
rm -rf /tmp/hacc-website
${jekyll}/bin/jekyll serve -s ${src} --disable-disk-cache -d /tmp/hacc-website
'';
} }

View file

@ -1,6 +1,6 @@
{ stdenvNoCC }: { stdenvNoCC, sfz, writeScriptBin }:
stdenvNoCC.mkDerivation { stdenvNoCC.mkDerivation rec {
name = "muc.hacc.earth-static"; name = "muc.hacc.earth-static";
src = ./.; src = ./.;
@ -13,4 +13,7 @@ stdenvNoCC.mkDerivation {
rm $out/default.nix rm $out/default.nix
''; '';
watch = writeScriptBin "watch" ''
${sfz}/bin/sfz -r ${src}
'';
} }

View file

@ -1,6 +1,6 @@
{ jekyll, stdenvNoCC }: { jekyll, stdenvNoCC, writeScriptBin }:
stdenvNoCC.mkDerivation { stdenvNoCC.mkDerivation rec {
name = "mumble.infra4future.de-static"; name = "mumble.infra4future.de-static";
src = ./.; src = ./.;
@ -11,4 +11,8 @@ stdenvNoCC.mkDerivation {
${jekyll}/bin/jekyll build -d $out --disable-disk-cache ${jekyll}/bin/jekyll build -d $out --disable-disk-cache
''; '';
watch = writeScriptBin "watch" ''
rm -rf /tmp/hacc-website
${jekyll}/bin/jekyll serve -s ${src} --disable-disk-cache -d /tmp/hacc-website
'';
} }