forked from tweag/flake-buildkite-pipeline
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathflake.nix
206 lines (185 loc) · 8.41 KB
/
flake.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
{
inputs.nixpkgs-lib.url = "github:nix-community/nixpkgs.lib";
outputs = { self, nixpkgs-lib }@inputs:
let
inherit (nixpkgs-lib) lib;
inherit (lib)
getAttrFromPath collect concatStringsSep mapAttrsRecursiveCond
optionalAttrs optionalString concatMapStringsSep splitString last head
optional optionals escapeShellArg isDerivation isAttrs concatLists id
uniqListExt filterAttrs;
inherit (builtins)
concatMap length filter elemAt listToAttrs attrValues mapAttrs elem;
in {
lib = let
descriptionOrName = description: name:
if isNull description then
if isNull name then "<unknown>" else name
else if isNull name then
description
else
"${description} (${name})";
in rec {
# [Step] -> JSON
mkPipeline = steps: builtins.toJSON { inherit steps; };
build = { buildArgs ? [ ], reproducePath ? null, reproduceRepo ? null
, derivationCache ? null }:
installable:
lib.optional (!isNull derivationCache) ''
if [ ! -f ${escapeShellArg installable} ]; then
echo '--- :arrow_down: Copy the missing derivation from cache'
nix copy --from ${escapeShellArg derivationCache} ${
escapeShellArg installable
}
fi
'' ++ lib.singleton ''
echo '--- :hammer: Realise the derivation'
nix build ${escapeShellArg installable} -L -v ${
concatMapStringsSep " " escapeShellArg buildArgs
}
'' ++ lib.optional (!isNull reproducePath && !isNull reproduceRepo) ''
echo -e "To reproduce this result, run\n\e[1mgit checkout $BUILDKITE_COMMIT; nix build ${reproduceRepo}#${
builtins.concatStringsSep "." reproducePath
}\e[0m"
'';
sign = keys: installable:
[ "echo '--- :black_nib: Sign the paths'" ] ++ map (key:
"nix store sign -k ${escapeShellArg key} -r ${
escapeShellArg installable
}") keys;
push = caches: installable:
[ "echo '--- :arrow_up: Push to binary cache'" ] ++ map (cache:
"nix copy --to ${escapeShellArg cache} ${
escapeShellArg installable
}") caches;
buildSignPush = { buildArgs ? [ ], signWithKeys ? [ ]
, pushToBinaryCaches ? [ ], reproducePath ? null, reproduceRepo ? null
, derivationCache ? null }:
installable:
concatStringsSep "\n" ((build {
inherit buildArgs reproducePath reproduceRepo derivationCache;
} installable)
++ optionals (signWithKeys != [ ]) (sign signWithKeys installable)
++ optionals (pushToBinaryCaches != [ ])
(push pushToBinaryCaches installable));
buildPushCachix = { signWithKeys ? [ ], pushToBinaryCaches ? [ ]
, buildArgs ? [ ], reproducePath ? null, reproduceRepo ? null
, derivationCache ? null }:
installable:
let
push = [ "echo '--- :arrow_up: Push to Cachix'" ]
++ map (cache: "cachix push ${escapeShellArg cache} ./result")
pushToBinaryCaches;
in concatStringsSep "\n" ([
(build { inherit buildArgs reproducePath reproduceRepo; }
installable)
] ++ optional (signWithKeys != [ ]) (sign signWithKeys installable)
++ optionals (pushToBinaryCaches != [ ]) push);
runInEnv = environment: command:
"nix develop ${escapeShellArg environment.drvPath} -c sh -c ${
escapeShellArg command
}";
flakeSteps' = { mkBuildCommands, signWithKeys, pushToBinaryCaches
, buildArgs, systems, commonExtraStepConfig ? { }
, reproduceRepo ? null, derivationCache ? null }:
flake:
let
attrsToList = set:
attrValues (mapAttrs (name: value: { inherit name value; }) set);
getAttrs' = names: filterAttrs (name: _: elem name names);
mapOverBuildables = f: buildables:
map f (concatMap attrsToList
(attrValues (getAttrs' systems buildables)));
buildOutput = f: output:
mapOverBuildables (pkg:
let v = f pkg;
in ({
commands = mkBuildCommands {
signWithKeys = if v.signPush then signWithKeys else [ ];
pushToBinaryCaches =
if v.signPush then pushToBinaryCaches else [ ];
inherit buildArgs;
reproducePath = v.pkg.meta.reproducePath or null;
inherit reproduceRepo derivationCache;
} (v.pkg).drvPath;
label = v.label;
artifact_paths = pkg.value.passthru.artifacts or [ ];
key = builtins.concatStringsSep "_"
v.pkg.meta.reproducePath or [ v.pkg.name ];
}) // {
__drv = v.pkg;
} // commonExtraStepConfig) output;
injectAttrPath = ap:
mapAttrs (system:
mapAttrs (name: value:
lib.recursiveUpdate value {
meta.reproducePath = ap ++ [ system name ];
}));
getCompatibleOutput = name: oldDefaultName:
lib.recursiveUpdate
(injectAttrPath [ name ] (flake.${name} or { })) (mapAttrs
(system: pkg: {
default = lib.recursiveUpdate pkg {
meta.reproducePath = [ oldDefaultName system ];
};
}) (flake.${oldDefaultName} or { }));
systemMsg = optionalString (length systems > 1);
packages = buildOutput ({ name, value }: {
label = ":nix: Build ${
descriptionOrName (value.meta.description or null)
(if name == "default" then value.name else name)
}${systemMsg " for ${value.system}"}";
pkg = value;
signPush = value.passthru.cache or true;
}) (getCompatibleOutput "packages" "defaultPackage");
checks = buildOutput ({ name, value }: {
label = ":nix: Check ${
descriptionOrName (value.meta.checkDescription or null) name
}${systemMsg " on ${value.system}"}";
pkg = value;
signPush = false;
}) (injectAttrPath [ "checks" ] flake.checks or { });
devShells = buildOutput ({ name, value }: {
label = ":nix: Build ${name} development environment${
systemMsg " for ${value.system}"
}";
pkg = value.inputDerivation // { inherit (value) meta; };
signPush = true;
}) (getCompatibleOutput "devShells" "devShell");
# TODO: apps?
uniqueSteps = uniqListExt {
inputList = packages ++ checks ++ devShells;
compare = a: b: a.__drv == b.__drv;
};
in map (set: removeAttrs set [ "__drv" ]) uniqueSteps;
flakeSteps = { systems ? [ "x86_64-linux" ], reproduceRepo ? null
, signWithKeys ? [ ], pushToBinaryCaches ? [ ], agents ? [ ]
, buildArgs ? [ ], commonExtraStepConfig ? { }, derivationCache ? null
}:
flakeSteps' {
mkBuildCommands = buildSignPush;
inherit reproduceRepo signWithKeys pushToBinaryCaches buildArgs
systems commonExtraStepConfig derivationCache;
};
flakeStepsCachix = { systems ? [ "x86_64-linux" ], reproduceRepo ? "."
, signWithKeys ? [ ], pushToBinaryCaches ? [ ], agents ? [ ]
, buildArgs ? [ ], commonExtraStepConfig ? { } }:
flakeSteps' {
mkBuildCommands = buildPushCachix;
inherit reproduceRepo pushToBinaryCaches signWithKeys buildArgs
systems commonExtraStepConfig;
};
uploadPipeline = pipeline:
{ derivationCache ? null }:
"buildkite-agent pipeline upload <<< ${
lib.escapeShellArg (__toJSON pipeline)
}${
lib.optionalString (!isNull derivationCache)
" | nix copy --to ${lib.escapeShellArg derivationCache} ${
lib.escapeShellArgs
(__attrNames (__getContext (__toJSON pipeline)))
}"
}";
};
};
}