-
-
Notifications
You must be signed in to change notification settings - Fork 366
Expand file tree
/
Copy patheval-machine-info.nix
More file actions
342 lines (295 loc) · 11 KB
/
eval-machine-info.nix
File metadata and controls
342 lines (295 loc) · 11 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
{ system ? builtins.currentSystem
, networkExprs
, flakeReference ? null
, flakeAttribute
, checkConfigurationOptions ? true
, uuid
, deploymentName
, args
, pluginNixExprs
}:
let
/* Return an attribute from nested attribute sets.
Example:
x = { a = { b = 3; }; }
attrByPath ["a" "b"] x
=> 3
x = { a = { b = 3; }; }
attrByPath ["a" "c"] x
error: can't found `a.c` in the set
*/
attrByPath = attrPath: e:
let
attrByPathRec = attrPath: e:
# if it's the end return the element it's self
if attrPath == [] then
{inherit e; found = true;}
else
let attr = builtins.head attrPath; in
if e ? ${attr} then
attrByPathRec (builtins.tail attrPath) e.${attr}
else
{e=null; found = false;};
result = attrByPathRec attrPath e;
in
# Check result
if result.found then
# If found return the value
result.e
else
# Not found, Create the searching path
let path = builtins.concatStringsSep "." attrPath; in
# Throw an exception
throw "can't found `${path}` in the set";
call = x: if builtins.isFunction x then x args else x;
# Copied from nixpkgs to avoid <nixpkgs> import
optional = cond: elem: if cond then [elem] else [];
zipAttrs = set: builtins.listToAttrs (
map (name: { inherit name; value = builtins.catAttrs name set; }) (builtins.concatMap builtins.attrNames set));
# the flake expresion
flakeExpr =
let
# Get the flake config
flake = builtins.getFlake flakeReference;
# get the chosen deployement.
deploy = attrByPath flakeAttribute flake.outputs.nixopsConfigurations;
in
# Return the deployement found.
deploy;
networks =
let
getNetworkFromExpr = networkExpr:
(call (import networkExpr)) // { _file = networkExpr; };
exprToKey = key: { key = toString key; };
networkExprClosure = builtins.genericClosure {
startSet = map exprToKey networkExprs;
operator = { key }: map exprToKey ((getNetworkFromExpr key).require or []);
};
in
map ({ key }: getNetworkFromExpr key) networkExprClosure
++ optional (flakeReference != null)
((call flakeExpr) // { _file = "<${flakeReference}>"; });
network = zipAttrs networks;
evalConfig =
if flakeReference != null
then
if network ? nixpkgs
then (builtins.head (network.nixpkgs)).lib.nixosSystem
else throw "NixOps network must have a 'nixpkgs' attribute"
else import (pkgs.path + "/nixos/lib/eval-config.nix");
pkgs = if flakeReference != null
then
if network ? nixpkgs
then (builtins.head network.nixpkgs).legacyPackages.${system}
else throw "NixOps network must have a 'nixpkgs' attribute"
else (builtins.head (network.network)).nixpkgs or (import <nixpkgs> { inherit system; });
inherit (pkgs) lib;
# Expose path to imported nixpkgs (currently only used to find version suffix)
nixpkgs = builtins.unsafeDiscardStringContext pkgs.path;
in rec {
inherit networks network;
inherit nixpkgs;
importedPluginNixExprs = map
(expr: import expr)
pluginNixExprs;
pluginOptions = { imports = (lib.foldl (a: e: a ++ e.options) [] importedPluginNixExprs); };
pluginResources = map (e: e.resources) importedPluginNixExprs;
pluginDeploymentConfigExporters = (lib.foldl (a: e: a ++ (e.config_exporters {
inherit pkgs;
inherit (lib) optionalAttrs;
})) [] importedPluginNixExprs);
defaults = network.defaults or [];
# Compute the definitions of the machines.
nodes =
lib.listToAttrs (map (machineName:
let
# Get the configuration of this machine from each network
# expression, attaching _file attributes so the NixOS module
# system can give sensible error messages.
modules =
lib.concatMap (n: lib.optional (lib.hasAttr machineName n)
{ imports = [(lib.getAttr machineName n)]; inherit (n) _file; })
networks;
in
{ name = machineName;
value = evalConfig {
modules =
modules ++
defaults ++
[ deploymentInfoModule ] ++
[ { key = "nixops-stuff";
# Make NixOps's deployment.* options available.
imports = [ ./options.nix ./resource.nix pluginOptions ];
# Provide a default hostname and deployment target equal
# to the attribute name of the machine in the model.
networking.hostName = lib.mkOverride 900 machineName;
deployment.targetHost = lib.mkOverride 900 machineName;
environment.checkConfigurationOptions = lib.mkOverride 900 checkConfigurationOptions;
nixpkgs.system = lib.mkDefault system;
_module.args = { inherit nodes resources uuid deploymentName; name = machineName; };
}
];
};
}
) (lib.attrNames (removeAttrs network [ "network" "defaults" "resources" "require" "nixpkgs" "_file" ])));
# Compute the definitions of the non-machine resources.
resourcesByType = lib.zipAttrs (network.resources or []);
deploymentInfoModule = {
deployment = {
name = deploymentName;
arguments = args;
inherit uuid;
};
};
evalResources = mainModule: _resources:
lib.mapAttrs
(name: defs:
let
# Arguments fed to all modules
moduleArgs = {
_module.args = {
inherit pkgs uuid name resources;
# inherit nodes, essentially
nodes =
lib.mapAttrs
(nodeName: node:
lib.mapAttrs
(key: lib.warn "Resource ${name} accesses nodes.${nodeName}.${key}, which is deprecated. Use the equivalent option instead: nodes.${nodeName}.${newOpt key}.")
info.machines.${nodeName}
// node)
nodes;
};
};
modules = [
moduleArgs
mainModule
deploymentInfoModule
./resource.nix
] ++ defs;
in
builtins.removeAttrs
(lib.evalModules { inherit modules; }).config
["_module"])
_resources;
newOpt = key: {
nixosRelease = "config.system.nixos.release and make sure it is set properly";
publicIPv4 = "config.networking.publicIPv4";
}.${key} or "config.deployment.${key}";
resources = lib.foldl
(a: b: a // (b {
inherit evalResources resourcesByType;
inherit (lib) zipAttrs;
}))
{
sshKeyPairs = evalResources ./ssh-keypair.nix (lib.zipAttrs resourcesByType.sshKeyPairs or []);
commandOutput = evalResources ./command-output.nix (lib.zipAttrs resourcesByType.commandOutput or []);
machines = lib.mapAttrs (n: v: v.config) nodes;
}
pluginResources;
# check if there are duplicate elements in a sorted list
noDups = l:
if lib.length l > 1
then
if (lib.head l) == (lib.head (lib.tail l))
then throw "found resources with duplicate names: ${lib.head l}"
else noDups (lib.tail l)
else true;
# Phase 1: evaluate only the deployment attributes.
info =
let
network' = network;
resources' = resources;
in rec {
machines =
lib.flip lib.mapAttrs nodes (n: v': let v = lib.scrubOptionValue v'; in
lib.foldr (a: b: a // b)
{
inherit (v.config.deployment)
targetEnv
targetPort
targetHost
targetUser
sshOptions
privilegeEscalationCommand
alwaysActivate
owners
keys
hasFastConnection
provisionSSHKey
;
nixosRelease = v.config.system.nixos.release or v.config.system.nixosRelease or (lib.removeSuffix v.config.system.nixosVersionSuffix v.config.system.nixosVersion);
publicIPv4 = v.config.networking.publicIPv4;
}
(map
(f: f v.config)
pluginDeploymentConfigExporters
));
network =
builtins.removeAttrs
(lib.fold (as: bs: as // bs) {} (network'.network or []))
[ "nixpkgs" ] # Not serialisable
;
resources =
let
resource_referenced = list: check: recurse:
lib.any lib.id (map (value: (check value) ||
((lib.isAttrs value) && (!(value ? _type) || recurse)
&& (resource_referenced (lib.attrValues value) check false)))
list);
flatten_resources = resources: lib.flatten ( map lib.attrValues (lib.attrValues resources) );
resource_used = res_set: resource:
resource_referenced
(flatten_resources res_set)
(value: value == resource )
true;
resources_without_defaults = res_class: defaults: res_set:
let
missing = lib.filter (res: !(resource_used (removeAttrs res_set [res_class])
res_set."${res_class}"."${res}"))
(lib.attrNames defaults);
in
res_set // { "${res_class}" = ( removeAttrs res_set."${res_class}" missing ); };
in (removeAttrs resources' [ "machines" ]);
};
# Phase 2: build complete machine configurations.
machines = { names }:
let nodes' = lib.filterAttrs (n: v: lib.elem n names) nodes; in
pkgs.runCommand "nixops-machines"
{ preferLocalBuild = true; }
''
mkdir -p $out
${toString (lib.attrValues (lib.mapAttrs (n: v: ''
ln -s ${v.config.system.build.toplevel} $out/${n}
'') nodes'))}
'';
# Function needed to calculate the nixops arguments. This should work even when arguments
# are not set yet, so we fake arguments to be able to evaluate the require attribute of
# the nixops network expressions.
dummyArgs = f: builtins.listToAttrs (map (a: lib.nameValuePair a false) (builtins.attrNames (builtins.functionArgs f)));
getNixOpsExprs = l: lib.unique (lib.flatten (map getRequires l));
getRequires = f:
let
nixopsExpr = import f;
requires =
if builtins.isFunction nixopsExpr then
((nixopsExpr (dummyArgs nixopsExpr)).require or [])
else
(nixopsExpr.require or []);
in
[ f ] ++ map getRequires requires;
exprToArgs = nixopsExpr: f:
if builtins.isFunction nixopsExpr then
map (a: { "${a}" = builtins.toString f; } ) (builtins.attrNames (builtins.functionArgs nixopsExpr))
else [];
fileToArgs = f:
let
nixopsExpr = import f;
in
if builtins.isFunction nixopsExpr then
map (a: { "${a}" = builtins.toString f; } ) (builtins.attrNames (builtins.functionArgs nixopsExpr))
else [];
getNixOpsArgs = fs: lib.zipAttrs (lib.unique (lib.concatMap fileToArgs (getNixOpsExprs fs)));
nixopsArguments =
if flakeReference == null then getNixOpsArgs networkExprs
else lib.listToAttrs (builtins.map (a: {name = a; value = [ flakeReference ];}) (lib.attrNames (builtins.functionArgs flakeExpr)));
}