summaryrefslogtreecommitdiffstats
path: root/default.nix
blob: f14329d40804b3216c1bf32d629827d8e8d862bb (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
{ current-date
, current-host-name
, current-user-name
}:

let
  lib = import <nixpkgs/lib>;

  krebs-modules-path = ./krebs/3modules;
  krebs-pkgs-path = ./krebs/5pkgs;
  user-modules-path = ./. + "/${current-user-name}/3modules";
  user-pkgs-path = ./. + "/${current-user-name}/5pkgs";

  # XXX This is only used interactively, e.g. using get.
  pkgs =
    let
      pkgs = import <nixpkgs> {};
      args = {
        inherit pkgs;
        lib = pkgs.lib;
      };
    in
    pkgs //
    import krebs-pkgs-path args //
    import user-pkgs-path args;

  out =
    { inherit pkgs; } //
    lib.mapAttrs (_: builtins.getAttr "main")
      (lib.filterAttrs (_: builtins.hasAttr "main")
        (lib.mapAttrs
          (k: v:
            if lib.hasPrefix "." k || v != "directory" then
              {}
            else if builtins.pathExists (./. + "/${k}/default.nix") then
              { main = import (./. + "/${k}"); }
            else if builtins.pathExists (./. + "/${k}/1systems") then
              { main = mk-namespace (./. + "/${k}"); }
            else
              {})
          (builtins.readDir ./.)));

  eval = path: import <nixpkgs/nixos/lib/eval-config.nix> {
    system = builtins.currentSystem;
    modules = [
      path
      krebs-modules-path
      user-modules-path
    ] ++ [
      ({ config, lib, pkgs, ... }@args: {
       _module.args.pkgs =
         (import krebs-pkgs-path args) //
         (import user-pkgs-path args);
      })
    ];
  };

  mk-namespace = path: mapNixDir mk-system (path + "/1systems");

  mk-system = path: rec {
    inherit (eval path) config options;
    system = config.system.build.toplevel;
    fetch = import ./krebs/0tools/fetch.nix { inherit config lib; };
  };

  mapNixDir = f: path: lib.mapAttrs (_: f) (nixDir path);

  nixDir = path:
    builtins.listToAttrs
      (catMaybes
        (lib.mapAttrsToList
          (k: v: {
            directory =
              let p = path + "/${k}/default.nix"; in
              if builtins.pathExists p
                then Just (lib.nameValuePair k p)
                else Nothing;
            regular =
              let p = path + "/${k}"; in
              if lib.hasSuffix ".nix" p
                then Just (lib.nameValuePair (lib.removeSuffix ".nix" k) p)
                else Nothing;
          }.${v} or Nothing)
          (builtins.readDir path)));

  # TODO move to lib
  Just = x: { type = "maybe"; value = x; };
  Nothing = { type = "maybe"; };
  isMaybe = x: builtins.typeOf x == "set" && x.type or false == "maybe";
  isJust = x: isMaybe x && builtins.hasAttr "value" x;
  fromJust = x: assert isJust x; x.value;
  catMaybes = xs: map fromJust (builtins.filter isJust xs);

in out