/
flakebox.nix
530 lines (462 loc) · 18.1 KB
/
flakebox.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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
{ pkgs, pkgs-unstable, pkgs-kitman, flakeboxLib, toolchains, advisory-db, profiles, craneMultiBuild, replaceGitHash }:
let
lib = pkgs.lib;
# `moreutils/bin/parallel` and `parallel/bin/parallel` conflict, so just use
# the binary we need from `moreutils`
moreutils-ts = pkgs.writeShellScriptBin "ts" "exec ${pkgs.moreutils}/bin/ts \"$@\"";
# placeholder we use to avoid actually needing to detect hash via running `git`
# 012345... for easy recognizability (in case something went wrong),
# rest randomized to avoid accidentally overwriting innocent bytes in the binary
gitHashPlaceholderValue = "01234569abcdef7afa1d2683a099c7af48a523c1";
filterWorkspaceDepsBuildFilesRegex = [ "Cargo.lock" "Cargo.toml" ".cargo" ".cargo/.*" ".config" ".config/.*" ".*/Cargo.toml" ".*/proto/.*" ];
commonSrc = builtins.path { path = ./..; name = "fedimint"; };
filterSrcWithRegexes = regexes: src:
let
basePath = toString src + "/";
in
lib.cleanSourceWith {
filter = (path: type:
let
relPath = lib.removePrefix basePath (toString path);
includePath =
(type == "directory") ||
lib.any
(re: builtins.match re relPath != null)
regexes;
in
# uncomment to debug:
# builtins.trace "${relPath}: ${lib.boolToString includePath}"
includePath
);
inherit src;
};
# Filter only files needed to build project dependencies
#
# To get good build times it's vitally important to not have to
# rebuild derivation needlessly. The way Nix caches things
# is very simple: if any input file changed, derivation needs to
# be rebuild.
#
# For this reason this filter function strips the `src` from
# any files that are not relevant to the build.
#
# Lile `filterWorkspaceFiles` but doesn't even need *.rs files
# (because they are not used for building dependencies)
filterWorkspaceDepsBuildFiles = src: filterSrcWithRegexes filterWorkspaceDepsBuildFilesRegex src;
# Filter only files relevant to building the workspace
filterWorkspaceBuildFiles = src: filterSrcWithRegexes (filterWorkspaceDepsBuildFilesRegex ++ [ ".*\.rs" ".*\.html" ".*/proto/.*" "db/migrations/.*" "devimint/src/cfg/.*" ]) src;
# Like `filterWorkspaceFiles` but with `./scripts/` included
filterWorkspaceTestFiles = src: filterSrcWithRegexes (filterWorkspaceDepsBuildFilesRegex ++ [ ".*\.rs" ".*\.html" ".*/proto/.*" "db/migrations/.*" "devimint/src/cfg/.*" "scripts/.*" ]) src;
filterWorkspaceAuditFiles = src: filterSrcWithRegexes (filterWorkspaceDepsBuildFilesRegex ++ [ "deny.toml" ]) src;
# env vars for linking rocksdb
commonEnvsShellRocksdbLink =
let
target_underscores = lib.strings.replaceStrings [ "-" ] [ "_" ] pkgs.stdenv.buildPlatform.config;
in
{
ROCKSDB_STATIC = "true";
ROCKSDB_LIB_DIR = "${pkgs.rocksdb}/lib/";
SNAPPY_LIB_DIR = "${pkgs.pkgsStatic.snappy}/lib/";
"ROCKSDB_${target_underscores}_STATIC" = "true";
"ROCKSDB_${target_underscores}_LIB_DIR" = "${pkgs.rocksdb}/lib/";
"SNAPPY_${target_underscores}_LIB_DIR" = "${pkgs.pkgsStatic.snappy}/lib/";
} // pkgs.lib.optionalAttrs (!pkgs.stdenv.isDarwin) {
# macos can't static libraries
SNAPPY_STATIC = "true";
"SNAPPY_${target_underscores}_STATIC" = "true";
};
commonEnvsShellRocksdbLinkCross = commonEnvsShellRocksdbLink // pkgs.lib.optionalAttrs (!pkgs.stdenv.isDarwin) {
# TODO: could we used the android-nixpkgs toolchain instead of another one?
# ROCKSDB_aarch64_linux_android_STATIC = "true";
# SNAPPY_aarch64_linux_android_STATIC = "true";
# ROCKSDB_aarch64_linux_android_LIB_DIR = "${pkgs-unstable.pkgsCross.aarch64-android-prebuilt.rocksdb}/lib/";
# SNAPPY_aarch64_linux_android_LIB_DIR = "${pkgs-unstable.pkgsCross.aarch64-android-prebuilt.pkgsStatic.snappy}/lib/";
# BROKEN
# error: "No timer implementation for this platform"
# ROCKSDB_armv7_linux_androideabi_STATIC = "true";
# SNAPPY_armv7_linux_androideabi_STATIC = "true";
# ROCKSDB_armv7_linux_androideabi_LIB_DIR = "${pkgs-unstable.pkgsCross.armv7a-android-prebuilt.rocksdb}/lib/";
# SNAPPY_armv7_linux_androideabi_LIB_DIR = "${pkgs-unstable.pkgsCross.armv7a-android-prebuilt.pkgsStatic.snappy}/lib/";
# x86-64-linux-android doesn't have a toolchain in nixpkgs
} // pkgs.lib.optionalAttrs pkgs.stdenv.isDarwin {
# broken: fails to compile with:
# `linux-headers-android-common> sh: line 1: gcc: command not found`
# ROCKSDB_aarch64_linux_android_STATIC = "true";
# SNAPPY_aarch64_linux_android_STATIC = "true";
# ROCKSDB_aarch64_linux_android_LIB_DIR = "${pkgs-unstable.pkgsCross.aarch64-android.rocksdb}/lib/";
# SNAPPY_aarch64_linux_android_LIB_DIR = "${pkgs-unstable.pkgsCross.aarch64-android.pkgsStatic.snappy}/lib/";
# requires downloading Xcode manually and adding to /nix/store
# then running with `env NIXPKGS_ALLOW_UNFREE=1 nix develop -L --impure`
# maybe we could live with it?
# ROCKSDB_aarch64_apple_ios_STATIC = "true";
# SNAPPY_aarch64_apple_ios_STATIC = "true";
# ROCKSDB_aarch64_apple_ios_LIB_DIR = "${pkgs-unstable.pkgsCross.iphone64.rocksdb}/lib/";
# SNAPPY_aarch64_apple_ios_LIB_DIR = "${pkgs-unstable.pkgsCross.iphone64.pkgsStatic.snappy}/lib/";
};
# env variables we want to set in all nix derivations & nix develop shell
commonEnvsShell = commonEnvsShellRocksdbLink // {
PROTOC = "${pkgs.protobuf}/bin/protoc";
PROTOC_INCLUDE = "${pkgs.protobuf}/include";
};
# env variables we want to set in all nix derivations (but NOT the nix develop shell)
commonEnvsBuild = commonEnvsShell // {
FEDIMINT_BUILD_FORCE_GIT_HASH = gitHashPlaceholderValue;
HOME = "/tmp";
};
commonArgs = {
pname = "fedimint";
buildInputs = with pkgs; [
openssl
pkg-config
protobuf
] ++ lib.optionals (!stdenv.isDarwin) [
util-linux
iproute2
] ++ lib.optionals stdenv.isDarwin [
libiconv
darwin.apple_sdk.frameworks.Security
] ++ builtins.attrValues {
inherit (pkgs) openssl;
};
nativeBuildInputs = with pkgs; [
pkg-config
moreutils-ts
# tests
(hiPrio pkgs.bashInteractive)
bc
bitcoind
clightning
electrs
jq
lnd
netcat
perl
pkgs-kitman.esplora
procps
which
cargo-nextest
moreutils-ts
parallel
] ++ builtins.attrValues {
inherit (pkgs) cargo-nextest;
};
# we carefully optimize our debug symbols on cargo level,
# and in case of errors and panics, would like to see the
# line numbers etc.
dontStrip = true;
};
commonCliTestArgs = commonArgs // {
pname = "fedimint-test";
# there's no point saving the `./target/` dir
doInstallCargoArtifacts = false;
# the build command will be the test
doCheck = true;
};
in
(flakeboxLib.craneMultiBuild { inherit toolchains profiles; }) (craneLib':
let
craneLib =
(craneLib'.overrideArgs (commonEnvsBuild // commonArgs // {
src = filterWorkspaceBuildFiles commonSrc;
pname = "fedimint";
version = "0.1.0";
})).overrideArgs'' (craneLib: args:
pkgs.lib.optionalAttrs (!(builtins.elem (craneLib.toolchainName or null) [ null "default" "stable" "nightly" ])) commonEnvsShellRocksdbLinkCross
);
craneLibTests = craneLib.overrideArgs (commonEnvsBuild // commonCliTestArgs // {
src = filterWorkspaceTestFiles commonSrc;
# there's no point saving the `./target/` dir
doInstallCargoArtifacts = false;
});
fedimintBuildPackageGroup = args: replaceGitHash {
name = args.pname;
package =
craneLib.buildPackageGroup args;
placeholder = gitHashPlaceholderValue;
};
in
rec {
inherit commonArgs;
inherit commonEnvsShell;
inherit commonEnvsShellRocksdbLink;
inherit commonEnvsShellRocksdbLinkCross;
inherit gitHashPlaceholderValue;
commonArgsBase = commonArgs;
workspaceDeps = craneLib.buildWorkspaceDepsOnly {
buildPhaseCargoCommand = "cargoWithProfile doc --locked ; cargoWithProfile check --all-targets --locked ; cargoWithProfile build --locked --all-targets";
};
workspaceBuild = craneLib.buildWorkspace {
cargoArtifacts = workspaceDeps;
buildPhaseCargoCommand = "cargoWithProfile doc --locked ; cargoWithProfile check --all-targets --locked ; cargoWithProfile build --locked --all-targets";
};
workspaceTest = craneLib.cargoNextest {
cargoArtifacts = workspaceBuild;
cargoExtraArgs = "--workspace --all-targets --locked";
FM_CARGO_DENY_COMPILATION = "1";
};
workspaceTestDoc = craneLib.cargoTest {
# can't use nextest due to: https://github.com/nextest-rs/nextest/issues/16
cargoTestExtraArgs = "--doc";
cargoArtifacts = workspaceBuild;
# workaround: `cargo test --doc` started to ignore CARGO_TARGET_<native-target>_RUSTFLAGS
# out of the blue
stdenv = pkgs.clangStdenv;
};
workspaceClippy = craneLib.cargoClippy {
cargoArtifacts = workspaceDeps;
cargoClippyExtraArgs = "--workspace --all-targets --no-deps -- --deny warnings --allow deprecated";
doInstallCargoArtifacts = false;
};
workspaceDoc = craneLib.mkCargoDerivation {
pnameSuffix = "-workspace-docs";
cargoArtifacts = workspaceDeps;
preConfigure = ''
export RUSTDOCFLAGS='-D rustdoc::broken_intra_doc_links -D warnings'
'';
buildPhaseCargoCommand = "cargo doc --workspace --no-deps --document-private-items";
doInstallCargoArtifacts = false;
postInstall = ''
cp -a target/doc/ $out
'';
doCheck = false;
};
# version of `workspaceDocs` with some nightly-only flags to publish
workspaceDocExport = craneLib.mkCargoDerivation {
# no need for inheriting any artifacts, as we are using it as a one-off, and only care
# about the docs
cargoArtifacts = null;
preConfigure = ''
export RUSTDOCFLAGS='-D rustdoc::broken_intra_doc_links -Z unstable-options --enable-index-page -D warnings'
'';
buildPhaseCargoCommand = "cargo doc --workspace --no-deps --document-private-items";
doInstallCargoArtifacts = false;
installPhase = ''
cp -a target/doc/ $out
'';
doCheck = false;
};
workspaceCargoUdepsDeps = craneLib.buildDepsOnly {
pname = "${commonArgs.pname}-udeps-deps";
nativeBuildInputs = commonArgs.nativeBuildInputs ++ [ pkgs.cargo-udeps ];
# since we filtered all the actual project source, everything will definitely fail
# but we only run this step to cache the build artifacts, so we ignore failure with `|| true`
buildPhaseCargoCommand = "cargo udeps --workspace --all-targets --profile $CARGO_PROFILE || true";
doCheck = false;
};
workspaceCargoUdeps = craneLib.mkCargoDerivation {
pname = "fedimint-udeps";
# no need for inheriting any artifacts, as we are using it as a one-off, and only care
# about the docs
cargoArtifacts = workspaceCargoUdepsDeps;
nativeBuildInputs = commonArgs.nativeBuildInputs ++ [ pkgs.cargo-udeps ];
buildPhaseCargoCommand = "cargo udeps --workspace --all-targets --profile $CARGO_PROFILE";
doInstallCargoArtifacts = false;
doCheck = false;
};
cargoAudit = craneLib.cargoAudit {
inherit advisory-db;
src = filterWorkspaceAuditFiles commonSrc;
};
cargoDeny = craneLib.cargoDeny {
src = filterWorkspaceAuditFiles commonSrc;
};
# Build only deps, but with llvm-cov so `workspaceCov` can reuse them cached
workspaceDepsCov = craneLib.buildDepsOnly {
pname = "fedimint-workspace-lcov";
buildPhaseCargoCommand = "source <(cargo llvm-cov show-env --export-prefix); cargo build --locked --workspace --all-targets --profile $CARGO_PROFILE";
cargoBuildCommand = "dontuse";
cargoCheckCommand = "dontuse";
nativeBuildInputs = [ pkgs.cargo-llvm-cov ];
doCheck = false;
};
workspaceCov = craneLib.buildPackage {
pname = "fedimint-workspace-lcov";
cargoArtifacts = workspaceDepsCov;
buildPhaseCargoCommand = "source <(cargo llvm-cov show-env --export-prefix); cargo build --locked --workspace --all-targets --profile $CARGO_PROFILE; env RUST_BACKTRACE=1 RUST_LOG=info,timing=debug cargo nextest run --locked --workspace --all-targets --cargo-profile $CARGO_PROFILE --profile $CARGO_PROFILE --test-threads=$(($(nproc) * 2)); mkdir -p $out ; cargo llvm-cov report --profile $CARGO_PROFILE --lcov --output-path $out/lcov.info";
installPhaseCommand = "true";
nativeBuildInputs = [ pkgs.cargo-llvm-cov ];
doCheck = false;
};
reconnectTest = craneLibTests.mkCargoDerivation {
pname = "${commonCliTestArgs.pname}-reconnect";
cargoArtifacts = workspaceBuild;
buildPhaseCargoCommand = "patchShebangs ./scripts ; ./scripts/tests/reconnect-test.sh";
};
latencyTest = craneLibTests.mkCargoDerivation {
pname = "${commonCliTestArgs.pname}-latency";
cargoArtifacts = workspaceBuild;
buildPhaseCargoCommand = "patchShebangs ./scripts ; ./scripts/tests/latency-test.sh";
};
devimintCliTest = craneLibTests.mkCargoDerivation {
pname = "${commonCliTestArgs.pname}-cli";
cargoArtifacts = workspaceBuild;
buildPhaseCargoCommand = "patchShebangs ./scripts ; ./scripts/tests/devimint-cli-test.sh";
};
cliLoadTestToolTest = craneLibTests.mkCargoDerivation {
pname = "${commonCliTestArgs.pname}-cli";
cargoArtifacts = workspaceBuild;
buildPhaseCargoCommand = "patchShebangs ./scripts ; ./scripts/tests/load-test-tool-test.sh";
};
backendTest = craneLibTests.mkCargoDerivation {
pname = "${commonCliTestArgs.pname}-backend-test";
cargoArtifacts = workspaceBuild;
buildPhaseCargoCommand = "patchShebangs ./scripts ; ./scripts/test/backend-test.sh";
};
ciTestAll = craneLibTests.mkCargoDerivation {
pname = "${commonCliTestArgs.pname}-all";
cargoArtifacts = workspaceBuild;
# One normal run, then if succeeded, modify the "always success test" to fail,
# and make sure we detect it (happened too many times that we didn't).
# Thanks to early termination, this should be all very quick, as we actually
# won't start other tests.
buildPhaseCargoCommand = ''
patchShebangs ./scripts
export FM_CARGO_DENY_COMPILATION=1
./scripts/tests/test-ci-all.sh || exit 1
sed -i -e 's/exit 0/exit 1/g' scripts/tests/always-success-test.sh
echo "Verifying failure detection..."
./scripts/tests/test-ci-all.sh 1>/dev/null 2>/dev/null && exit 1
'';
};
alwaysFailTest = craneLibTests.mkCargoDerivation {
pname = "${commonCliTestArgs.pname}-always-fail";
cargoArtifacts = workspaceBuild;
buildPhaseCargoCommand = "patchShebangs ./scripts ; ./scripts/tests/always-fail-test.sh";
};
wasmTest = craneLibTests.mkCargoDerivation {
pname = "wasm-test";
# TODO: https://github.com/ipetkov/crane/issues/416
cargoArtifacts = craneMultiBuild.${craneLib.cargoProfile or "release"}.workspaceBuild;
nativeBuildInputs = commonCliTestArgs.nativeBuildInputs ++ [ pkgs.firefox pkgs.wasm-bindgen-cli pkgs.geckodriver pkgs.wasm-pack ];
buildPhaseCargoCommand = "patchShebangs ./scripts; SKIP_CARGO_BUILD=1 ./scripts/tests/wasm-test.sh";
};
fedimint-pkgs = fedimintBuildPackageGroup {
pname = "fedimint-pkgs";
packages = [
"fedimintd"
"fedimint-cli"
"fedimint-dbtool"
];
defaultBin = "fedimintd";
};
gateway-pkgs = fedimintBuildPackageGroup {
pname = "gateway-pkgs";
packages = [
"ln-gateway"
"gateway-cli"
];
};
client-pkgs = fedimintBuildPackageGroup {
pname = "client-pkgs";
packages = [
"fedimint-client"
];
};
devimint = fedimintBuildPackageGroup {
pname = "devimint";
packages = [
"devimint"
];
};
fedimint-load-test-tool = fedimintBuildPackageGroup {
pname = "fedimint-load-test-tool";
packages = [ "fedimint-load-test-tool" ];
};
fedimintd = flakeboxLib.pickBinary
{
pkg = fedimint-pkgs;
bin = "fedimintd";
};
fedimint-cli = flakeboxLib.pickBinary
{
pkg = fedimint-pkgs;
bin = "fedimint-cli";
};
fedimint-dbtool = flakeboxLib.pickBinary
{
pkg = fedimint-pkgs;
bin = "fedimint-dbtool";
};
gatewayd = flakeboxLib.pickBinary
{
pkg = gateway-pkgs;
bin = "gatewayd";
};
gateway-cli = flakeboxLib.pickBinary
{
pkg = gateway-pkgs;
bin = "gateway-cli";
};
container =
let
entrypointScript =
pkgs.writeShellScriptBin "entrypoint" ''
exec bash "${../misc/fedimintd-container-entrypoint.sh}" "$@"
'';
in
{
fedimintd = pkgs.dockerTools.buildLayeredImage {
name = "fedimintd";
contents = [
fedimint-pkgs
pkgs.bash
pkgs.coreutils
];
config = {
Cmd = [ ]; # entrypoint will handle empty vs non-empty cmd
Env = [
"FM_DATA_DIR=/data"
];
Entrypoint = [
"${entrypointScript}/bin/entrypoint"
];
WorkDir = "/data";
Volumes = {
"/data" = { };
};
ExposedPorts = {
"${builtins.toString 8173}/tcp" = { };
"${builtins.toString 8174}/tcp" = { };
};
};
};
fedimint-cli = pkgs.dockerTools.buildLayeredImage {
name = "fedimint-cli";
contents = [ fedimint-pkgs pkgs.bash pkgs.coreutils ];
config = {
Cmd = [
"${fedimint-pkgs}/bin/fedimint-cli"
];
};
};
gatewayd = pkgs.dockerTools.buildLayeredImage {
name = "gatewayd";
contents = [ gateway-pkgs pkgs.bash pkgs.coreutils ];
config = {
Cmd = [
"${gateway-pkgs}/bin/gatewayd"
];
};
};
gateway-cli = pkgs.dockerTools.buildLayeredImage {
name = "gateway-cli";
contents = [ gateway-pkgs pkgs.bash pkgs.coreutils ];
config = {
Cmd = [
"${gateway-pkgs}/bin/gateway-cli"
];
};
};
devtools =
pkgs.dockerTools.buildLayeredImage
{
name = "fedimint-devtools";
contents = [ devimint fedimint-dbtool fedimint-load-test-tool pkgs.bash pkgs.coreutils ];
config = {
Cmd = [
"${pkgs.bash}/bin/bash"
];
};
};
};
})