/
lib.nix
379 lines (329 loc) · 11.5 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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
self:
let
inherit (self.inputs.nixpkgs) lib;
inherit (self.inputs) data-merge;
in
rec {
actions = {
apply = part: base:
base // part // {
inputs =
base.inputs or { } //
part.inputs or { };
output = inputs:
lib.recursiveUpdate
(base.output or (_: { }) inputs)
(part.output or (_: { }) inputs);
};
behavior = {
onUpdate = action: behavior.once action.id action;
# Same as `stopOnSuccess` and `stopOnFailure` combined
# but more efficient (just one input so only one DB query).
once = key: _: next: actions.apply
{
inputs."behavior: run only once for \"${key}\"" = {
not = true;
match = ''
"_behavior": once: "${key}": _
'';
};
output = _:
let fact._behavior.once.${key} = null; in
{ success = fact; } //
lib.optionalAttrs (next ? job) {
failure = fact;
};
}
next;
stopOnSuccess = key: _: actions.apply {
inputs."behavior: stop on success for \"${key}\"" = {
not = true;
match = ''
"_behavior": stopOnSuccess: "${key}": _
'';
};
output = _: {
success._behavior.stopOnSuccess.${key} = null;
};
};
stopOnFailure = key: _: actions.apply {
inputs."behavior: stop on failure for \"${key}\"" = {
not = true;
match = ''
"_behavior": stopOnFailure: "${key}": _
'';
};
output = _: {
failure._behavior.stopOnFailure.${key} = null;
};
};
onInputChange = input: key: _: next: actions.apply
{
inputs."behavior: input \"${input}\" changed for \"${key}\"" = {
not = true;
match = ''
"_behavior": onInputChange: "${key}": "${input}": _inputs."${input}".id
'';
};
output = inputs:
let fact._behavior.onInputChange.${key}.${input} = inputs.${input}.id; in
{ success = fact; } //
lib.optionalAttrs (next ? job) {
failure = fact;
};
}
next;
# TODO add onAnyInputChange
};
};
jobs = {
escapeNames = from: to: job:
let
escape = builtins.replaceStrings
(from ++ [ "/" ])
(to ++ [ "-" ]);
in lib.mapAttrs' (k: v: lib.nameValuePair (escape k) (v // {
group = builtins.mapAttrs (k: v: v // {
task = lib.mapAttrs' (k: lib.nameValuePair (escape k)) v.task or { };
}) v.group or { };
})) job;
singleTask = name: task: { ${name}.group.${name}.task.${name} = task; };
};
tasks.script = language: script:
let
runner = "run-${language}";
scriptName = builtins.hashString "md5" script;
in {
config = {
packages = [
"github:input-output-hk/cicero/${self.rev or ""}#${runner}"
];
command = [
# It is ok to hard-code the system here
# because we only care about the derivation name.
"/bin/${self.outputs.legacyPackages.x86_64-linux.${runner}.name}"
"/local/scripts/${language}/${scriptName}"
];
};
template = [{
destination = "local/scripts/${language}/${scriptName}";
left_delimiter = "";
right_delimiter = "";
data = script;
}];
};
/* Chains are a concise way to write jobs.
Put simply chains are a fold-right of wrappers.
Each "step/link/part" (no name defined) of a chain
is a function that takes the action that this job
is being defined for as its first argument
and the next "step/link/part" as its second argument.
These argument are supplied automatically when called
by the `chain` function.
It returns the job as an attribute set.
Most "steps/links/parts" are created from a builder function
that takes some specific arguments which are usually given
directly in the action definition.
This simple contract sometimes allows to use other functions
that are not primarily meant to be used in a chain, or use
functions that are meant for chains by themselves.
*/
chains = {
/* The main entrypoint to chains.
For simplicity, plain attribute sets
are also allowed. They will simply be merged
with the next (if any) "step/link/part"
using `data-merge.merge`.
*/
chain = action: steps:
lib.foldr (a: b:
if builtins.typeOf a == "set"
then data-merge.merge b a
else (a action) b
) { } steps;
jobs = {
escapeNames = from: to: actions: jobs.escapeNames from to;
singleTask = { name, ... }: jobs.singleTask name;
};
tasks = {
/* Like `tasks.script` but the second argument is
a function that takes the command of the
next script and returns the new script.
Example:
```nix
wrapScript "bash" (inner: ''
echo 'Running …'
time ${lib.escapeShellArgs inner}
echo '… finished.'
'')
```
*/
wrapScript = language: outerFn: action: inner:
let outer = script language (outerFn inner.config.command or [ ]); in
data-merge.merge
(lib.recursiveUpdate inner {
config.command = outer.config.command;
# XXX we have to pre-create these keys because they may not be present
# see https://github.com/divnix/data-merge/issues/1
config.packages = inner.config.packages or [ ];
template = inner.template or [ ];
})
{
config.packages = data-merge.append outer.config.packages;
template = data-merge.append outer.template;
};
networking = {
nameservers = nameservers: action: next:
data-merge.merge
(next // {
# XXX we have to pre-create `template` because it may not be present
# see https://github.com/divnix/data-merge/issues/1
template = next.template or [ ];
})
{
template = data-merge.append [{
destination = "/etc/resolv.conf";
left_delimiter = "";
right_delimiter = "";
data = lib.concatStringsSep "\n" (map (ns: "nameserver " + ns) nameservers);
}];
};
addNameservers = nameservers:
wrapScript "bash" (next: ''
mkdir -p /etc
for nameserver in ${lib.escapeShellArgs nameservers}; do
echo "nameserver $nameserver" >> /etc/resolv.conf
done
${lib.escapeShellArgs next}
'');
};
nix = {
install = action: next:
data-merge.merge
(lib.recursiveUpdate next {
# XXX we have to pre-create `config.packages` because it may not be present
# see https://github.com/divnix/data-merge/issues/1
config.packages = next.config.packages or [ ];
})
{
config.packages = data-merge.append [
"github:input-output-hk/nomad-driver-nix/wrap-nix#wrap-nix"
];
env.NIX_CONFIG = ''
experimental-features = nix-command flakes
${next.env.NIX_CONFIG or ""}
'';
};
develop = action: next:
nix.install action (
wrapScript "bash" (next: ''
nix --extra-experimental-features 'nix-command flakes' \
develop -c ${lib.escapeShellArgs next}
'') action next
);
build = action: next:
nix.install action (
wrapScript "bash" (next: ''
if [[ -f flake.nix ]]; then
nix build
else
nix-build
fi
${lib.escapeShellArgs next}
'') action next
);
};
makes = target: action: next:
data-merge.merge
(wrapScript "bash" (next: ''
m ${lib.escapeShellArg target}
${lib.escapeShellArgs next}
'') action next)
{
config.packages = data-merge.append [ "github:fluidattacks/makes" ];
};
git.clone = { clone_url, sha, ... }:
action: next:
data-merge.merge
(wrapScript "bash" (next: ''
export SSL_CERT_FILE=/current-profile/etc/ssl/certs/ca-bundle.crt
export HOME="$PWD/.home"
mkdir -p "$HOME"
git config --global advice.detachedHead false
git clone --quiet ${lib.escapeShellArg clone_url} src
cd src
git checkout ${lib.escapeShellArg sha}
${lib.escapeShellArgs next}
'') action next)
{
config.packages = data-merge.append [
"github:NixOS/nixpkgs/${self.inputs.nixpkgs.rev}#gitMinimal"
"github:NixOS/nixpkgs/${self.inputs.nixpkgs.rev}#cacert"
];
};
github.reportStatus = statuses_url: action: next:
data-merge.merge
(wrapScript "bash"
(inner: ''
export SSL_CERT_FILE=/current-profile/etc/ssl/certs/ca-bundle.crt
# TODO Only get from vault. Env var is just for development.
if [[ -z "''${GITHUB_TOKEN:-}" ]]; then
GITHUB_TOKEN=$(vault kv get -field=token kv/data/cicero/github)
fi
function cleanup {
rm -f "$secret_headers"
}
trap cleanup EXIT
secret_headers="$(mktemp)"
cat >> "$secret_headers" <<EOF
Authorization: token $GITHUB_TOKEN
EOF
function report {
jq -nc '{
state: $state,
context: $action_name,
description: $description,
target_url: "\(env.CICERO_WEB_URL)/action/\($action_id)",
}' \
--arg state "$1" \
--arg description "Run $NOMAD_JOB_ID" \
--arg action_id ${lib.escapeShellArg action.id} \
--arg action_name ${lib.escapeShellArg action.name} \
| curl ${lib.escapeShellArg statuses_url} \
> /dev/null --no-progress-meter \
-H 'Accept: application/vnd.github.v3+json' \
-H @"$secret_headers" \
--data-binary @-
}
function err {
report error
}
trap err ERR
report pending
if ${lib.escapeShellArgs inner}; then
report success
else
status=$?
report failure
exit $status
fi
'')
action
next)
{
config.packages = data-merge.append [
"github:NixOS/nixpkgs/${self.inputs.nixpkgs.rev}#curl"
"github:NixOS/nixpkgs/${self.inputs.nixpkgs.rev}#jq"
"github:NixOS/nixpkgs/${self.inputs.nixpkgs.rev}#vault"
"github:NixOS/nixpkgs/${self.inputs.nixpkgs.rev}#cacert"
];
};
};
};
inherit (actions) behavior;
inherit (tasks) script;
inherit (chains) chain;
inherit (chains.jobs) escapeNames singleTask;
inherit (chains.tasks) wrapScript networking nix makes git github;
inherit data-merge;
}