From b2fac26a692ea43570c061d8d04e59d070c424c9 Mon Sep 17 00:00:00 2001 From: valentin gagarin Date: Wed, 13 Nov 2024 15:24:40 +0100 Subject: [PATCH] uncle bob --- default.nix | 17 ++------------ lib.nix | 67 ++++++++++++++++++++++++++++++++++++----------------- 2 files changed, 48 insertions(+), 36 deletions(-) diff --git a/default.nix b/default.nix index 7a56fdb6..ce38d1ca 100644 --- a/default.nix +++ b/default.nix @@ -10,23 +10,10 @@ }: let lib' = pkgs.callPackage ./lib.nix { }; - join = lib.concatStringsSep; in { - site = pkgs.stdenv.mkDerivation { - name = "fediversity.eu"; - src = ./content; - buildPhase = '' - true - ''; - installPhase = '' - mkdir $out - '' + join "\n" (lib.mapAttrsToList - (name: value: '' - cp ${value} $out/${name} - '') - (lib'.files ./content)); - }; + site = lib'.site "fediversity.eu" ./content; + shell = pkgs.mkShellNoCC { packages = with pkgs; [ cmark diff --git a/lib.nix b/lib.nix index 6b8053e1..8119ab52 100644 --- a/lib.nix +++ b/lib.nix @@ -1,5 +1,51 @@ { pkgs, lib, ... }: +let + join = lib.concatStringsSep; +in rec { + /** + Build the web site + */ + site = name: dir: + let + script = '' + mkdir $out + '' + join "\n" copy; + copy = lib.mapAttrsToList + ( + path: document: '' + mkdir -p $out/$(dirname ${path}) + cp ${document} $out/${path} + '' + ) + (files (sources dir)); + in + pkgs.runCommand name { } script; + + /** + Get source files from a flat directory + */ + sources = dir: lib.mapAttrs' + ( + attrname: value: { + name = lib.removeSuffix ".nix" attrname; + value = import (dir + "/${attrname}"); + } + ) + (builtins.readDir dir); + + /** + Create a mapping from output file path to document contents + */ + files = documents: lib.mapAttrs' + ( + name: document: { + name = document.outPath; + value = html document "${name}.html"; + } + ) + documents; + /** Convert a Nix document to HTML */ @@ -22,25 +68,4 @@ rec { pkgs.runCommand "${name}.html" { buildInputs = [ pkgs.cmark ]; } '' cmark ${builtins.toFile "${name}.md" markdown} > $out ''; - - /** - Get documents from a flat directory of files - */ - documents = dir: lib.mapAttrs' - ( - attrname: value: { - name = lib.removeSuffix ".nix" attrname; - value = import (dir + "/${attrname}"); - } - ) - (builtins.readDir dir); - - files = dir: lib.mapAttrs' - ( - name: document: { - name = document.outPath; - value = html document "${name}.html"; - } - ) - (documents dir); }