-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflake-module.nix
More file actions
399 lines (357 loc) · 11.6 KB
/
flake-module.nix
File metadata and controls
399 lines (357 loc) · 11.6 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
{
inputs,
config,
lib,
...
}:
let
inherit (builtins)
pathExists
;
inherit (lib)
mkOption
types
optional
optionals
literalExpression
genAttrs'
;
in
{
options.hostess =
let
mkInputOption =
n:
mkOption {
type = types.raw;
example = literalExpression "inputs.${n}";
description = ''
Injected input for ${n}.
'';
};
mkOptionalInputOption =
n:
mkOption {
type = types.nullOr types.raw;
default = null;
description = ''
(Optional) Injected input for ${n}.
'';
};
mkPathOption =
n: d:
mkOption {
type = types.nullOr types.path;
default = d;
description = ''
The ${n} path.
'';
};
cfg = config.hostess;
in
{
# Injected inputs.
nixpkgs = mkInputOption "nixpkgs"; # Doy.
home-manager = mkInputOption "Home Manager"; # (Optional) handles Home Manager configs automatically.
disko = mkOptionalInputOption "Disko"; # (Optional) handles disko automatically.
# Path options.
root = mkPathOption "root" null;
hostPath = mkPathOption "host" (cfg.root + "/hosts");
homePath = mkPathOption "home" null;
nixosModulePath = mkPathOption "nixos modules" (cfg.root + "/modules/nixos");
nixosModulePaths = mkOption {
type = types.listOf types.path;
default = [ ];
description = ''
NixOS module search paths.
'';
};
homeModulePath = mkPathOption "Home Manager modules" null;
homeModulePaths = mkOption {
type = types.listOf types.path;
default = [ ];
description = ''
Home Manager module search paths.
'';
};
profilePath = mkPathOption "profiles" (cfg.root + "/profiles");
profilePaths = mkOption {
type = types.listOf types.path;
default = [ ];
description = ''
Profile search paths.
'';
};
namespaces = mkOption {
default = { };
type = types.attrsOf (
types.submodule {
options = {
nixosModulePaths = mkOption {
type = types.listOf types.path;
default = [ ];
description = ''
NixOS module search paths in this namespace.
'';
};
homeModulePaths = mkOption {
type = types.listOf types.path;
default = [ ];
description = ''
Home Manager module search paths in this namespace.
'';
};
profilePaths = mkOption {
type = types.listOf types.path;
default = [ ];
description = ''
Profile search paths in this namespace.
'';
};
};
}
);
};
# Configs.
commonNixosModules = mkOption {
type = types.listOf types.raw;
default = [ ];
example = [
./somemodule.nix
"somemods/global"
"extras:coolstuff"
];
};
commonHomeModules = mkOption {
type = types.listOf types.raw;
default = [ ];
example = [
./some-homemodule.nix
"homemods/global"
"extras:somemodule"
];
};
commonNixpkgsConfig = mkOption {
type = types.raw;
default = { };
description = ''
A global default nixpkgs config.
'';
};
commonOverlays = mkOption {
type = types.listOf types.raw;
default = [ ];
description = ''
A list of global overlays to apply.
'';
};
metaSpecialArgs = mkOption {
type = types.raw;
default = { };
description = ''
Extra arguments to pass to the meta.nix of every host.
'';
};
homeMetaSpecialArgs = mkOption {
type = types.raw;
default = { };
description = ''
Extra arguments passed to the meta.nix of every home config.
'';
};
nixosSpecialArgs = mkOption {
description = "Special args to pass to NixOS systems.";
type = types.raw;
default = { };
};
homeSpecialArgs = mkOption {
description = "Special args to pass to Home Manager configs.";
type = types.raw;
default = { };
};
perTagRules = mkOption {
description = "Rules for host tags";
default = { };
type = types.attrsOf types.submodule {
options = {
modules = mkOption {
description = "Extra modules for hosts under this tag";
type = types.listOf types.raw;
default = [ ];
};
profiles = mkOption {
description = "Extra profiles for hosts under this tag";
type = types.listOf types.raw;
default = [ ];
};
};
};
};
};
config =
let
cfg = config.hostess;
in
{
flake =
let
inherit (libHostess.hostess)
subdirs
safeImport
deferModule
collectCommonNixosModules
compileNamespacedNixosModuleList
compileNamespacedHomeModuleList
compileNamespacedProfileList
;
libHostess = lib.extend (import ./lib { inherit config; });
buildHost =
hostname:
let
hostDir = cfg.hostPath + "/${hostname}";
meta =
deferModule
(safeImport (hostDir + "/meta.nix") {
system = "x86_64-linux"; # System.
tags = [ ]; # Assigned tags.
modules = [ ]; # Modules.
profiles = [ ]; # Profiles.
useCommonModule = true; # Whether or not to use the common module.
useCore = true; # Whether or not to include Hostess' core module.
useLibHostess = true; # Whether to use Hostess' library. Necessary for including extra modules.
meta = { }; # Extra user defined metadata.
home-manager = { }; # Home manager settings.
})
(
{
inherit inputs;
lib = libHostess;
}
// cfg.metaSpecialArgs
);
system = meta.system or "x86_64-linux";
useCommonModule = meta.useCommonModule or true;
pkgs = import cfg.nixpkgs {
inherit system;
config = cfg.commonNixpkgsConfig;
overlays = cfg.commonOverlays;
};
tagRules = map (
tag:
cfg.perTagRules."${tag}" or {
modules = [ ];
profiles = [ ];
}
) (meta.tags or [ ]);
tagModules = builtins.concatLists (map (r: r.modules or [ ]) tagRules);
tagProfiles = builtins.concatLists (map (r: r.profiles or [ ]) tagRules);
# Use lib.lists.unique to prevent doubly including modules/profiles.
finalModules = lib.lists.unique ((meta.modules or [ ]) ++ tagModules);
finalProfiles = lib.lists.unique ((meta.profiles or [ ]) ++ tagProfiles);
namedModules = compileNamespacedNixosModuleList finalModules;
namedProfiles = compileNamespacedProfileList finalProfiles;
usingDisko = (!isNull cfg.disko) && (pathExists (hostDir + "/disk.nix"));
diskoModules = optionals usingDisko [
cfg.disko.nixosModules.disko
(safeImport (hostDir + "/disk.nix") { })
];
homeUsers = meta.home-manager.users or [ ];
buildHomeDesc =
name:
let
homeDir = (cfg.homePath + "/${name}");
userMeta =
deferModule
(safeImport (homeDir + "/meta.nix") {
inherit name;
useCommonModule = true;
modules = [ ];
})
(
{
inherit config inputs meta;
lib = libHostess;
}
// cfg.homeMetaSpecialArgs
);
homeCommon = optionals (userMeta.useCommonModule or true) (
compileNamespacedHomeModuleList cfg.commonHomeModules
);
homeNamed = compileNamespacedHomeModuleList (userMeta.modules or [ ]);
in
{
name = userMeta.name or name;
rawModule = {
imports = homeCommon ++ homeNamed ++ [ (homeDir + "/default.nix") ];
};
};
homeConfig =
meta.home-manager or {
enable = false;
useLibHostess = true; # Will be passed as a special arg, home configs requiring *may* throw errors.
useGlobalPkgs = true;
useUserPackages = true;
};
homeEnable = homeConfig.enable or false;
homeUserModules = lib.pipe homeUsers [
(map buildHomeDesc)
(
x:
genAttrs' x (d: {
name = d.name;
value = d.rawModule;
})
)
];
homeModule = optionals homeEnable [
cfg.home-manager.nixosModules.home-manager
(
{ ... }:
{
home-manager.useGlobalPkgs = homeConfig.useGlobalPkgs or true;
home-manager.useUserPackages = homeConfig.useUserPackages or true;
home-manager.extraSpecialArgs = {
useLibHostess = homeConfig.useLibHostess or true;
}
// (
if (homeConfig.useLibHostess or true) then
{
lib = libHostess;
}
else
{ }
);
home-manager.users = homeUserModules;
}
)
];
in
cfg.nixpkgs.lib.nixosSystem {
inherit system pkgs;
modules =
(optionals useCommonModule collectCommonNixosModules)
++ namedModules
++ namedProfiles
++ homeModule
++ (optionals useCommonModule cfg.commonNixosModules)
++ diskoModules
++ (optional (pathExists (hostDir + "/configuration.nix")) (hostDir + "/configuration.nix"));
specialArgs = {
useLibHostess = meta.useLibHostess or true;
meta = meta.meta or { };
};
};
hostNames = if pathExists cfg.hostPath then subdirs cfg.hostPath else [ ];
nixosConfigurations = builtins.listToAttrs (
map (name: {
inherit name;
value = buildHost name;
}) hostNames
);
in
{
inherit nixosConfigurations;
inherit (config) hostess;
};
};
}