aboutsummaryrefslogtreecommitdiff
path: root/www/framework/default.nix
diff options
context:
space:
mode:
authorValentin <valentin@fricklerhandwerk.de>2025-12-09 17:48:21 +0100
committerValentin <valentin@fricklerhandwerk.de>2025-12-20 17:44:07 +0100
commit1a99a306ae044625e3f89e16d1d6f809ce8c12e0 (patch)
tree3a04bfee536d0cfe885e27dc7b916fd353380d5a /www/framework/default.nix
parent9a5020deaed97df8ab1307068f34a382b2e24b31 (diff)
Website-Erzeugung aus Nixpkgs-Modul statt Paket
Die Dateien für die Website mit Nix zu erzeugen ist zur Zeit der kleinste Schritt, um repetitive Muster weg zu abstrahieren. Alles andere bräuchte mehr Infrastruktur, während Nix bereits da ist und direkt verwendet werden kann. Es erlaubt jetzt einen nahtlosen Übergang zu einer strukturierteren Darstellung. Selbst falls das System sehr komplex wird, bevor es auf einen Anwendungsserver mit Datenbank usw. migriert werden kann, können im Prinzip Datentypen für die Sprache der Anwendung und sogar Nutzdaten im Zielformat aus den Modulsystem-Typen generiert werden. Das würde diese spätere Migration zumindest abmildern. Wichtig: Dateinamen dürfen keine Leerzeichen mehr enthalten, um genau zu sein siehe [0]. [0]: <https://github.com/NixOS/nix/commit/f1b4663805a9dbcb1ace64ec110092d17c9155e0>
Diffstat (limited to 'www/framework/default.nix')
-rw-r--r--www/framework/default.nix134
1 files changed, 134 insertions, 0 deletions
diff --git a/www/framework/default.nix b/www/framework/default.nix
new file mode 100644
index 0000000..92e1ed8
--- /dev/null
+++ b/www/framework/default.nix
@@ -0,0 +1,134 @@
+{
+ config,
+ pkgs,
+ lib,
+ ...
+}:
+let
+ inherit (lib) mkOption types;
+in
+{
+ imports =
+ with lib.fileset;
+ toList (difference (fileFilter (file: file.hasExt "nix") ./.) ./default.nix);
+
+ options = {
+ types = mkOption {
+ description = "Datentypen für Website-Inhalte";
+ type = with types; lazyAttrsOf deferredModule;
+ default = { };
+ };
+
+ files = mkOption {
+ description = ''
+ Dateien, aus denen die Website besteht, als Abbildung vom Dateipfad zum Inhalt
+ '';
+ type = with types; attrsOf (either path (submodule config.types.file));
+ };
+
+ redirects.raw = mkOption {
+ description = ''
+ Weiterleitungen aller historischen Dateipfade auf Kanonische Pfade
+ '';
+ type = with types; lazyAttrsOf str;
+ readOnly = true;
+ default =
+ with lib;
+ listToAttrs (
+ concatMap (
+ name:
+ if config.files.${name} ? locations then
+ map (loc: {
+ name = "/" + loc;
+ value = "/" + name;
+ }) (tail config.files.${name}.locations)
+ else
+ [ ]
+ ) (attrNames config.files)
+ );
+ };
+
+ redirects.caddy = mkOption {
+ description = ''
+ Weiterleitungen im Caddyfile-Format
+ '';
+ type = types.str;
+ readOnly = true;
+ default =
+ with lib;
+ let
+ lines = mapAttrsToList (name: value: ''redir "${name}" ${value} permanent'') config.redirects.raw;
+ in
+ concatStringsSep "\n" lines;
+ };
+
+ result = mkOption {
+ description = ''
+ Fertige Website als Verzeichnis
+ '';
+ type = types.package;
+ readOnly = true;
+ default =
+ let
+ script = ''
+ mkdir $out
+ ''
+ + lib.concatStringsSep "\n" copy;
+ copy = lib.mapAttrsToList (
+ path: file:
+ let
+ content = if file ? path then file.path else file;
+ in
+ ''
+ mkdir -p $out/$(dirname '${toString path}')
+ ${
+ "" # `--no-preserve-mode` ist erforderlich da Nix store paths read-only sind, und man andernfalls `cp -r` nicht durchführen kann.
+ }cp -r --no-preserve=mode ${content} $out/'${toString path}'
+ ''
+ ) config.files;
+ in
+ pkgs.runCommand "source" { } script;
+ };
+ };
+
+ config.types.document =
+ { lib, ... }:
+ let
+ inherit (lib) mkOption types;
+ in
+ {
+ options = {
+ locations = mkOption {
+ description = ''
+ Frühere Pfade dieses Dokuments
+
+ Einträge sind relative Pfade.
+ Der erste Eintrag ist der kanonische Pfad.
+ Alle anderen Elemente werden für Weiterleitungen zum kanonischen Pfad benutzt.
+ '';
+ type = with types; nonEmptyListOf str;
+ example = [
+ "about/overview"
+ "index"
+ ];
+ };
+ };
+ };
+
+ config.types.file =
+ { lib, ... }:
+ let
+ inherit (lib) mkOption types;
+ in
+ {
+ imports = [ config.types.document ];
+ options = {
+ path = mkOption {
+ type = types.path;
+ description = ''
+ Datei mit manuell gesteuertem Pfad
+ '';
+ };
+ };
+ };
+}