forked from fedimint/fedimint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flake.nix
412 lines (364 loc) · 15.7 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
{
inputs = {
nixpkgs = {
url = "github:nixos/nixpkgs/nixos-24.05";
};
flake-utils.url = "github:numtide/flake-utils";
fenix = {
url = "github:nix-community/fenix";
inputs.nixpkgs.follows = "nixpkgs";
};
flakebox = {
url = "github:dpc/flakebox?rev=12d5ee4f6c47bc01f07ec6f5848a83db265902d3";
inputs.nixpkgs.follows = "nixpkgs";
inputs.fenix.follows = "fenix";
};
bundlers = {
# TODO: switch back to upstream after https://github.com/matthewbauer/nix-bundle/pull/103 is available
url = "github:dpc/bundlers?branch=24-02-21-tar-deterministic&rev=e8aafe89a11ae0a5f3ce97d1d7d0fcfb354c79eb";
};
advisory-db = {
url = "github:rustsec/advisory-db";
flake = false;
};
};
outputs = { self, nixpkgs, flake-utils, flakebox, advisory-db, bundlers, ... }:
let
# overlay combining all overlays we use
overlayAll =
nixpkgs.lib.composeManyExtensions
[
(import ./nix/overlays/rocksdb.nix)
(import ./nix/overlays/wasm-bindgen.nix)
(import ./nix/overlays/cargo-nextest.nix)
(import ./nix/overlays/esplora-electrs.nix)
(import ./nix/overlays/clightning.nix)
(import ./nix/overlays/darwin-compile-fixes.nix)
(import ./nix/overlays/cargo-honggfuzz.nix)
];
in
{
overlays = {
# technically overlay outputs are supposed to be just a function,
# instead of a list, but keeping this one just to phase it out smoothly
fedimint = [ overlayAll ];
all = overlayAll;
wasm-bindgen = import ./nix/overlays/wasm-bindgen.nix;
darwin-compile-fixes = import ./nix/overlays/darwin-compile-fixes.nix;
cargo-honggfuzz = import ./nix/overlays/cargo-honggfuzz.nix;
};
bundlers = bundlers.bundlers;
defaultBundler = bundlers.defaultBundler;
nixosModules = {
fedimintd = import ./nix/modules/fedimintd.nix;
};
} //
flake-utils.lib.eachDefaultSystem
(system:
let
pkgs = import nixpkgs {
inherit system;
overlays = [ overlayAll ];
};
lib = pkgs.lib;
stdenv = pkgs.stdenv;
flakeboxLib = flakebox.lib.${system} {
# customizations will go here in the future
config = {
direnv.enable = false;
toolchain.components = [
"rustc"
"cargo"
"clippy"
"rust-analyzer"
"rust-src"
"llvm-tools"
];
just.rules.clippy = {
content = lib.mkForce ''
# run `cargo clippy` on everything
clippy *ARGS="--locked --offline --workspace --all-targets":
cargo clippy {{ARGS}}
# run `cargo clippy --fix` on everything
clippy-fix *ARGS="--locked --offline --workspace --all-targets":
cargo clippy {{ARGS}} --fix
'';
};
motd = {
enable = true;
command = ''
>&2 echo "🚧 In an enfort to improve documentation, we now require all structs and"
>&2 echo "🚧 and public methods to be documented with a docstring."
>&2 echo "🚧 See https://github.com/fedimint/fedimint/issues/3807"
'';
};
# we have our own weird CI workflows
github.ci.enable = false;
just.importPaths = [
"justfile.fedimint.just"
];
# we have a custom final check
just.rules.final-check.enable = false;
git.pre-commit.trailing_newline = false;
git.pre-commit.hooks = {
check_forbidden_dependencies = builtins.readFile ./nix/check-forbidden-deps.sh;
};
git.pre-commit.hooks = {
cargo-sort = builtins.readFile ./nix/check-cargo-sort.sh;
};
};
};
toolchainArgs = { };
stdTargets = flakeboxLib.mkStdTargets { };
stdToolchains = flakeboxLib.mkStdToolchains toolchainArgs;
# toolchains for the native build (default shell)
toolchainNative = flakeboxLib.mkFenixToolchain (toolchainArgs
// {
targets = (pkgs.lib.getAttrs
[
"default"
"wasm32-unknown"
]
stdTargets
);
});
# toolchains for the native + wasm build
toolchainWasm = flakeboxLib.mkFenixToolchain (toolchainArgs
// {
defaultTarget = "wasm32-unknown-unknown";
targets = (pkgs.lib.getAttrs
[
"default"
"wasm32-unknown"
]
stdTargets
);
args = {
nativeBuildInputs = [
pkgs.wasm-bindgen-cli
pkgs.geckodriver
pkgs.wasm-pack
] ++ lib.optionals (stdenv.isLinux) [
pkgs.firefox
];
};
});
# toolchains for the native + wasm build
toolchainAll = flakeboxLib.mkFenixToolchain (toolchainArgs
// {
targets = (pkgs.lib.getAttrs
([
"default"
"aarch64-android"
"x86_64-android"
"arm-android"
"armv7-android"
"wasm32-unknown"
] ++ lib.optionals pkgs.stdenv.isDarwin [
"aarch64-ios"
"aarch64-ios-sim"
"x86_64-ios"
])
stdTargets);
});
# 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 =
let
# the hash we will set if the tree is dirty;
dirtyHashPrefix = builtins.substring 0 16 self.dirtyRev;
dirtyHashSuffix = builtins.substring (40 - 16) 16 self.dirtyRev;
# the string needs to be 40 characters, like the original,
# so to denote `-dirty` we replace the middle with zeros
dirtyHash = "${dirtyHashPrefix}00000000${dirtyHashSuffix}";
in
{ package, name, placeholder, gitHash ? if (self ? rev) then self.rev else dirtyHash }:
stdenv.mkDerivation {
inherit system;
inherit name;
dontUnpack = true;
dontStrip = !pkgs.stdenv.isDarwin;
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}/${gitHash}/' $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 ];
};
craneMultiBuild = import nix/flakebox.nix {
inherit pkgs flakeboxLib advisory-db replaceGitHash;
# Yes, you're seeing right. We're passing result of this call as an argument
# to it.
inherit craneMultiBuild;
toolchains = stdToolchains // { "wasm32-unknown" = toolchainWasm; };
profiles = [ "dev" "ci" "test" "release" ];
};
devShells =
let
commonShellArgs = craneMultiBuild.commonEnvsShell // craneMultiBuild.commonArgs // {
toolchain = toolchainNative;
buildInputs = craneMultiBuild.commonArgs.buildInputs;
nativeBuildInputs = craneMultiBuild.commonArgs.nativeBuildInputs ++ [
pkgs.cargo-udeps
pkgs.cargo-audit
pkgs.cargo-deny
pkgs.cargo-sort
pkgs.parallel
pkgs.just
pkgs.time
pkgs.gawk
(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
(pkgs.hiPrio pkgs.bashInteractive)
pkgs.tmux
pkgs.tmuxinator
(pkgs.mprocs.overrideAttrs (final: prev: {
patches = prev.patches ++ [
(pkgs.fetchurl {
url = "https://github.com/pvolok/mprocs/pull/88.patch";
name = "clipboard-fix.patch";
sha256 = "sha256-9dx1vaEQ6kD66M+vsJLIq1FK+nEObuXSi3cmpSZuQWk=";
})
];
}))
pkgs.docker-compose
pkgs.tokio-console
pkgs.git
# Nix
pkgs.nixpkgs-fmt
pkgs.shellcheck
pkgs.nil
pkgs.convco
pkgs.nodePackages.bash-language-server
pkgs.sccache
] ++ lib.optionals (!stdenv.isAarch64 && !stdenv.isDarwin) [
pkgs.semgrep
] ++ lib.optionals (!stdenv.isDarwin) [
# broken on MacOS?
pkgs.cargo-workspaces
# marked as broken on MacOS
pkgs.cargo-llvm-cov
];
shellHook = ''
export REPO_ROOT="$(git rev-parse --show-toplevel)"
export PATH="$REPO_ROOT/bin:$PATH"
# 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
export RUSTC_WRAPPER=${pkgs.sccache}/bin/sccache
export CARGO_BUILD_TARGET_DIR="''${CARGO_BUILD_TARGET_DIR:-''${REPO_ROOT}/target-nix}"
export FM_DISCOVER_API_VERSION_TIMEOUT=10
[ -f "$REPO_ROOT/.shrc.local" ] && source "$REPO_ROOT/.shrc.local"
if [ ''${#TMPDIR} -ge 40 ]; then
>&2 echo "⚠️ TMPDIR too long. This might lead to problems running tests and regtest fed. Will try to use /tmp/ instead"
# Note: this seems to work fine in `nix develop`, but doesn't work on some `direnv` implementations (doesn't work for dpc at least)
export TMPDIR="/tmp"
fi
if [ "$(ulimit -Sn)" -lt "1024" ]; then
>&2 echo "⚠️ ulimit too small. Run 'ulimit -Sn 1024' to avoid problems running tests"
fi
if [ -z "$(git config --global merge.ours.driver)" ]; then
>&2 echo "⚠️ Recommended to run 'git config --global merge.ours.driver true' to enable better lock file handling. See https://blog.aspect.dev/easier-merges-on-lockfiles for more info"
fi
'';
};
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 = flakeboxLib.mkDevShell (commonShellArgs // { });
fuzz = flakeboxLib.mkDevShell (commonShellArgs // {
nativeBuildInputs = with pkgs; commonShellArgs.nativeBuildInputs ++ [
cargo-hongfuzz
libbfd_2_38
libunwind.dev
libopcodes_2_38
libblocksruntime
lldb
clang
];
});
lint = flakeboxLib.mkLintShell {
nativeBuildInputs = [
pkgs.cargo-sort
];
};
# 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 = flakeboxLib.mkDevShell (commonShellArgs // craneMultiBuild.commonEnvsShellRocksdbLinkCross // {
toolchain = toolchainAll;
shellHook = ''
# hijack cargo for our evil purposes
export CARGO_ORIG_BIN="$(${pkgs.which}/bin/which cargo)"
export REPO_ROOT="$(git rev-parse --show-toplevel)"
export PATH="$REPO_ROOT/bin:$PATH"
export PATH="''${REPO_ROOT}/nix/cargo-wrapper/:$PATH"
'';
});
# Like `cross` but only with wasm
crossWasm = flakeboxLib.mkDevShell (commonShellArgs // {
toolchain = toolchainWasm;
nativeBuildInputs = commonShellArgs.nativeBuildInputs or [ ] ++ [
pkgs.wasm-pack
pkgs.wasm-bindgen-cli
pkgs.geckodriver
] ++ lib.optionals (stdenv.isLinux) [
pkgs.firefox
];
});
replit = pkgs.mkShell {
nativeBuildInputs = with pkgs; [
pkg-config
openssl
];
};
bootstrap = pkgs.mkShell {
nativeBuildInputs = with pkgs; [
cachix
];
};
};
in
{
inherit devShells;
# 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 = craneMultiBuild;
packages = {
inherit (craneMultiBuild) gatewayd fedimint-dbtool gateway-cli fedimint-cli fedimintd fedimint-load-test-tool;
inherit (craneMultiBuild) client-pkgs gateway-pkgs fedimint-pkgs devimint;
};
lib = {
inherit replaceGitHash devShells;
};
});
nixConfig = {
extra-substituters = [ "https://fedimint.cachix.org" ];
extra-trusted-public-keys = [ "fedimint.cachix.org-1:FpJJjy1iPVlvyv4OMiN5y9+/arFLPcnZhZVVCHCDYTs=" ];
};
}