-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathlib.nix
More file actions
245 lines (221 loc) · 7.42 KB
/
Copy pathlib.nix
File metadata and controls
245 lines (221 loc) · 7.42 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
{
lib,
pkgs,
}: let
inherit (builtins) elem isList match toJSON;
inherit (lib.attrsets) filterAttrs;
inherit (lib.lists) toList;
inherit (lib.modules) mkDefault mkDerivedConfig mkIf mkMerge;
inherit (lib.options) literalExpression mkEnableOption mkOption;
inherit (lib.strings) concatMapStringsSep hasPrefix;
inherit (lib.types) addCheck anything attrsOf bool coercedTo either enum functionTo int ints lines listOf nullOr oneOf path str submodule uniq;
in rec {
addCheckForTypes = {
baseType,
allowedFileTypes,
isRequiredForFileTypes,
config,
name,
valName,
}:
addCheck baseType (val: let
nonNull = val != null;
permitsSource = elem config.type allowedFileTypes;
fileTypeStrings = toJSON allowedFileTypes;
in
if nonNull && !permitsSource
then throw "'${name}.${valName}' is set, but '${valName}' is only permitted for the following types: ${fileTypeStrings}"
else if !nonNull && permitsSource && isRequiredForFileTypes
then throw "'${name}.${valName}' is required for the following types: ${fileTypeStrings}"
else true);
# inlined from https://github.com/NixOS/nixpkgs/tree/master/nixos/modules/config/shells-environment.nix
# using osOptions precludes using hjem (or this type) standalone
envVarType = attrsOf (nullOr (oneOf [(listOf (oneOf [int str path])) int str path]));
listOrSingletonOf = type: coercedTo (either (listOf type) type) toList (listOf type);
fileToJson = f:
filterAttrs (_: v: v != null) {
inherit
(f)
clobber
gid
permissions
source
target
type
uid
;
};
fileTypeRelativeTo = {
rootDir,
clobberDefault,
clobberDefaultText,
}:
submodule ({
name,
config,
options,
...
}: {
options = let
fileAttrType = baseType: valName:
addCheckForTypes {
inherit config name baseType valName;
allowedFileTypes = ["copy" "delete" "directory" "modify"];
isRequiredForFileTypes = false;
};
sourceType = {
baseType,
isRequiredForFileTypes,
valName,
}:
addCheckForTypes {
inherit config name baseType isRequiredForFileTypes valName;
allowedFileTypes = ["copy" "symlink"];
};
in {
enable =
mkEnableOption "creation of this file"
// {
default = true;
example = false;
};
type = mkOption {
type = enum [
"symlink"
"copy"
"delete"
"directory"
"modify"
];
default = "symlink";
description = ''
Type of path to create.
'';
};
target = mkOption {
type = str;
apply = p:
if hasPrefix "/" p
then throw "This option cannot handle absolute paths yet!"
else "${config.relativeTo}/${p}";
defaultText = name;
description = ''
Path to target file relative to `${rootDir}`.
'';
};
text = mkOption {
default = null;
type = sourceType {
baseType = nullOr lines;
valName = "text";
isRequiredForFileTypes = false;
};
description = "Text of the file.";
};
source = mkOption {
type = sourceType {
baseType = nullOr path;
valName = "source";
isRequiredForFileTypes = true;
};
default = null;
description = "Path of the source file or directory.";
};
permissions = mkOption {
type = addCheck (fileAttrType (nullOr str) "permissions") (val: val != null -> match "[0-7]{3,4}" val == []);
default = null;
description = "Permissions (in octal) to set on the target path.";
};
uid = mkOption {
# UIDs and GIDs are unsigned 32-bit ints
# https://serverfault.com/a/105265
type = fileAttrType (nullOr ints.u32) "uid";
default = null;
description = "User ID to set as owner on the target path.";
};
gid = mkOption {
type = fileAttrType (nullOr ints.u32) "gid";
default = null;
description = "Group ID to set as owner on the target path.";
};
generator = mkOption {
# functionTo doesn't actually check the return type, so do that ourselves
type = addCheck (uniq (nullOr (functionTo (either options.source.type options.text.type)))) (x: let
generatedValue = x config.value;
generatesDrv = options.source.type.check generatedValue;
generatesStr = options.text.type.check generatedValue;
in
x != null -> (generatesDrv || generatesStr));
default = null;
description = ''
Function that when applied to `value` will create the `source` or `text` of the file.
Detection is automatic, as we check if the `generator` generates a derivation or a string after applying to `value`.
'';
example = literalExpression "lib.generators.toGitINI";
};
value = mkOption {
type = nullOr (attrsOf anything);
default = null;
description = "Value passed to the `generator`.";
example = {
user.email = "me@example.com";
};
};
executable = mkOption {
type = bool;
default = false;
example = true;
description = ''
Whether to set the execute bit on the target file.
'';
};
clobber = mkOption {
type = bool;
default = clobberDefault;
defaultText = clobberDefaultText;
description = ''
Whether to "clobber" existing target paths.
- If using the **systemd-tmpfiles** hook (Linux only), tmpfile rules
will be constructed with `L+` (*re*create) instead of `L`
(create) type while this is set to `true`.
'';
};
relativeTo = mkOption {
internal = true;
type = path;
default = rootDir;
description = "Path that symlinks are relative to.";
apply = x:
assert (hasPrefix "/" x || abort "Relative path ${x} cannot be used for files.<path>.relativeTo"); x;
};
};
config = let
generatedValue = config.generator config.value;
hasGenerator = config.generator != null;
generatesDrv = options.source.type.check generatedValue;
generatesStr = options.text.type.check generatedValue;
in
mkMerge [
{
# for docs
_module.args.name = mkDefault (literalExpression "‹path›");
target = mkDefault name;
source = mkIf (config.text != null) (mkDerivedConfig options.text (text:
pkgs.writeTextFile {
inherit name text;
inherit (config) executable;
}));
}
(lib.mkIf (hasGenerator && generatesDrv) {
source = mkDefault generatedValue;
})
(lib.mkIf (hasGenerator && generatesStr) {
text = mkDefault generatedValue;
})
];
});
toEnv = env:
if isList env
then concatMapStringsSep ":" toString env
else toString env;
}