/
lib.nix
274 lines (233 loc) · 8.35 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
self:
let
inherit (self.inputs.nixpkgs) lib;
inherit (self.inputs) data-merge;
in
rec {
workflows = rec {
/*
Convenience for defining and wrapping a workflow.
Takes an attribute set of "spec workflow" arguments
as its first argument but drops any unknown keys.
The second argument is a "std workflow".
This allows access to any possible extra args before
calling `mkWorkflow` with the "spec workflow" args.
Useful if you get std itself passed as an extra arg,
as you need to access std to call `mkWorkflow`.
*/
callWorkflow = { name, id, ... } @ args:
lib.flip mkWorkflow { inherit name id; };
/*
Takes a "std workflow" and returns a "spec workflow".
The "spec workflow" is a function that takes a set
`{ name, id }` as its only argument and returns
the final JSON that the cicero evaluator parses.
The "std workflow" is the set or function
which layout is specific to `mkWorkflow`.
It is similar to a "spec workflow".
I'll try to briefly visualize it here:
```nix
[workflow:] {
actions = [actions:] {
<name> = [action:] { factFoo ? null }: {
when.conditionFoo = factFoo != null;
job = …;
};
};
}
`job` is the JSON representation a Nomad job's HCL or
a list that is passed to the `chain` function (see its docs).
The arguments in brackets are optional,
meaning that either a function with a single argument
or an attribute set is allowed in their place.
If the function form is used, that first argument
is the result of the function itself for recursive access
with specific extra keys added:
- workflow: { name, id, <self keys…> }
- actions: { workflow, <self keys…> }
- action: { actions, <self keys…> }
This allows access to the parent structure
from any place in the hierarchy.
```
*/
mkWorkflow = wf: { name, id } @ wfInfo: let
wfFn =
if builtins.typeOf wf == "set"
then (_: wf)
else wf;
initWf = wfFn (wfFn wfInfo // wfInfo);
in initWf // {
actions = let
actionsFn =
if builtins.typeOf initWf.actions == "set"
then (_: initWf.actions)
else initWf.actions;
actionsInfo = { workflow = initWf // wfInfo; };
initActions = actionsFn (actionsFn actionsInfo // actionsInfo);
in mkActions initActions actionsInfo;
};
mkActions = actions: actionsInfo:
builtins.mapAttrs (mkAction (actions // actionsInfo)) actions;
mkAction = actions: name: action: let
# actionRaw = { fact ? null }: { when = …; job = …; }
actionRaw =
if builtins.typeOf (action {}) == "set"
then action
else (action {});
actionRawArgs = builtins.functionArgs actionRaw;
# actionFn = self: { fact ? null }: { when = …; job = …; }
actionFn =
if builtins.typeOf (action {}) == "set"
then (_: action)
else action;
info = { inherit name actions; };
actionFnArg = actionFn info {} // info;
initAction = actionFn actionFnArg;
wrapper = facts:
let result = initAction facts; in
result // {
job =
if builtins.typeOf result.job == "list"
then chain actionFnArg result.job
else result.job;
};
in
# We need to `setFunctionArgs` because that information is lost
# as the `wrapper` just takes `facts:`, leading to no facts
# being passed to this action as it appears like it takes none.
lib.setFunctionArgs wrapper actionRawArgs;
};
jobs.singleTask = { name, ... }: task: {
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 some specific arguments
(usually supplied directly in the workflow),
the action that this job is being defined for
(usually supplied via usage of the `chain` function, not directly),
and the next "step/link/part" (also through the `chain` function).
It returns the job as an attribute set.
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;
tasks = {
/*
Like `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: inner: let
outer = script language (outerFn inner.config.command);
in
data-merge.merge (lib.recursiveUpdate inner {
config.command = outer.config.command;
}) {
config.packages = data-merge.append outer.config.packages;
template = data-merge.append outer.template;
};
git.clone = { repo, sha, ... }: action: next:
data-merge.merge
(wrapScript "bash" (next: ''
set -exuo pipefail
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 repo.clone_url} src
cd src
git checkout ${lib.escapeShellArg sha}
${lib.escapeShellArgs next}
'') next)
{
config.packages = data-merge.append [
"github:NixOS/nixpkgs/${self.inputs.nixpkgs.rev or "nixpkgs-unstable"}#gitMinimal"
"github:NixOS/nixpkgs/${self.inputs.nixpkgs.rev or "nixpkgs-unstable"}#cacert"
];
};
github.reportStatus = statuses_url: action: next:
data-merge.merge
(wrapScript "bash" (inner: ''
set -euxo pipefail
function report {
cicero-std github status ${lib.escapeShellArg statuses_url} \
--arg state "$1" \
--arg description 'Workflow #'${lib.escapeShellArg action.actions.workflow.id} \
--arg workflow_id ${lib.escapeShellArg action.actions.workflow.id} \
--arg workflow_name ${lib.escapeShellArg action.actions.workflow.name} \
--arg action_name ${lib.escapeShellArg action.name}
}
function err {
report error
}
trap err ERR
report pending
if ${lib.escapeShellArgs inner}; then
report success
else
$status=$?
report failure
exit $status
fi
'') next)
{
config.packages = data-merge.append [
"github:input-output-hk/cicero/${self.rev or ""}#cicero-std"
];
};
};
};
inherit (workflows) callWorkflow mkWorkflow;
inherit (jobs) singleTask;
inherit (tasks) script;
inherit (chains) chain;
inherit (chains.tasks) wrapScript git github;
inherit data-merge;
}