about summary refs log tree commit diff
path: root/users/sterni/nix/build/website/default.nix
blob: e0a5ed75d260e149de70a44bd93d9d1b06aa8b43 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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;
}