aboutsummaryrefslogtreecommitdiff
path: root/lib/nix-furnace/default.nix
blob: 7c18ae92148db32fba895abfc9e6ea2b90f6cfe2 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
let
  lazyImport = path:
    if (builtins.pathExists path)
    then import path
    else {};

  mkIfNull = cond: onTrue:
    if cond
    then onTrue
    else null;

  getSubdirs = dir:
    builtins.filter (x: x != null)
    (builtins.attrValues
      (builtins.mapAttrs
        (name: value: mkIfNull (value == "directory") name)
        (builtins.readDir dir)));

  getSubfiles = dir:
    builtins.filter (x: x != null)
    (builtins.attrValues
      (builtins.mapAttrs
        (name: value: mkIfNull (value == "file") name)
        (builtins.readDir dir)));

  mkHomeImports = modName: [
    (lazyImport ../../modules/${modName}/options.nix)
    (lazyImport ../../modules/${modName}/home/default.nix)
  ];
  mkSystemImports = modName: [
    (lazyImport ../../modules/${modName}/options.nix)
    (lazyImport ../../modules/${modName}/system/default.nix)
  ];

  mkModules = mapFn: {
    imports =
      [../../modules/options.nix]
      ++ (builtins.foldl' (a: b: a ++ b) [] (builtins.map mapFn (getSubdirs ../../modules)));
  };

  mkHomeModules = mkModules mkHomeImports;
  mkSystemModules = mkModules mkSystemImports;

  mkHost = {
    hostname,
    namespace,
    inputs,
    ...
  }: let
    preloadedConfig = import ../../hosts/${hostname}/config.nix {inherit inputs;};
    username = preloadedConfig."${namespace}".user.name;

    furnaceOptions = {lib, ...}: let
      inherit (lib) mkOption types;
    in {
      options.nix-furnace = {
        extraHomeModules = mkOption {
          type = with types; listOf path;
          default = [];
        };

        extraSystemModules = mkOption {
          type = with types; listOf path;
          default = [];
        };
      };
    };
  in
    inputs.nixpkgs.lib.nixosSystem {
      specialArgs = {inherit inputs;};
      modules =
        [
          {networking.hostName = hostname;}

          ../../hosts/${hostname}/config.nix

          mkSystemModules

          furnaceOptions

          # Home-manager
          inputs.home-manager.nixosModules.home-manager
          {
            home-manager = {
              backupFileExtension = "bak";
              useGlobalPkgs = true;
              useUserPackages = true;
              extraSpecialArgs = {inherit inputs;};
              users."${username}" = {
                imports =
                  [
                    mkHomeModules

                    furnaceOptions

                    ../../hosts/${hostname}/config.nix
                  ]
                  ++ preloadedConfig.nix-furnace.extraHomeModules;
              };
            };
          }
        ]
        ++ preloadedConfig.nix-furnace.extraSystemModules;
    };

  mkFlake = {
    namespace,
    inputs,
    ...
  }: {
    nixosConfigurations =
      builtins.listToAttrs
      (builtins.map (hostname: {
          name = hostname;
          value = mkHost {inherit inputs hostname namespace;};
        })
        (getSubdirs ../../hosts));
  };
in {
  inherit mkFlake;
}