-
Notifications
You must be signed in to change notification settings - Fork 2
/
lib.nix
333 lines (315 loc) · 10.6 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
322
323
324
325
326
327
328
329
330
331
332
333
{ lib, ... }:
rec {
# converts a list of `mountOption` to a comma-separated string that is passed to the mount unit
toOptionsString =
mountOptions:
builtins.concatStringsSep "," (
map (
option: if option.value == null then option.name else "${option.name}=${option.value}"
) mountOptions
);
# concatenates two paths
# inserts a "/" in between if there is none, removes one if there are two
concatTwoPaths =
parent: child:
with lib.strings;
if hasSuffix "/" parent then
if
hasPrefix "/" child
# "/parent/" "/child"
then
parent + (removePrefix "/" child)
# "/parent/" "child"
else
parent + child
else if
hasPrefix "/" child
# "/parent" "/child"
then
parent + child
# "/parent" "child"
else
parent + "/" + child;
# concatenates a list of paths using `concatTwoPaths`
concatPaths = builtins.foldl' concatTwoPaths "";
# get the parent directory of an absolute path
parentDirectory =
path:
with lib.strings;
assert "/" == (builtins.substring 0 1 path);
let
parts = splitString "/" (removeSuffix "/" path);
len = builtins.length parts;
in
if len < 1 then "/" else concatPaths ([ "/" ] ++ (lib.lists.sublist 0 (len - 1) parts));
# retrieves all directories configured in a `preserveAtSubmodule`
getAllDirectories =
stateConfig:
stateConfig.directories ++ (builtins.concatLists (getUserDirectories stateConfig.users));
# retrieves all files configured in a `preserveAtSubmodule`
getAllFiles =
stateConfig: stateConfig.files ++ (builtins.concatLists (getUserFiles stateConfig.users));
# retrieves the list of directories for all users in a `preserveAtSubmodule`
getUserDirectories = lib.mapAttrsToList (_: userConfig: userConfig.directories);
# retrieves the list of files for all users in a `preserveAtSubmodule`
getUserFiles = lib.mapAttrsToList (_: userConfig: userConfig.files);
# filters a list of files or directories, returns only bindmounts
onlyBindMounts =
forInitrd: builtins.filter (conf: conf.how == "bindmount" && conf.inInitrd == forInitrd);
# filters a list of files or directories, returns only symlinks
onlySymLinks =
forInitrd: builtins.filter (conf: conf.how == "symlink" && conf.inInitrd == forInitrd);
# creates tmpfiles.d rules for the `settings` option of the tmpfiles module from a `preserveAtSubmodule`
mkTmpfilesRules =
forInitrd: preserveAt: stateConfig:
let
allDirectories = getAllDirectories stateConfig;
allFiles = getAllFiles stateConfig;
mountedDirectories = onlyBindMounts forInitrd allDirectories;
mountedFiles = onlyBindMounts forInitrd allFiles;
symlinkedDirectories = onlySymLinks forInitrd allDirectories;
symlinkedFiles = onlySymLinks forInitrd allFiles;
prefix = if forInitrd then "/sysroot" else "/";
mountedDirRules = map (
dirConfig:
let
persistentDirPath = concatPaths [
prefix
stateConfig.persistentStoragePath
dirConfig.directory
];
volatileDirPath = concatPaths [
prefix
dirConfig.directory
];
in
{
# directory on persistent storage
"${persistentDirPath}".d = {
inherit (dirConfig) user group mode;
};
# directory on volatile storage
"${volatileDirPath}".d = {
inherit (dirConfig) user group mode;
};
}
// lib.optionalAttrs dirConfig.configureParent {
# parent directory of directory on persistent storage
"${parentDirectory persistentDirPath}".d = {
inherit (dirConfig.parent) user group mode;
};
# parent directory of symlink on volatile storage
"${parentDirectory volatileDirPath}".d = {
inherit (dirConfig.parent) user group mode;
};
}
) mountedDirectories;
mountedFileRules = map (
fileConfig:
let
persistentFilePath = concatPaths [
prefix
stateConfig.persistentStoragePath
fileConfig.file
];
volatileFilePath = concatPaths [
prefix
fileConfig.file
];
in
{
# file on persistent storage
"${concatPaths [
prefix
stateConfig.persistentStoragePath
fileConfig.file
]}".f = {
inherit (fileConfig) user group mode;
};
# file on volatile storage
"${concatPaths [
prefix
fileConfig.file
]}".f = {
inherit (fileConfig) user group mode;
};
}
// lib.optionalAttrs fileConfig.configureParent {
# parent directory of file on persistent storage
"${parentDirectory persistentFilePath}".d = {
inherit (fileConfig.parent) user group mode;
};
# parent directory of symlink on volatile storage
"${parentDirectory volatileFilePath}".d = {
inherit (fileConfig.parent) user group mode;
};
}
) mountedFiles;
symlinkedDirRules = map (
dirConfig:
let
persistentDirPath = concatPaths [
prefix
stateConfig.persistentStoragePath
dirConfig.directory
];
volatileDirPath = concatPaths [
prefix
dirConfig.directory
];
in
{
# symlink on volatile storage
"${volatileDirPath}".L = {
inherit (dirConfig) user group mode;
argument = concatPaths [
stateConfig.persistentStoragePath
dirConfig.directory
];
};
}
// lib.optionalAttrs dirConfig.createLinkTarget {
# directory on persistent storage
"${persistentDirPath}".d = {
inherit (dirConfig) user group mode;
};
}
// lib.optionalAttrs dirConfig.configureParent {
# parent directory of directory on persistent storage
"${parentDirectory persistentDirPath}".d = {
inherit (dirConfig.parent) user group mode;
};
# parent directory of symlink on volatile storage
"${parentDirectory volatileDirPath}".d = {
inherit (dirConfig.parent) user group mode;
};
}
) symlinkedDirectories;
symlinkedFileRules = map (
fileConfig:
let
persistentFilePath = concatPaths [
prefix
stateConfig.persistentStoragePath
fileConfig.file
];
volatileFilePath = concatPaths [
prefix
fileConfig.file
];
in
{
# symlink on volatile storage
"${volatileFilePath}".L = {
inherit (fileConfig) user group mode;
argument = concatPaths [
stateConfig.persistentStoragePath
fileConfig.file
];
};
}
// lib.optionalAttrs fileConfig.createLinkTarget {
# file on persistent storage
"${persistentFilePath}".f = {
inherit (fileConfig) user group mode;
};
}
// lib.optionalAttrs fileConfig.configureParent {
# parent directory of file on persistent storage
"${parentDirectory persistentFilePath}".d = {
inherit (fileConfig.parent) user group mode;
};
# parent directory of symlink on volatile storage
"${parentDirectory volatileFilePath}".d = {
inherit (fileConfig.parent) user group mode;
};
}
) symlinkedFiles;
rules = mountedDirRules ++ symlinkedDirRules ++ mountedFileRules ++ symlinkedFileRules;
in
rules;
# creates systemd mount unit configurations from a `preserveAtSubmodule`
mkMountUnits =
forInitrd: preserveAt: stateConfig:
let
allDirectories = getAllDirectories stateConfig;
allFiles = getAllFiles stateConfig;
mountedDirectories = onlyBindMounts forInitrd allDirectories;
mountedFiles = onlyBindMounts forInitrd allFiles;
prefix = if forInitrd then "/sysroot" else "/";
directoryMounts = map (directoryConfig: {
options = toOptionsString (
directoryConfig.mountOptions
++ (lib.optional forInitrd {
name = "x-initrd.mount";
value = null;
})
);
where = concatPaths [
prefix
directoryConfig.directory
];
what = concatPaths [
prefix
stateConfig.persistentStoragePath
directoryConfig.directory
];
unitConfig.DefaultDependencies = "no";
conflicts = [ "umount.target" ];
wantedBy = if forInitrd then [
"initrd-preservation.target"
] else [
"preservation.target"
];
before = if forInitrd then [
# directory mounts are set up before tmpfiles
"systemd-tmpfiles-setup-sysroot.service"
"initrd-preservation.target"
] else [
"systemd-tmpfiles-setup.service"
"preservation.target"
];
}) mountedDirectories;
fileMounts = map (fileConfig: {
options = toOptionsString (
fileConfig.mountOptions
++ (lib.optional forInitrd {
name = "x-initrd.mount";
value = null;
})
);
where = concatPaths [
prefix
fileConfig.file
];
what = concatPaths [
prefix
stateConfig.persistentStoragePath
fileConfig.file
];
unitConfig = {
DefaultDependencies = "no";
ConditionPathExists = concatPaths [
prefix
stateConfig.persistentStoragePath
fileConfig.file
];
};
conflicts = [ "umount.target" ];
after =
if forInitrd then
[ "systemd-tmpfiles-setup-sysroot.service" ]
else
[ "systemd-tmpfiles-setup.service" ];
wantedBy = if forInitrd then [ "initrd-preservation.target" ] else [ "preservation.target" ];
before = if forInitrd then [ "initrd-preservation.target" ] else [ "preservation.target" ];
}) mountedFiles;
mountUnits = directoryMounts ++ fileMounts;
in
mountUnits;
# aliases to avoid the use of a nameless bool outside this lib
mkRegularMountUnits = mkMountUnits false;
mkInitrdMountUnits = mkMountUnits true;
mkRegularTmpfilesRules = mkTmpfilesRules false;
mkInitrdTmpfilesRules = mkTmpfilesRules true;
}