From 984c930a7c9a9923e70400c30ff5cad1ca5c6325 Mon Sep 17 00:00:00 2001 From: sterni Date: Thu, 27 Feb 2025 02:18:34 +0100 Subject: [PATCH] feat(sterni/nix/build): add my simplistic nix website authoring tool I am already using this outside of depot where it isn't under VCS. It makes sense to canonicalize it, also to ensure that it stays somewhat generic. Change-Id: I5595d98ab4198794c395feb4d3c08df1e2d01a36 Reviewed-on: https://cl.tvl.fyi/c/depot/+/13184 Autosubmit: sterni Reviewed-by: sterni Tested-by: BuildkiteCI --- .../machines/ingeborg/http/sterni.lv.nix | 20 ++++---- users/sterni/nix/build/website/default.nix | 50 +++++++++++++++++++ 2 files changed, 60 insertions(+), 10 deletions(-) create mode 100644 users/sterni/nix/build/website/default.nix diff --git a/users/sterni/machines/ingeborg/http/sterni.lv.nix b/users/sterni/machines/ingeborg/http/sterni.lv.nix index d71ced6df..870a71983 100644 --- a/users/sterni/machines/ingeborg/http/sterni.lv.nix +++ b/users/sterni/machines/ingeborg/http/sterni.lv.nix @@ -15,16 +15,16 @@ in services.nginx.virtualHosts."sterni.lv" = { enableACME = true; forceSSL = true; - root = pkgs.writeTextFile { - name = "sterni.lv-http-root"; - destination = "/index.html"; - text = { } [ - ( { } [ - ( { charset = "utf-8"; } null) - ( { } "no thoughts") - ]) - (<body> { } "🦩") - ]; + root = depot.users.sterni.nix.build.website "sterni.lv" { } { + "index.html" = { ... }: pkgs.writeText "index.html" ( + <html> { } [ + (<head> { } [ + (<meta> { charset = "utf-8"; } null) + (<title> { } "no thoughts") + ]) + (<body> { } "🦩") + ] + ); }; # TODO(sterni): tmp.sterni.lv locations."/tmp/".root = toString /srv/http; diff --git a/users/sterni/nix/build/website/default.nix b/users/sterni/nix/build/website/default.nix new file mode 100644 index 000000000..e0a5ed75d --- /dev/null +++ b/users/sterni/nix/build/website/default.nix @@ -0,0 +1,50 @@ +# Experimental Nix Static Website Authoring Tool +# +# Proof of Concept for a Nix library that allows for authoring static websites +# in Nix in a relatively ad hoc way, i.e. no specific markup or structure +# requirements. In particular, the library can help with creating relative +# references between files/pages so that the website is fully relocatable via +# (relativeToRoot). I use this for fairly trivial websites at the moment though +# I'm not happy with the relative linking feature yet. The API is probably going +# to be redesigned to improve this—you have been warned. +{ depot, pkgs, lib, ... }: + +# TODO(sterni): implement generic deploy script +# TODO(sterni): port orgPage to depot +# TODO(sterni): replace relativeToRoot with a fixpoint that exposes (relative) urls +let + inherit (lib) + isDerivation + ; + + inherit (depot.nix) + writeTree + utils + ; + + minify = type: file: pkgs.runCommandNoCC (utils.storePathName file) + { + nativeBuildInputs = [ pkgs.buildPackages.minify ]; + env = { inherit file; }; + } + '' + minify --type ${type} < "$file" > "$out" + ''; + + makeWebsite = name: { args ? { } }: tree: + let + callTree = relativeToRoot: tree: + if builtins.isFunction tree then + tree (args // { inherit relativeToRoot; }) + else if builtins.isAttrs tree && !(isDerivation tree) then + builtins.mapAttrs (_: v: callTree "${relativeToRoot}../" v) tree + else + tree; + in + writeTree name (builtins.mapAttrs (_: callTree "") tree); +in + +{ + __functor = _: makeWebsite; + inherit makeWebsite minify; +}