forked from fedimint/fedimint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flake.nix
518 lines (457 loc) · 21.4 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
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
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-22.11";
nixpkgs-unstable.url = "github:NixOS/nixpkgs/nixos-unstable";
nixpkgs-kitman.url = "github:jkitman/nixpkgs/add-esplora-pkg";
crane.url = "github:ipetkov/crane?rev=116b32c30b5ff28e49f4fcbeeb1bbe3544593204";
crane.inputs.nixpkgs.follows = "nixpkgs";
flake-utils.url = "github:numtide/flake-utils";
fenix = {
url = "github:nix-community/fenix";
inputs.nixpkgs.follows = "nixpkgs";
};
flake-compat = {
url = "github:edolstra/flake-compat";
flake = false;
};
advisory-db = {
url = "github:rustsec/advisory-db";
flake = false;
};
android-nixpkgs = {
# url = "github:tadfisher/android-nixpkgs?rev=39538bf26d9064555c2a77b5bd6eb88049285905"; # stable
url = "github:dpc/android-nixpkgs?rev=ffce46832f161877b7c197bfc7def734e8b9caa4"; # stable channel + workaround https://github.com/tadfisher/android-nixpkgs/issues/59
};
};
outputs = { self, nixpkgs, nixpkgs-unstable, nixpkgs-kitman, flake-utils, flake-compat, fenix, crane, advisory-db, android-nixpkgs }:
flake-utils.lib.eachDefaultSystem
(system:
let
pkgs = import nixpkgs {
inherit system;
};
pkgs-kitman = import nixpkgs-kitman {
inherit system;
};
pkgs-unstable = import nixpkgs-unstable {
inherit system;
};
lib = pkgs.lib;
stdenv = pkgs.stdenv;
clightning-dev = pkgs.clightning.overrideAttrs (oldAttrs: {
configureFlags = [ "--enable-developer" "--disable-valgrind" ];
} // pkgs.lib.optionalAttrs (!pkgs.stdenv.isDarwin) {
NIX_CFLAGS_COMPILE = "-Wno-stringop-truncation";
});
# `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 \"$@\"";
toolchain = import ./flake.toolchain.nix {
inherit pkgs lib system stdenv fenix android-nixpkgs;
};
craneLib' = crane.lib.${system};
craneLibNative' = craneLib'.overrideToolchain toolchain.fenixToolchain;
craneLibCross' = builtins.mapAttrs
(name: target: crane.lib.${system}.overrideToolchain toolchain.fenixToolchainCross.${name})
toolchain.crossTargets
;
craneExtendCommon = import ./nix/craneCommon.nix
{
inherit pkgs pkgs-kitman clightning-dev advisory-db lib moreutils-ts;
src = ./.;
srcDotCargo = ./.cargo;
};
craneExtendBuild = import ./nix/craneBuild.nix
{
inherit pkgs pkgs-kitman clightning-dev advisory-db lib moreutils-ts;
};
craneLibNative = craneExtendBuild (craneExtendCommon craneLibNative');
craneLibDevShell = craneLibNative.overrideScope' (self: prev: {
commonProfile = null;
});
craneLibCross = target: craneExtendBuild (craneExtendCommon craneLibCross'.${target});
# Replace placeholder git hash in a binary
#
# To avoid impurity, we use a git hash placeholder when building binaries
# and then replace them with the real git hash in the binaries themselves.
replaceGitHash = { package, name, placeholder }:
let
# the hash we will set if the tree is dirty;
dirty-hash = "0000000000000000000000000000000000000000";
# git hash to set (passed by Nix if the tree is clean, or `dirty-hash` when dirty)
git-hash = if (self ? rev) then self.rev else dirty-hash;
in
stdenv.mkDerivation {
inherit system;
inherit name;
dontUnpack = true;
installPhase = ''
cp -a ${package} $out
for path in `find $out -type f -executable`; do
# need to use a temporary file not to overwrite source as we are reading it
bbe -e 's/${placeholder}/${git-hash}/' $path -o ./tmp || exit 1
chmod +w $path
# use cat to keep all the original permissions etc as they were
cat ./tmp > "$path"
chmod -w $path
done
'';
buildInputs = [ pkgs.bbe ];
};
# Create a package that contains only one `bin`ary from an input `pkg`
#
# For efficiency we built some binaries together (like fedimintd + fedimint-cli),
# but we would like to expose them separately.
pickBinary = { pkg, bin }:
stdenv.mkDerivation {
inherit system;
name = bin;
dontUnpack = true;
installPhase = ''
mkdir -p $out/bin
cp -a ${pkg}/bin/${bin} $out/bin/${bin}
'';
};
# outputs that do something over the whole workspace
workspaceOutputs = craneLib: {
workspaceDeps = craneLib.workspaceDeps;
workspaceBuild = craneLib.workspaceBuild;
workspaceClippy = craneLib.workspaceClippy;
workspaceTest = craneLib.workspaceTest;
workspaceTestDoc = craneLib.workspaceTestDoc;
workspaceDoc = craneLib.workspaceDoc;
workspaceDocExport = (craneLib.overrideToolchain toolchain.fenixToolchainDocNightly).workspaceDocExport;
workspaceCargoUdeps = ((craneLib.overrideScope' (self: prev: {
# udeps works only with `test` profile
commonProfile = "test";
})).overrideToolchain toolchain.fenixToolchainDocNightly).workspaceCargoUdeps;
workspaceCov = craneLib.workspaceCov;
workspaceAudit = craneLib.workspaceAudit;
};
# Outputs that build a particular Rust package.
# Notably, these don't have the git hash replaced (yet) - for that, see `rustPackageOutputsFinal`
rustPackageOutputs = craneLib: {
default = craneLib.fedimint-pkgs;
fedimint-pkgs = craneLib.fedimint-pkgs;
gateway-pkgs = craneLib.gateway-pkgs;
client-pkgs = craneLib.client-pkgs { };
fedimint-dbtool-pkgs = craneLib.fedimint-dbtool-pkgs;
devimint = craneLib.devimint;
fedimint-load-test-tool = craneLib.fedimint-load-test-tool;
};
# `rustPackageOutputs` with git hash replaced from placeholder to a real value
# To avoid rebuilding too much source needlessly, we replace placeholders with real git hash (which changes on every build),
# as a very last step.
rustPackageOutputsFinal = craneLib: builtins.mapAttrs (name: package: replaceGitHash { inherit name package; placeholder = craneLib.gitHashPlaceholderValue; }) (rustPackageOutputs craneLib);
# All tests, grouped together, so we can `nix build -L .#cli-test.<name>` or `nix build -L .#ci.cli-test.<name>`, etc.
cli-test = craneLib: {
all = craneLib.cliTestsAll;
reconnect = craneLib.cliTestReconnect;
latency = craneLib.cliTestLatency;
cli = craneLib.cliTestCli;
load-test-tool = craneLib.cliLoadTestToolTest;
rust-tests = craneLib.cliRustTests;
always-fail = craneLib.cliTestAlwaysFail;
};
wasm-tests = { craneLibNative, craneLibCross }: craneLibCross.wasmTests {
nativeWorkspaceBuild = craneLibNative.workspaceBuild;
wasmTarget = toolchain.crossTargets.wasm32-unknown-unknown;
};
# packages we expose from our flake (note: we also expose `legacyPackages` for hierarchical outputs)
packages = craneLib: (workspaceOutputs craneLib) //
# replace git hash in the final binaries
(rustPackageOutputsFinal craneLib)
// {
fedimintd = pickBinary
{
pkg = (rustPackageOutputsFinal craneLib).fedimint-pkgs;
bin = "fedimintd";
};
fedimint-cli = pickBinary
{
pkg = (rustPackageOutputsFinal craneLib).fedimint-pkgs;
bin = "fedimint-cli";
};
gatewayd = pickBinary
{
pkg = (rustPackageOutputsFinal craneLib).gateway-pkgs;
bin = "gatewayd";
};
gateway-cli = pickBinary
{
pkg = (rustPackageOutputsFinal craneLib).gateway-pkgs;
bin = "gateway-cli";
};
fedimint-load-test-tool = pickBinary
{
pkg = (rustPackageOutputsFinal craneLib).fedimint-load-test-tool;
bin = "fedimint-load-test-tool";
};
};
# Technically nested sets are not allowed in `packages`, so we can
# dump the nested things here. They'll work the same way for most
# purposes (like `nix build`).
legacyPackages =
let
craneLibDebug = craneLibNative.overrideScope' (self: prev: {
commonProfile = "dev";
});
craneLibCi = craneLibNative.overrideScope' (self: prev: {
commonProfile = "ci";
});
in
{
# Debug Builds
#
# This works by using `overrideAttrs` on output derivations to set `CARGO_PROFILE`, and importantly
# recursing into `cargoArtifacts` to do the same. This way a debug build depends on debug build of all dependencies.
# See https://github.com/ipetkov/crane/discussions/140#discussioncomment-3857137 for more info.
debug = (workspaceOutputs craneLibDebug) // { cli-test = cli-test craneLibDebug; } // (packages craneLibDebug);
ci = (workspaceOutputs craneLibCi) // {
wasm-tests = wasm-tests {
craneLibCross = (craneLibCross "wasm32-unknown-unknown").overrideScope' (self: prev: {
commonProfile = "ci";
});
craneLibNative = craneLibCi;
};
cli-test = cli-test craneLibCi;
} // (packages craneLibCi);
cross = builtins.mapAttrs
(name: target: {
client-pkgs = (craneLibCross name).client-pkgs { inherit target; };
})
toolchain.crossTargets;
container =
let
entrypointScript =
pkgs.writeShellScriptBin "entrypoint" ''
exec bash "${./misc/fedimintd-container-entrypoint.sh}" "$@"
'';
packagesNative = (packages craneLibNative);
in
{
fedimintd = pkgs.dockerTools.buildLayeredImage {
name = "fedimintd";
contents = [
packagesNative.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 = [ craneLibNative.fedimint-pkgs pkgs.bash pkgs.coreutils ];
config = {
Cmd = [
"${craneLibNative.fedimint-pkgs}/bin/fedimint-cli"
];
};
};
gatewayd = pkgs.dockerTools.buildLayeredImage {
name = "gatewayd";
contents = [ craneLibNative.gateway-pkgs pkgs.bash pkgs.coreutils ];
config = {
Cmd = [
"${craneLibNative.gateway-pkgs}/bin/gatewayd"
];
};
};
gateway-cli = pkgs.dockerTools.buildLayeredImage {
name = "gateway-cli";
contents = [ craneLibNative.gateway-pkgs pkgs.bash pkgs.coreutils ];
config = {
Cmd = [
"${craneLibNative.gateway-pkgs}/bin/gateway-cli"
];
};
};
};
};
devShells =
let
shellCommon = craneLib:
let
build = craneLibDevShell;
commonArgs = build.commonArgs;
commonEnvsShell = build.commonEnvsShell;
in
commonEnvsShell // {
buildInputs = commonArgs.buildInputs;
nativeBuildInputs = with pkgs; commonArgs.nativeBuildInputs ++ [
pkgs.rust-analyzer
toolchain.fenixToolchainRustfmt
cargo-llvm-cov
cargo-udeps
pkgs.parallel
pkgs.just
typos
(pkgs.writeShellScriptBin "git-recommit" "exec git commit --edit -F <(cat \"$(git rev-parse --git-path COMMIT_EDITMSG)\" | grep -v -E '^#.*') \"$@\"")
# This is required to prevent a mangled bash shell in nix develop
# see: https://discourse.nixos.org/t/interactive-bash-with-nix-develop-flake/15486
(hiPrio pkgs.bashInteractive)
tmux
tmuxinator
(mprocs.overrideAttrs (final: prev: {
patches = prev.patches ++ [
(fetchurl {
url = "https://github.com/pvolok/mprocs/pull/88.patch";
name = "clipboard-fix.patch";
sha256 = "sha256-9dx1vaEQ6kD66M+vsJLIq1FK+nEObuXSi3cmpSZuQWk=";
})
];
}))
docker-compose
pkgs.tokio-console
pkgs.git
moreutils-ts
# Nix
pkgs.nixpkgs-fmt
pkgs.shellcheck
pkgs.rnix-lsp
pkgs.nil
pkgs-unstable.convco
pkgs.nodePackages.bash-language-server
] ++ lib.optionals (!stdenv.isAarch64 && !stdenv.isDarwin) [
pkgs.semgrep
] ++ lib.optionals (stdenv.isLinux) [
xclip
wl-clipboard
];
RUST_SRC_PATH = "${toolchain.fenixChannel.rust-src}/lib/rustlib/src/rust/library";
shellHook = ''
# auto-install git hooks
dot_git="$(git rev-parse --git-common-dir)"
if [[ ! -d "$dot_git/hooks" ]]; then mkdir "$dot_git/hooks"; fi
for hook in misc/git-hooks/* ; do ln -sf "$(pwd)/$hook" "$dot_git/hooks/" ; done
${pkgs.git}/bin/git config commit.template misc/git-hooks/commit-template.txt
# workaround https://github.com/rust-lang/cargo/issues/11020
cargo_cmd_bins=( $(ls $HOME/.cargo/bin/cargo-{clippy,udeps,llvm-cov} 2>/dev/null) )
if (( ''${#cargo_cmd_bins[@]} != 0 )); then
>&2 echo "⚠️ Detected binaries that might conflict with reproducible environment: ''${cargo_cmd_bins[@]}" 1>&2
>&2 echo " Considering deleting them. See https://github.com/rust-lang/cargo/issues/11020 for details" 1>&2
fi
# Note: the string escaping necessary here (Nix's multi-line string and shell's) is mind-twisting.
if [ -n "$TMUX" ]; then
# if [ "$(tmux show-options -A default-command)" == 'default-command* \'\''' ]; then
if [ "$(tmux show-options -A default-command)" == 'bla' ]; then
echo
>&2 echo "⚠️ tmux's 'default-command' not set"
>&2 echo " ️ Please add 'set -g default-command \"\''${SHELL}\"' to your '$HOME/.tmux.conf' for tmuxinator test setup to work correctly"
fi
fi
# if running in direnv
if [ -n "''${DIRENV_IN_ENVRC:-}" ]; then
# and not set DIRENV_LOG_FORMAT
if [ -n "''${DIRENV_LOG_FORMAT:-}" ]; then
>&2 echo "💡 Set 'DIRENV_LOG_FORMAT=\"\"' in your shell environment variables for a cleaner output of direnv"
fi
fi
if [ ''${#TMPDIR} -ge 40 ]; then
>&2 echo "⚠️ TMPDIR too long. This might lead to problems running tests and regtest fed. Are you nesting 'nix develop' invocations?"
fi
if [ "$(ulimit -Sn)" -lt "1024" ]; then
>&2 echo "⚠️ ulimit too small. Run 'ulimit -Sn 1024' to avoid problems running tests"
fi
>&2 echo "💡 Run 'just' for a list of available 'just ...' helper recipes"
'';
};
shellCommonNative = shellCommon toolchain.craneLibNative;
shellCommonCross = shellCommon toolchain.craneLibCross;
in
{
# The default shell - meant to developers working on the project,
# so notably not building any project binaries, but including all
# the settings and tools necessary to build and work with the codebase.
default = pkgs.mkShell (shellCommonNative
// {
nativeBuildInputs = shellCommonNative.nativeBuildInputs ++ [ toolchain.fenixToolchain ];
});
nightly = pkgs.mkShell (shellCommonNative
// {
nativeBuildInputs = shellCommonNative.nativeBuildInputs ++ [ toolchain.fenixToolchainNightly pkgs.cargo-fuzz ];
});
# Shell with extra stuff to support cross-compilation with `cargo build --target <target>`
#
# This will pull extra stuff so to save time and download time to most common developers,
# was moved into another shell.
cross = pkgs.mkShell (shellCommonCross // {
nativeBuildInputs = shellCommonCross.nativeBuildInputs ++ [ toolchain.fenixToolchainCrossAll ];
shellHook = shellCommonCross.shellHook
+ toolchain.androidCrossEnvVars
+ toolchain.wasm32CrossEnvVars;
});
# Like `cross` but only with wasm
crossWasm = pkgs.mkShell (shellCommonCross // {
nativeBuildInputs = shellCommonCross.nativeBuildInputs ++ [
toolchain.fenixToolchainCrossWasm
pkgs.wasm-pack
pkgs.wasm-bindgen-cli
pkgs.geckodriver
] ++ lib.optionals (stdenv.isLinux) [
pkgs.firefox
];
shellHook = shellCommonCross.shellHook + toolchain.wasm32CrossEnvVars;
});
# this shell is used only in CI, so it should contain minimum amount
# of stuff to avoid building and caching things we don't need
lint = pkgs.mkShell {
nativeBuildInputs = with pkgs; [
toolchain.fenixToolchainCargoFmt
nixpkgs-fmt
shellcheck
git
parallel
semgrep
typos
moreutils-ts
nix
];
};
replit = pkgs.mkShell {
nativeBuildInputs = with pkgs; [
pkg-config
openssl
];
LIBCLANG_PATH = "${pkgs.libclang.lib}/lib/";
};
bootstrap = pkgs.mkShell {
nativeBuildInputs = with pkgs; [
cachix
];
};
};
in
{
inherit devShells legacyPackages;
packages = packages craneLibNative;
lib = {
inherit replaceGitHash devShells;
commonArgsBase = craneLibNative.commonArgsBase;
};
checks = {
workspaceBuild = craneLibNative.workspaceBuild;
workspaceClippy = craneLibNative.workspaceClippy;
};
});
nixConfig = {
extra-substituters = [ "https://fedimint.cachix.org" ];
extra-trusted-public-keys = [ "fedimint.cachix.org-1:FpJJjy1iPVlvyv4OMiN5y9+/arFLPcnZhZVVCHCDYTs=" ];
};
}