-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.nix
321 lines (281 loc) · 9.62 KB
/
lib.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
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
{
lib,
inputs,
withSystem,
...
}:
let
inherit (inputs) self;
inherit (builtins)
readDir
elemAt
filter
pathExists
foldl'
;
inherit (lib.lists) optionals singleton flatten;
inherit (lib.attrsets)
recursiveUpdate
foldAttrs
attrValues
mapAttrs
filterAttrs
;
inherit (lib.modules) mkDefault evalModules;
inherit (lib.trivial) mergeAttrs;
classToOS = class: if (class == "darwin") then "darwin" else "linux";
classToND = class: if (class == "darwin") then "darwin" else "nixos";
redefineClass =
cfg: class:
(
(cfg.additionalClasses or { })
// {
linux = "nixos";
}
).${class} or class;
constructSystem =
config: class: arch:
let
class' = redefineClass config class;
os = classToOS class';
in
"${arch}-${os}";
splitSystem =
system:
let
sp = builtins.split "-" system;
arch = elemAt sp 0;
class = elemAt sp 2;
in
{
inherit arch class;
};
/**
mkHost is a function that uses withSystem to give us inputs' and self'
it also assumes the the system type either nixos or darwin and uses the appropriate
# Type
```
mkHost :: AttrSet -> AttrSet
```
# Example
```nix
mkHost {
name = "myhost";
path = "/path/to/host";
system = "x86_64-linux";
class = "nixos";
modules = [ ./module.nix ];
specialArgs = { foo = "bar"; };
}
```
*/
mkHost =
{
name,
path,
# by the time we recive the argument here it can only be one of
# nixos, darwin, or iso. The redefineClass function should be used prior
class,
system,
modules ? [ ],
specialArgs ? { },
...
}:
let
darwinInput = inputs.darwin or inputs.nix-darwin or (throw "cannot find nix-darwin input");
nixpkgs = inputs.nixpkgs or (throw "cannot find nixpkgs input");
# create the modulesPath based on the system, we need
modulesPath = if class == "darwin" then "${darwinInput}/modules" else "${nixpkgs}/nixos/modules";
# we need to import the module list for our system
# this is either the nixos modules list provided by nixpkgs
# or the darwin modules list provided by nix darwin
baseModules = import "${modulesPath}/module-list.nix";
eval = evalModules {
# we use recursiveUpdate such that users can "override" the specialArgs
#
# This should only be used for special arguments that need to be evaluated
# when resolving module structure (like in imports).
specialArgs = recursiveUpdate {
inherit
# these are normal args that people expect to be passed,
# but we expect to be evaulated when resolving module structure
inputs
# even though self is just the same as `inputs.self`
# we still pass this as some people will use this
self
# we need to set this beacuse some modules require it sadly
# you may also recall `modulesPath + /installer/scan/not-detected.nix`
modulesPath
;
} specialArgs;
# A nominal type for modules. When set and non-null, this adds a check to
# make sure that only compatible modules are imported.
class = classToND class;
modules = flatten [
# bring in all of our base modules
baseModules
# import our host system paths
(
if path != null then
path
else
(filter pathExists [
# if the previous path does not exist then we will try to import some paths with some assumptions
"${self}/hosts/${name}/default.nix"
"${self}/systems/${name}/default.nix"
])
)
# get an installer profile from nixpkgs to base the Isos off of
# this is useful because it makes things alot easier
(optionals (class == "iso") [
"${nixpkgs}/nixos/modules/installer/cd-dvd/installation-cd-minimal-new-kernel.nix"
])
# the next 3 singleton's are split up to make it easier to understand as they do things diffrent things
# recall `specialArgs` would take be prefered when resolving module structure
# well this is how we do it use it for all args that don't need to rosolve module structure
(singleton {
_module.args = withSystem system (
{ self', inputs', ... }:
{
inherit self' inputs';
}
);
})
# some modules to have these arguments, like documentation.nix
# <https://github.com/NixOS/nixpkgs/blob/9692553cb583e8dca46b66ab76c0eb2ada1a4098/nixos/modules/misc/documentation.nix>
(singleton {
_module.args = {
inherit baseModules;
# this should in the future be the modules that the user added without baseModules
modules = [ ];
# TODO: remove in 25.05
# https://github.com/NixOS/nixpkgs/blob/9692553cb583e8dca46b66ab76c0eb2ada1a4098/nixos/lib/eval-config.nix#L38
extraModules = [ ];
};
})
# here we make some basic assumptions about the system the person is using
# like the system type and the hostname
(singleton {
# we set the systems hostname based on the host value
# which should be a string that is the hostname of the system
networking.hostName = mkDefault name;
nixpkgs = {
# you can also do this as `inherit system;` with the normal `lib.nixosSystem`
# however for evalModules this will not work, so we do this instead
hostPlatform = mkDefault system;
# The path to the nixpkgs sources used to build the system.
# This is automatically set up to be the store path of the nixpkgs flake used to build
# the system if using lib.nixosSystem, and is otherwise null by default.
# so that means that we should set it to our nixpkgs flake output path
flake.source = nixpkgs.outPath;
};
})
# if we are on darwin we need to import the nixpkgs source, its used in some
# modules, if this is not set then you will get an error
(optionals (class == "darwin") (singleton {
# without supplying an upstream nixpkgs source, nix-darwin will not be able to build
# and will complain and log an error demanding that you must set this value
nixpkgs.source = mkDefault nixpkgs;
system = {
# i don't quite know why this is set but upstream does it so i will too
checks.verifyNixPath = false;
# we use these values to keep track of what upstream revision we are on, this also
# prevents us from recreating docs for the same configuration build if nothing has changed
darwinVersionSuffix = ".${darwinInput.shortRev or darwinInput.dirtyShortRev or "dirty"}";
darwinRevision = darwinInput.rev or darwinInput.dirtyRev or "dirty";
};
}))
# import any additional modules that the user has provided
modules
];
};
in
if ((classToND class) == "nixos") then
{ nixosConfigurations.${name} = eval; }
else
{
darwinConfigurations.${name} = eval // {
system = eval.config.system.build.toplevel;
};
};
foldAttrsReccursive = foldl' (acc: attrs: recursiveUpdate acc attrs) { };
mkHosts =
easyHostsConfig:
foldAttrs mergeAttrs { } (
attrValues (
mapAttrs (
name: hostConfig:
mkHost {
inherit name;
inherit (hostConfig) system path;
class = redefineClass easyHostsConfig hostConfig.class;
# merging is handled later
modules = [
hostConfig.modules
easyHostsConfig.shared.modules
(easyHostsConfig.perClass hostConfig.class).modules
];
specialArgs = foldAttrsReccursive [
hostConfig.specialArgs
easyHostsConfig.shared.specialArgs
(easyHostsConfig.perClass hostConfig.class).specialArgs
];
}
) easyHostsConfig.hosts
)
);
normaliseHosts =
cfg: hosts:
if (cfg.onlySystem == null) then
foldAttrs (acc: host: acc // host) { } (
attrValues (
mapAttrs (
system: hosts':
mapAttrs (
name: _:
let
inherit (splitSystem system) arch class;
in
{
inherit arch class;
system = constructSystem cfg arch class;
path = "${cfg.path}/${system}/${name}";
}
) hosts'
) hosts
)
)
else
mapAttrs (
host: _:
let
inherit (splitSystem cfg.onlySystem) arch class;
in
{
inherit arch class;
system = constructSystem cfg arch class;
path = "${cfg.path}/${host}";
}
) hosts;
buildHosts =
cfg:
let
hostsDir = readDir cfg.path;
hosts =
if (cfg.onlySystem != null) then
hostsDir
else
mapAttrs (path: _: readDir "${cfg.path}/${path}") (
filterAttrs (_: type: type == "directory") hostsDir
);
in
normaliseHosts cfg hosts;
in
{
inherit
constructSystem
mkHost
mkHosts
buildHosts
;
}