-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroup.nix
109 lines (108 loc) · 3.43 KB
/
group.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
{
POP,
lib,
}: {
new = {
groupName ? "",
groups ? ["a" "b" "c"],
instancePrefix ? "instance",
ips ? [""],
pkgs,
prefix,
start ? 0,
stateVersion,
system,
}: let
end = start + builtins.length ips - 1;
groupName' =
if groupName == ""
then ""
else "${groupName}-";
fullGroupName = assert (builtins.match ".+-" prefix != null);
if groupName == ""
then builtins.substring 0 (builtins.stringLength prefix - 1) prefix
else prefix + groupName;
instanceNumber = n: let
n' = assert (n < 100); builtins.toString n;
in
if builtins.stringLength n' < 2
then "0${n'}"
else n';
instanceName = n: instancePrefix + instanceNumber n;
generateInstancesWithValues = values: f:
builtins.listToAttrs (builtins.concatLists (lib.lists.imap0 (i: v: let
instanceName' = instanceName i;
in [
(
lib.attrsets.nameValuePair
(groupName'
+ instanceName')
(f instanceName' i v)
)
])
values));
generateInstancesWithRange = start: end: f:
builtins.listToAttrs (builtins.concatMap (n: let
instanceName' = instanceName n;
in [
(
lib.attrsets.nameValuePair
(groupName'
+ instanceName')
(f instanceName')
)
])
(lib.lists.range start end));
generateInstancesFor = blockType: start: end: {inputs, ...}:
generateInstancesWithRange start end (_: {
imports = [
inputs.cells.group.${blockType}.${fullGroupName}
];
});
generateInstancesForColmenaConfigurations = ips: {cell, ...}:
generateInstancesWithValues ips
(instanceName: n: ip: let
group = builtins.elemAt groups (lib.trivial.mod n (builtins.length groups));
in {
deployment = {
targetHost = ip;
tags = ["${fullGroupName}-group-${group}"] ++ lib.lists.optional ("${fullGroupName}-" != prefix) "${prefix}group-${group}";
};
imports = [
cell.nixosConfigurations."${groupName'}${instanceName}"
];
});
generateInstancesForNixosConfigurations = start: end: bee: system: {
inputs,
cell,
...
}:
generateInstancesWithRange start end (instanceName: {
inherit bee;
inherit system;
imports = let
disko = assert (inputs ? disko); inputs.disko.nixosModules.disko;
host = inputs.cells.host.nixosProfiles."${fullGroupName}-${instanceName}" or {};
in [
cell.diskoConfigurations."${groupName'}${instanceName}"
cell.hardwareProfiles."${groupName'}${instanceName}"
cell.nixosModules."${groupName'}${instanceName}"
cell.nixosProfiles."${groupName'}${instanceName}"
disko
host
];
});
in
POP.lib.kPop {
inherit end;
colmenaConfigurations = generateInstancesForColmenaConfigurations ips;
diskoConfigurations = generateInstancesFor "diskoConfigurations" start end;
hardwareProfiles = generateInstancesFor "hardwareProfiles" start end;
nixosModules = generateInstancesFor "nixosModules" start end;
nixosProfiles = generateInstancesFor "nixosProfiles" start end;
nixosConfigurations = generateInstancesForNixosConfigurations start end {
inherit pkgs;
inherit system;
} {inherit stateVersion;};
};
}