summaryrefslogtreecommitdiffstats
path: root/default.nix
blob: 656a7f4b390eb5a5297943b5a1ac158d6b56cec7 (plain)
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
{ current-host-name ?
    let v = builtins.getEnv "HOSTNAME"; in
    if v != "" then v else builtins.readFile /proc/sys/kernel/hostname
, current-user-name ?
    let v = builtins.getEnv "LOGNAME"; in
    if v != "" then v else abort "undefined variable: LOGNAME"
, StrictHostKeyChecking ? "yes"
}@args:

let stockholm = {
    inherit krebs;
    inherit users;
    inherit lib;
    inherit pkgs;
  };

  krebs = import ./krebs (args // { inherit lib stockholm; });

  lib = let
    nlib = import (slib.npath "lib");
    klib = import (slib.kpath "4lib") { lib = nlib; };
    slib = rec {
      npath = p: <nixpkgs> + "/${p}";
      kpath = p: ./. + "/krebs/${p}";
      upath = p: ./. + "/${current-user-name}/${p}";
    };
    ulib = let p = slib.upath "4lib"; in
      nlib.optionalAttrs (klib.dir.has-default-nix p)
                         (import p { lib = nlib // klib; });
  in nlib // klib // slib // ulib // builtins;

  inherit (eval {}) pkgs;

  base-module = { config, ... }: {
    imports = builtins.filter lib.dir.has-default-nix (lib.concatLists [
      (map (f: f "2configs") [ lib.upath ])
      (map (f: f "3modules") [ lib.kpath lib.upath ])
    ]);

    krebs.current.enable = true;
    krebs.current.host = config.krebs.hosts.${current-host-name};
    krebs.current.user = config.krebs.users.${current-user-name};

    nixpkgs.config.packageOverrides = pkgs: let
      kpkgs = import (lib.kpath "5pkgs") { inherit lib pkgs; };
      upkgs = import (lib.upath "5pkgs") { inherit lib; pkgs = pkgs // kpkgs; };
    in kpkgs // upkgs;
  };

  eval = config: import (lib.npath "nixos/lib/eval-config.nix") {
    specialArgs = {
      inherit lib;
    };
    modules = [
      base-module
      config
    ];
  };

  # TODO move user namespaces' to users/, so no exception for krebs/ is needed
  users =
    lib.mapAttrs
      (name: _: eval-all-systems (lib.nspath name "1systems"))
      (lib.filterAttrs
        (n: t: !lib.hasPrefix "." n && t == "directory" && n != "krebs")
        (builtins.readDir ./.));

  eval-all-systems = path:
    lib.mapAttrs'
      (n: _: (lib.nameValuePair (lib.removeSuffix ".nix" n)
                                (eval-system (path + "/${n}"))))
      (builtins.readDir path);

  eval-system = path: rec {
    inherit (eval path) config options;
    system = config.system.build.toplevel;
  };

in stockholm