-
Notifications
You must be signed in to change notification settings - Fork 7
/
flake-module.nix
184 lines (162 loc) · 5.13 KB
/
flake-module.nix
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
{ darwin, home-manager, ... }: inputs@{ lib, config, ... }:
let
inherit (builtins) listToAttrs pathExists;
inherit (lib) mkOption types concatMap mapAttrsToList nixosSystem optionals;
inherit (darwin.lib) darwinSystem;
inherit (home-manager.lib) homeManagerConfiguration;
cfg = config.ezConfigs;
# Import a module of a given name if it exists.
# First check for a `.nix` file, then a directory.
importModule = name:
let
file = cfg.root + "/${name}.nix";
dir = cfg.root + "/${name}";
in
if pathExists file then file
else if pathExists dir then dir
else { };
# Generates an atterset of nixosConfigurations or darwinConfigurations.
# systemBuilder: The function to use to build the system. i.e. nixosSystem or darwinSystem
# defaultModule: The module to always include in the system. i.e. ./nixos or ./darwin
# hosts: A list of host declarations. i.e. [ { host = "foo"; system = "aarch64-darwin"; } ... ]
systemsWith = { systemBuilder, systemSuffix, defaultModules, hostsDirectory }: hosts:
listToAttrs (map
({ name, arch }: {
inherit name;
value = systemBuilder {
system = "${arch}-${systemSuffix}";
specialArgs = { inherit inputs; };
modules = defaultModules ++ [
(importModule "${hostsDirectory}/${name}")
];
};
})
hosts);
# Combined list attrsets containing the hostname
# and it's configuration for all nixos and darwin systems.
allHosts = mapAttrsToList
(host: systemConf: systemConf //
{ inherit host; })
(config.nixosConfigurations //
config.darwinConfigurations);
# Creates home-manager confgurations for each user on each host.
# Tries to import users/${user} for each user.
# Conditionally imports home/darwin and home/linux based on the host system.
userConfigs = users:
listToAttrs (concatMap
({ host, pkgs, ... }:
let
inherit (pkgs.stdenv) isDarwin isLinux;
in
map
({ name }: {
name = "${name}@${host}";
value = homeManagerConfiguration {
inherit pkgs;
extraSpecialArgs = { inherit inputs; };
modules = [ ./home (importModule "${cfg.hm.directory}/${name}") ] ++
optionals isDarwin [ (importModule "${cfg.hm.directory}/darwin") ] ++
optionals isLinux [ (importModule "${cfg.hm.directory}/linux") ];
};
})
users)
allHosts);
hostDefinition.options = {
name = mkOption {
type = types.string;
description = "The hostname of the system.";
};
arch = mkOption {
type = types.enum [ "x86_64" "aarch64" ];
description = "The architecture of the system.";
};
};
userDefinition.options = {
name = mkOption {
type = types.string;
description = "The name of the user.";
};
};
in
{
options.ezConfigs = {
root = mkOption {
type = types.pathInStore;
description = ''
The root from which configuration modules should be searched. You most likely want this to be `./.` or `self`.
'';
};
hm = {
directory = mkOption {
default = "home";
type = types.string;
description = ''
The directory in which to look for home-manager configurations.
'';
};
users = mkOption {
default = [ ];
type = types.listOf (types.submodule userDefinition);
};
};
nixos = {
directory = mkOption {
default = "nixos";
type = types.string;
description = ''
The directory in which to look for nixos configurations.
'';
};
hostDirectory = mkOption {
default = "hosts";
type = types.string;
description = ''
The directory in which to look for host specific nixos configurations.
'';
};
hosts = mkOption {
default = [ ];
type = types.listOf (types.submodule hostDefinition);
};
};
darwin = {
directory = mkOption {
default = "darwin";
type = types.string;
description = ''
The directory in which to look for darwin configurations.
'';
};
hostDirectory = mkOption {
default = "hosts";
type = types.string;
description = ''
The directory in which to look for host specific darwin configurations.
'';
};
hosts = mkOption {
default = [ ];
type = types.listOf (types.submodule hostDefinition);
};
};
};
config.flake = {
homeConfigurations = userConfigs cfg.hm.users;
nixosConfigurations = systemsWith
{
systemBuilder = nixosSystem;
systemSuffix = "linux";
defaultModules = [ (importModule "${cfg.nixos.directory}") ];
hostsDirectory = cfg.nixos.hostDirectory;
}
cfg.nixos.hosts;
darwinConfigurations = systemsWith
{
systemBuilder = darwinSystem;
systemSuffix = "darwin";
defaultModules = [ (importModule "${cfg.darwin.directory}") ];
hostsDirectory = cfg.darwin.hostDirectory;
}
cfg.darwin.hosts;
};
}