forked from oxidecomputer/omicron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flake.nix
438 lines (398 loc) · 12.8 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
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
{
description = "Development environment for Omicron";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs = { nixpkgs, rust-overlay, ... }:
let
overlays = [ (import rust-overlay) ];
pkgs = import nixpkgs {
inherit overlays;
system = "x86_64-linux";
};
# use the Rust toolchain defined in the `rust-toolchain.toml` file.
rustToolchain = (pkgs.pkgsBuildHost.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml).override {
extensions = [
"rust-src" # for rust-analyzer
];
};
buildInputs = with pkgs; [
# libs
openssl
postgresql
xmlsec
sqlite
libclang
libxml2
libtool
];
nativeBuildInputs = with pkgs; [
rustToolchain
cmake
stdenv
pkg-config
];
openAPIVersion = with pkgs.lib; path:
let
file = strings.fileContents path;
parts = strings.splitString "\n" file;
extractHash = prefix: (line: trivial.pipe line [
(elemAt parts)
(strings.removeSuffix "\"")
(strings.removePrefix "${prefix}=\"")
]);
in
{
commit = extractHash "COMMIT" 0;
sha = extractHash "SHA2" 1;
};
downloadBuildomat =
let baseURL = "https://buildomat.eng.oxide.computer/public/file/oxidecomputer";
in { kind, repo, file, commit, sha }:
builtins.fetchurl {
url = "${baseURL}/${repo}/${kind}/${commit}/${file}";
sha256 = sha;
};
downloadOpenAPI = { repo, file, version }:
downloadBuildomat
{
inherit repo file;
kind = "openapi";
commit = pkgs.lib.debug.traceValFn
(v: "${file}: commit=${v}")
version.commit;
sha = version.sha;
};
dendriteVersion = openAPIVersion
./tools/dendrite_openapi_version;
mgVersion = openAPIVersion
./tools/maghemite_mg_openapi_version;
dendriteOpenAPI = downloadOpenAPI
{
repo = "dendrite";
file = "dpd.json";
version = dendriteVersion;
};
ddmOpenAPI = downloadOpenAPI
{
repo = "maghemite";
file = "ddm-admin.json";
version = openAPIVersion ./tools/maghemite_ddm_openapi_version;
};
mgOpenAPI = downloadOpenAPI
{
repo = "maghemite";
file = "mg-admin.json";
version = mgVersion;
};
# given a list of strings of the form `PREFIX="SHA256"`, finds the string
# starting with the provided `name` and returns the hash for that prefix.
findSha = with pkgs.lib;
shas: (name:
let
upperName = strings.toUpper name;
prefix = "${upperName}=\"";
in
trivial.pipe shas [
(lists.findFirst (strings.hasPrefix prefix) "")
(strings.removePrefix prefix)
(strings.removeSuffix "\"")
]);
dendrite-stub = with pkgs.lib;
let
commit = dendriteVersion.commit;
repo = "dendrite";
stubShas =
let
file = builtins.readFile
./tools/dendrite_stub_checksums;
in
strings.splitString "\n" file;
findStubSha = name: findSha stubShas "CIDL_SHA256_${name}";
fetchLinuxBin = file:
downloadBuildomat {
inherit commit file repo;
sha = findStubSha "linux_${file}";
kind = "linux-bin";
};
# get stuff
tarball = downloadBuildomat
{
inherit commit repo;
sha = findStubSha "illumos";
kind = "image";
file = "dendrite-stub.tar.gz";
};
swadm = fetchLinuxBin
"swadm";
dpd = fetchLinuxBin
"dpd";
in
with pkgs; stdenv.mkDerivation
{
name = "dendrite-stub";
version = commit;
src = tarball;
nativeBuildInputs = [
# patch the binary to use the right dynamic library paths.
autoPatchelfHook
];
buildInputs = [
glibc
gcc-unwrapped
openssl
];
installPhase =
let
binPath = "root/opt/oxide/dendrite/bin";
in
''
mkdir -p $out/${binPath}
cp -r . $out/root
cp ${swadm} $out/${binPath}/swadm
chmod +x $out/${binPath}/swadm
cp ${dpd} $out/${binPath}/dpd
chmod +x $out/${binPath}/dpd
mkdir -p $out/bin
ln -s $out/${binPath}/swadm $out/bin/swadm
ln -s $out/${binPath}/dpd $out/bin/dpd
'';
};
mgd = with pkgs.lib;
let
commit = mgVersion.commit;
repo = "maghemite";
shas =
let
file = builtins.readFile
./tools/maghemite_mgd_checksums;
in
strings.splitString
"\n"
file;
# get stuff
tarball = downloadBuildomat
{
inherit commit repo;
sha = findSha shas "CIDL_SHA256";
kind = "image";
file = "mgd.tar.gz";
};
linuxBin =
downloadBuildomat
{
inherit commit repo;
sha = findSha shas "MGD_LINUX_SHA256";
kind = "linux";
file = "mgd";
};
in
with pkgs;
stdenv.mkDerivation
{
name = "mgd";
src = tarball;
version = commit;
nativeBuildInputs = [
# patch the binary to use the right dynamic library paths.
autoPatchelfHook
];
buildInputs = [
glibc
gcc-unwrapped
openssl.dev
];
installPhase =
let
binPath = "root/opt/oxide/mgd/bin";
in
''
mkdir -p $out/${binPath}
cp -r . $out/root
cp ${linuxBin} $out/${binPath}/mgd
chmod +x $out/${binPath}/mgd
mkdir -p $out/bin
ln -s $out/${binPath}/mgd $out/bin/mgd
'';
};
# reads the version for Clickhouse or Cockroachdb from the
# `tools/clickhouse_version` and `tools/cockroachdb_version` files.
readVersionFile = with pkgs.lib; file: trivial.pipe ./tools/${file} [
(builtins.readFile)
(strings.removeSuffix "\n")
(strings.removePrefix "v")
(debug.traceValFn (v: "${file}: ${v}"))
];
clickhouse = with pkgs;
let
name = "clickhouse";
version = readVersionFile "${name}_version";
sha256 =
let
shaFile = builtins.readFile ./tools/${name}_checksums;
shas = lib.strings.splitString "\n" shaFile;
in
findSha shas "CIDL_SHA256_LINUX";
src = builtins.fetchurl
{
inherit sha256;
url = "https://oxide-clickhouse-build.s3.us-west-2.amazonaws.com/${name}-v${version}.linux.tar.gz";
};
in
stdenv.mkDerivation
{
inherit src name version;
sourceRoot = ".";
nativeBuildInputs = [
# patch the binary to use the right dynamic library paths.
autoPatchelfHook
];
buildInputs = [
glibc
gcc-unwrapped
];
installPhase = ''
mkdir -p $out/bin
mkdir -p $out/etc
cp ./${name} $out/bin/${name}
cp ./._config.xml $out/bin/config.xml
'';
};
cockroachdb = with pkgs;
let
name = "cockroachdb";
binName = "cockroach";
version = readVersionFile "${name}_version";
lines =
let
shaFile = builtins.readFile ./tools/${name}_checksums;
in
lib.strings.splitString "\n" shaFile;
commit = findSha lines "COCKROACH_COMMIT";
sha256 = findSha lines "CIDL_SHA256_LINUX";
src = builtins.fetchurl
{
inherit sha256;
url = "https://buildomat.eng.oxide.computer/public/file/oxidecomputer/cockroach/linux-amd64/${commit}/cockroach.tgz";
};
in
stdenv.mkDerivation
{
inherit name src version;
nativeBuildInputs = [
# patch the binary to use the right dynamic library paths.
autoPatchelfHook
];
buildInputs = [
glibc
gcc-unwrapped
ncurses6
];
installPhase = ''
mkdir -p $out/bin
cp ./${binName} $out/bin/${binName}
'';
};
in
{
packages.x86_64-linux = {
inherit dendrite-stub mgd cockroachdb clickhouse;
};
checks.x86_64-linux = with pkgs;
let
# produces a check derivation that ensures a package's executable has
# the expected version.
mkVersionCheck = { pkg, cmd }: runCommand "check-${pkg.name}-version"
{
PATH = "${pkg.out}";
} ''
actualVersion=$(${pkg.out}/bin/${cmd})
if [ "$actualVersion" != "${pkg.version}" ]; then
echo "expected ${pkg.name} version \"${pkg.version}\", got \"$actualVersion\""
exit 1
fi
# the check derivation must have an output.
touch $out
'';
# produces a check derivation that ensures a package's executable
# runs.
mkExecCheck = { pkg, cmd }: runCommand "check-${pkg.name}-${cmd}-exec"
{ } ''
${pkg.out}/bin/${cmd} && touch $out
'';
in
{
clickhouseVersion = mkVersionCheck
{
pkg = clickhouse;
cmd = "clickhouse server --version | cut -d ' ' -f 4";
};
cockroachdbVersion = mkVersionCheck
{
pkg = cockroachdb;
cmd = "cockroach version --build-tag | tr -d 'v'";
};
mgdCanExec = mkExecCheck {
pkg = mgd;
cmd = "mgd help";
};
dpdCanExec = mkExecCheck {
pkg = dendrite-stub;
cmd = "dpd help";
};
swadmCanExec = mkExecCheck {
pkg = dendrite-stub;
cmd = "swadm help";
};
};
devShells.x86_64-linux.default = with pkgs;
mkShell.override
{
stdenv =
# use Mold as the linker rather than ld, for faster builds. Mold
# to require substantially less memory to link Nexus and its
# avoiding swapping on memory-constrained dev systems.
stdenvAdapters.useMoldLinker
# use Clang as the C compiler for all C libraries.
clangStdenv;
}
{
inherit buildInputs;
nativeBuildInputs = nativeBuildInputs ++ [
# Dendrite and maghemite, for running tests.
dendrite-stub
mgd
clickhouse
cockroachdb
];
name = "omicron";
DEP_PQ_LIBDIRS = "${postgresql.lib}/lib";
LIBCLANG_PATH = "${libclang.lib}/lib";
OPENSSL_DIR = "${openssl.dev}";
OPENSSL_LIB_DIR = "${openssl.out}/lib";
MG_OPENAPI_PATH = mgOpenAPI;
DDM_OPENAPI_PATH = ddmOpenAPI;
DPD_OPENAPI_PATH = dendriteOpenAPI;
# Needed by rustfmt-wrapper, see:
# https://github.com/oxidecomputer/rustfmt-wrapper/blob/main/src/lib.rs
RUSTFMT = "${rustToolchain}/bin/rustfmt";
shellHook = ''
rm out/mgd
rm out/dendrite-stub
rm -r out/clickhouse
rm -r out/cockroachdb
mkdir -p out/clickhouse
mkdir -p out/cockroachdb/
ln -s ${mgd.out} -T out/mgd
ln -s ${dendrite-stub.out} -T out/dendrite-stub
ln -s ${clickhouse.out}/bin/clickhouse out/clickhouse/clickhouse
ln -s ${clickhouse.out}/etc/config.xml out/clickhouse
ln -s ${cockroachdb.out}/bin out/cockroachdb/bin
'';
};
};
}