diff --git a/README.md b/README.md index 81abc4c..790b280 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,23 @@ nix build .#nixosConfigurations.parsons.config.system.build.toplevel (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! If it's still available on parsons from a previous deploy, do: diff --git a/flake.nix b/flake.nix index 46ab7e2..86d01e9 100644 --- a/flake.nix +++ b/flake.nix @@ -85,6 +85,9 @@ checks = builtins.mapAttrs (system: deployLib: deployLib.deployChecks self.deploy) deploy-rs.lib; + + packages.x86_64-linux = + self.nixosConfigurations.parsons.config.hacc.websites.builders; }; } diff --git a/modules/websites.nix b/modules/websites.nix index 0227dcb..0b701cb 100644 --- a/modules/websites.nix +++ b/modules/websites.nix @@ -21,33 +21,42 @@ in default = []; 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 + subdirs = + let dirAttrs = filterAttrs + (n: v: v == "directory" || lists.elem n cfg.ignore) + (builtins.readDir cfg.directory); + in mapAttrsToList (n: v: n) dirAttrs; + mkWebsiteDrv = subdir: + pkgs.callPackage "${cfg.directory}/${subdir}" {}; + mkWebsiteVHost = subdir: { + name = subdir; + # the nginx virtualhost config (for all sites) goes in here + value = { + enableACME = true; + forceSSL = true; + + locations."/".root = + (mkWebsiteDrv subdir).outPath; + }; + }; + in mkIf cfg.enable { services.nginx = { enable = true; virtualHosts = - let - subdirs = - let dirAttrs = filterAttrs - (n: v: v == "directory" || lists.elem n cfg.ignore) - (builtins.readDir cfg.directory); - in mapAttrsToList (n: v: n) dirAttrs; - - mkWebsite = subdir: { - name = subdir; - # the nginx virtualhost config (for all sites) goes in here - value = { - enableACME = 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 = - (pkgs.callPackage "${cfg.directory}/${subdir}" {}).outPath; - }; - }; - in listToAttrs (map mkWebsite subdirs); + listToAttrs (map mkWebsiteVHost subdirs); }; + hacc.websites.builders = + listToAttrs (map (subdir: { + name = subdir; + value = if (mkWebsiteDrv subdir) ? watch then (mkWebsiteDrv subdir).watch else null; + }) subdirs); }; } diff --git a/websites/hacc.earth/default.nix b/websites/hacc.earth/default.nix index 3b7d2d3..eb80f4e 100644 --- a/websites/hacc.earth/default.nix +++ b/websites/hacc.earth/default.nix @@ -1,6 +1,6 @@ -{ stdenvNoCC }: +{ stdenvNoCC, sfz, writeScriptBin }: -stdenvNoCC.mkDerivation { +stdenvNoCC.mkDerivation rec { name = "hacc.earth-static"; src = ./.; @@ -13,4 +13,7 @@ stdenvNoCC.mkDerivation { rm $out/default.nix ''; + watch = writeScriptBin "watch" '' + ${sfz}/bin/sfz -r ${src} "$@" + ''; } diff --git a/websites/help.studentsforfuture.info/default.nix b/websites/help.studentsforfuture.info/default.nix index 501be08..069a6cb 100644 --- a/websites/help.studentsforfuture.info/default.nix +++ b/websites/help.studentsforfuture.info/default.nix @@ -1,6 +1,6 @@ -{ stdenvNoCC }: +{ stdenvNoCC, sfz, writeScriptBin }: -stdenvNoCC.mkDerivation { +stdenvNoCC.mkDerivation rec { name = "muc.hacc.earth-static"; src = ./.; @@ -13,4 +13,7 @@ stdenvNoCC.mkDerivation { rm $out/default.nix ''; + watch = writeScriptBin "watch" '' + ${sfz}/bin/sfz -r ${src} + ''; } diff --git a/websites/infra4future.de/default.nix b/websites/infra4future.de/default.nix index b67444f..72d86f7 100644 --- a/websites/infra4future.de/default.nix +++ b/websites/infra4future.de/default.nix @@ -1,6 +1,6 @@ -{ jekyll, stdenvNoCC }: +{ jekyll, stdenvNoCC, writeScriptBin }: -stdenvNoCC.mkDerivation { +stdenvNoCC.mkDerivation rec { name = "infra4future.de-static"; src = ./.; @@ -11,4 +11,8 @@ stdenvNoCC.mkDerivation { ${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 + ''; } diff --git a/websites/muc.hacc.earth/default.nix b/websites/muc.hacc.earth/default.nix index 501be08..069a6cb 100644 --- a/websites/muc.hacc.earth/default.nix +++ b/websites/muc.hacc.earth/default.nix @@ -1,6 +1,6 @@ -{ stdenvNoCC }: +{ stdenvNoCC, sfz, writeScriptBin }: -stdenvNoCC.mkDerivation { +stdenvNoCC.mkDerivation rec { name = "muc.hacc.earth-static"; src = ./.; @@ -13,4 +13,7 @@ stdenvNoCC.mkDerivation { rm $out/default.nix ''; + watch = writeScriptBin "watch" '' + ${sfz}/bin/sfz -r ${src} + ''; } diff --git a/websites/mumble.infra4future.de/default.nix b/websites/mumble.infra4future.de/default.nix index 27b5234..615f53b 100644 --- a/websites/mumble.infra4future.de/default.nix +++ b/websites/mumble.infra4future.de/default.nix @@ -1,6 +1,6 @@ -{ jekyll, stdenvNoCC }: +{ jekyll, stdenvNoCC, writeScriptBin }: -stdenvNoCC.mkDerivation { +stdenvNoCC.mkDerivation rec { name = "mumble.infra4future.de-static"; src = ./.; @@ -11,4 +11,8 @@ stdenvNoCC.mkDerivation { ${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 + ''; }