-
Notifications
You must be signed in to change notification settings - Fork 1
/
default.nix
130 lines (115 loc) · 3.71 KB
/
default.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
{ confDir, coreLib, corePkgs }:
with coreLib;
let
machine = import ./machine { inherit confDir corePkgs coreLib findMetaConfig; };
findMetaConfig =
{ cluster, name }:
cluster.${name};
makeMachine =
{ name, metaConfig, carrier ? null, alias ? null, clusterName ? null, extraModules ? [], buildAttribute ? null }:
let
ensuredClusterName = if isNull clusterName then name else clusterName;
in {
inherit name alias metaConfig carrier extraModules;
clusterName = ensuredClusterName;
build = {
attribute = if isNull buildAttribute then metaConfig.buildAttribute else buildAttribute;
toplevel = buildConfig { name = ensuredClusterName; inherit metaConfig; };
};
};
buildConfig =
{ name, metaConfig }:
if !metaConfig.managed then
null
else if metaConfig.spin == "nixos" then
machine.nixos { inherit name metaConfig; }
else if metaConfig.spin == "vpsadminos" then
machine.vpsadminos { inherit name metaConfig; }
else
null;
expandCarriers = machineAttrs: flatten (mapAttrsToList (name: m:
if m.metaConfig.carrier.enable then
[ m ] ++ (expandCarrier machineAttrs m)
else
m
) machineAttrs);
expandCarrier = machineAttrs: carrierMachine: map (cm:
makeMachine {
name = "${carrierMachine.name}#${if isNull cm.alias then cm.machine else cm.alias}";
alias = cm.alias;
clusterName = cm.machine;
carrier = carrierMachine.name;
extraModules = cm.extraModules;
buildAttribute = cm.buildAttribute;
metaConfig = coreLib.updateManyAttrsByPath ([
{
path = [ "labels" ];
update = old: old // cm.labels;
}
{
path = [ "tags" ];
update = old: old ++ cm.tags;
}
] ++ (generationUpdates cm)) machineAttrs.${cm.machine}.metaConfig;
}
) carrierMachine.metaConfig.carrier.machines;
generationUpdates = cm:
flatten (map (generations:
map (attr: {
path = [ generations attr ];
update = old:
let v = cm.${generations}.${attr};
in if isNull v then old else v;
}) [ "min" "max" "maxAge" ]
) [ "buildGenerations" "hostGenerations" ]);
in rec {
inherit corePkgs coreLib;
mkNetUdevRule = name: mac: ''
ACTION=="add", SUBSYSTEM=="net", DRIVERS=="?*", KERNEL=="eth*", ATTR{address}=="${mac}", NAME="${name}"
'';
mkNetUdevRules = rs: concatStringsSep "\n" (mapAttrsToList (name: mac:
mkNetUdevRule name mac
) rs);
inherit findMetaConfig;
# Return all configured machines in a list
getClusterMachines = cluster:
let
machineAttrs = mapAttrs (name: metaConfig:
makeMachine { inherit name metaConfig; }
) cluster;
in expandCarriers machineAttrs;
# Get IP version addresses from all machines in a cluster
getAllAddressesOf = cluster: v:
let
machines = getClusterMachines cluster;
addresses = flatten (map (machine:
map (addr: machine // addr) machine.metaConfig.addresses.${"v${toString v}"}
) machines);
in addresses;
mkOptions = {
addresses = v:
{ config, ... }:
{
options = {
address = mkOption {
type = types.str;
description = "IPv${toString v} address";
};
prefix = mkOption {
type = types.ints.positive;
description = "Prefix length";
};
string = mkOption {
type = types.nullOr types.str;
default = null;
apply = v:
if isNull v then
"${config.address}/${toString config.prefix}"
else
v;
description = "Address with prefix as string";
};
};
};
};
}