-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathflake.nix
More file actions
477 lines (457 loc) · 16.5 KB
/
Copy pathflake.nix
File metadata and controls
477 lines (457 loc) · 16.5 KB
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
#
# Disclaimer: This nix environment is provided as-is.
# None of this is officially supported and use is at your own risk.
# We do not maintain or support nix environments.
#
{
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";
rust-overlay.url = "github:oxalica/rust-overlay";
# Keep this revision in sync with EURYDICE_REV in .docker/c/Dockerfile,
# which is what CI uses for the extraction. charon and karamel follow
# eurydice transitively, so pinning eurydice pins all three.
eurydice.url = "github:aeneasverif/eurydice/aaa9fa657fb6f09802edb890252040d94cd93982";
eurydice.inputs.karamel.inputs.fstar.follows = "fstar-pinned";
# Pin hax 0.3.6, update if hax-lib is updated in Cargo.toml
hax.url = "github:cryspen/hax/87ba96831ecfeb7dbb54efcf97036fbc5f25bc71";
# Pin F* to the version CI uses for hax lax/prove (see .github/workflows/mlkem-hax.yml).
fstar-pinned.url = "github:FStarLang/FStar/v2025.10.06";
googletest = {
url = "github:google/googletest/release-1.11.0";
flake = false;
};
benchmark = {
url = "github:google/benchmark/v1.8.4";
flake = false;
};
json = {
url = "github:nlohmann/json/v3.10.3";
flake = false;
};
};
outputs =
{
self,
nixpkgs,
flake-utils,
rust-overlay,
eurydice,
fstar-pinned,
hax,
googletest,
benchmark,
json,
...
}@inputs:
flake-utils.lib.eachDefaultSystem (
system:
let
pkgs = import nixpkgs {
inherit system;
overlays = [ rust-overlay.overlays.default ];
};
charon = eurydice.inputs.charon;
crane = charon.inputs.crane;
# Use the overridden package exported by the eurydice flake.
karamel = eurydice.packages.${system}.karamel;
fstar = fstar-pinned;
tools-environment = {
CHARON_HOME = charon.packages.${system}.charon;
EURYDICE_HOME = pkgs.runCommand "eurydice-home" { } ''
mkdir -p $out
cp -r ${eurydice.packages.${system}.default}/bin/eurydice $out
cp -r ${eurydice}/include $out
'';
FSTAR_HOME = fstar.packages.${system}.default;
HAX_HOME = hax;
KRML_HOME = karamel;
CHARON_REV = charon.rev or "dirty";
EURYDICE_REV = eurydice.rev or "dirty";
KRML_REV = karamel.version;
FSTAR_REV = fstar.rev or "dirty";
LIBCRUX_REV = self.rev or "dirty";
};
rustToolchain = pkgs.rust-bin.stable.latest.default.override {
extensions = [
"rust-src"
"rust-analyzer"
];
targets = [ "aarch64-unknown-linux-gnu" ];
};
rustNightlyWithMiri = pkgs.rust-bin.selectLatestNightlyWith (
toolchain:
toolchain.default.override {
extensions = [
"miri"
"rust-src"
];
}
);
# Wrapper so `cargo miri ...` uses the nightly toolchain while the
# default `cargo`/`rustc` in the shell remain stable. Stable cargo
# exports CARGO/RUSTC pointing at its own binaries when invoking
# subcommands, so we override them to the nightly equivalents.
cargoMiri = pkgs.writeShellScriptBin "cargo-miri" ''
export PATH=${rustNightlyWithMiri}/bin:$PATH
export CARGO=${rustNightlyWithMiri}/bin/cargo
export RUSTC=${rustNightlyWithMiri}/bin/rustc
exec ${rustNightlyWithMiri}/bin/cargo-miri "$@"
'';
craneLib = (crane.mkLib pkgs).overrideToolchain rustToolchain;
# Cargo.lock is gitignored, so it isn't part of the flake's source
# tree. Read it from the invocation directory; requires `--impure`
# and that nix is invoked from the repo root. When the lockfile is
# absent (fresh checkout), fall back to an error-derivation so that
# the devShell still evaluates — its shellHook will then generate
# the lockfile via `cargo generate-lockfile`.
pwd = builtins.getEnv "PWD";
hasCargoLock = pwd != "" && builtins.pathExists "${pwd}/Cargo.lock";
defaultCargoLock =
if hasCargoLock then
builtins.path {
path = "${pwd}/Cargo.lock";
name = "Cargo.lock";
}
else
pkgs.runCommand "cargo-lock-missing" { } ''
cat >&2 <<'EOF'
error: Cargo.lock not found.
Cargo.lock is intentionally not committed to this repo. To
bootstrap it, enter the dev shell once (the shellHook runs
`cargo generate-lockfile` automatically):
nix develop --impure
Or run the dedicated app:
nix run --impure .#gen-lockfile
Then re-run your `nix build` command.
EOF
exit 1
'';
# Construct a copy of the current directory with the given `Cargo.lock` added.
build_src =
cargoLock:
let
src = builtins.filterSource (name: _: !(pkgs.lib.hasSuffix "flake.nix" name)) ./.;
in
pkgs.runCommand "libcrux-src" { } ''
cp -r ${src} $out
chmod u+w $out
rm -f $out/Cargo.lock
cp ${cargoLock} $out/Cargo.lock
'';
ml-kem =
pkgs.callPackage
(
{
lib,
clang-tools_18,
cmake,
mold-wrapped,
ninja,
git,
python3,
craneLib,
hax,
googletest,
benchmark,
json,
tools-environment,
cargoLock ? defaultCargoLock,
checkHax ? true,
runBenchmarks ? true,
}:
let
src = build_src cargoLock;
cargoArtifacts = craneLib.buildDepsOnly { inherit src; };
in
craneLib.buildPackage (
tools-environment
// {
name = "ml-kem";
inherit src cargoArtifacts;
nativeBuildInputs = [
clang-tools_18
# Alias `clang_format` to `clang-format-18`
(pkgs.writeShellScriptBin "clang-format-18" ''exec ${clang-tools_18}/bin/clang-format "$@"'')
cmake
mold-wrapped
ninja
git
python3
fstar.packages.${system}.default
]
++ lib.optional checkHax [
hax
];
buildPhase = ''
cd libcrux-ml-kem
patchShebangs ./.
${lib.optionalString checkHax ''
python hax.py extract
''}
./c.sh
cd c
${lib.optionalString runBenchmarks "LIBCRUX_BENCHMARKS=1"} \
cmake \
-DFETCHCONTENT_SOURCE_DIR_GOOGLETEST=${googletest} \
-DFETCHCONTENT_SOURCE_DIR_BENCHMARK=${benchmark} \
-DFETCHCONTENT_SOURCE_DIR_JSON=${json} \
-DCMAKE_EXE_LINKER_FLAGS="-fuse-ld=mold" \
-DCMAKE_SHARED_LINKER_FLAGS="-fuse-ld=mold" \
-G "Ninja Multi-Config" -B build
cmake --build build --config Release
rm -rf build/_deps
'';
checkPhase = ''
build/Release/ml_kem_test
build/Release/sha3_test
''
+ lib.optionalString runBenchmarks ''
build/Release/ml_kem_bench
'';
installPhase = ''
cd ./..
cp -r . $out
'';
}
)
)
{
inherit
googletest
benchmark
json
craneLib
tools-environment
;
hax = hax.packages.${system}.default;
clang-tools_18 = pkgs.llvmPackages_18.clang-tools;
};
ml-dsa =
pkgs.callPackage
(
{
lib,
clang-tools_18,
cmake,
mold-wrapped,
ninja,
git,
python3,
perl,
craneLib,
hax,
googletest,
benchmark,
json,
tools-environment,
cargoLock ? defaultCargoLock,
checkHax ? true,
}:
let
src = build_src cargoLock;
cargoArtifacts = craneLib.buildDepsOnly { inherit src; };
in
craneLib.buildPackage (
tools-environment
// {
name = "ml-dsa";
inherit src cargoArtifacts;
nativeBuildInputs = [
clang-tools_18
# Alias `clang_format` to `clang-format-18`
(pkgs.writeShellScriptBin "clang-format-18" ''exec ${clang-tools_18}/bin/clang-format "$@"'')
cmake
mold-wrapped
ninja
git
python3
fstar.packages.${system}.default
perl
]
++ lib.optional checkHax [
hax
];
buildPhase = ''
cd libcrux-ml-dsa
patchShebangs ./.
${lib.optionalString checkHax ''
./hax.sh extract
''}
./boring.sh --no-clean
cd cg
cmake \
-DFETCHCONTENT_SOURCE_DIR_GOOGLETEST=${googletest} \
-DFETCHCONTENT_SOURCE_DIR_BENCHMARK=${benchmark} \
-DFETCHCONTENT_SOURCE_DIR_JSON=${json} \
-DCMAKE_EXE_LINKER_FLAGS="-fuse-ld=mold" \
-DCMAKE_SHARED_LINKER_FLAGS="-fuse-ld=mold" \
-G "Ninja Multi-Config" -B build
cmake --build build --config Release
rm -rf build/_deps
'';
checkPhase = ''
build/Release/ml_dsa_test
'';
installPhase = ''
cd ./..
cp -r . $out
'';
}
)
)
{
inherit
googletest
benchmark
json
craneLib
tools-environment
;
hax = hax.packages.${system}.default;
clang-tools_18 = pkgs.llvmPackages_18.clang-tools;
};
clang-format-18-wrapper = pkgs.writeShellScriptBin "clang-format-18" ''
exec ${pkgs.llvmPackages_18.clang-tools}/bin/clang-format "$@"
'';
# Env vars from `tools-environment` coerced to strings so they're
# safe to pass through `writeShellApplication`'s `runtimeEnv`
# (which JSON-encodes anything that's still an attrset).
tools-environment-strings = builtins.mapAttrs (_: toString) tools-environment;
ml-kem-extract-app = pkgs.writeShellApplication {
name = "ml-kem-extract";
runtimeInputs = [
rustToolchain
pkgs.llvmPackages_18.clang-tools
clang-format-18-wrapper
pkgs.git
pkgs.python3
fstar.packages.${system}.default
];
runtimeEnv = tools-environment-strings;
# there is a mismatch between the version of karamel installed to $KRML_HOME by the flake.nix
# and what CI expects the layout to be from a repo checkout. In the installed version,
# the include path is include/krml/krml whereas it is include/krml in the repo
# Use --no-karamel_include for now to work around that
text = ''
root=$(git rev-parse --show-toplevel)
cd "$root/libcrux-ml-kem/extracts"
./extract-all.sh --no-karamel_include
'';
};
ml-dsa-extract-app = pkgs.writeShellApplication {
name = "ml-dsa-extract";
runtimeInputs = [
rustToolchain
pkgs.llvmPackages_18.clang-tools
clang-format-18-wrapper
pkgs.git
pkgs.python3
pkgs.perl
fstar.packages.${system}.default
];
runtimeEnv = tools-environment-strings;
text = ''
root=$(git rev-parse --show-toplevel)
cd "$root/libcrux-ml-dsa"
./boring.sh --no-clean
'';
};
combined-extract-app = pkgs.writeShellApplication {
name = "combined-extract";
runtimeInputs = [
rustToolchain
pkgs.llvmPackages_18.clang-tools
clang-format-18-wrapper
pkgs.git
];
runtimeEnv = tools-environment-strings;
text = ''
root=$(git rev-parse --show-toplevel)
cd "$root/combined_extraction"
./extract.sh "$@"
'';
};
gen-lockfile-app = pkgs.writeShellApplication {
name = "gen-lockfile";
runtimeInputs = [ rustToolchain ];
text = ''
if [ -f Cargo.lock ]; then
echo "Cargo.lock already exists at $PWD/Cargo.lock"
exit 0
fi
if [ ! -f Cargo.toml ]; then
echo "error: no Cargo.toml in $PWD — run this from the repo root" >&2
exit 1
fi
cargo generate-lockfile
'';
};
in
rec {
packages = {
inherit ml-kem ml-dsa;
};
apps = {
ml-kem-extract = {
type = "app";
program = "${ml-kem-extract-app}/bin/ml-kem-extract";
};
ml-dsa-extract = {
type = "app";
program = "${ml-dsa-extract-app}/bin/ml-dsa-extract";
};
combined-extract = {
type = "app";
program = "${combined-extract-app}/bin/combined-extract";
};
gen-lockfile = {
type = "app";
program = "${gen-lockfile-app}/bin/gen-lockfile";
};
};
devShells.default = craneLib.devShell (
tools-environment
// {
# Build inputs are listed inline rather than pulled in via
# `inputsFrom = [ packages.ml-kem ]`. That reference would force
# crane to realize `cargoArtifacts` (and therefore Cargo.lock)
# just to enter the shell, which breaks a fresh checkout where
# the lockfile is generated by the shellHook below.
packages = [
pkgs.clang_18
pkgs.openssl
pkgs.pkg-config
pkgs.jq
pkgs.valgrind
pkgs.libclang
rustToolchain
cargoMiri
hax.packages.${system}.default
fstar.packages.${system}.default
pkgs.qemu
pkgs.llvmPackages_18.clang-tools
clang-format-18-wrapper
pkgs.cmake
pkgs.mold-wrapped
pkgs.ninja
pkgs.git
pkgs.python3
pkgs.perl
];
RUST_SRC_PATH = "${rustToolchain.outPath}/lib/rustlib/src/rust/library";
LIBCLANG_PATH = "${pkgs.llvmPackages_18.libclang.lib}/lib";
CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_LINKER = "${pkgs.pkgsCross.aarch64-multiplatform.stdenv.cc}/bin/aarch64-unknown-linux-gnu-gcc";
CARGO_TARGET_AARCH64_UNKNOWN_LINUX_GNU_RUNNER = "qemu-aarch64";
CC_aarch64_unknown_linux_gnu = "${pkgs.pkgsCross.aarch64-multiplatform.stdenv.cc}/bin/aarch64-unknown-linux-gnu-gcc";
shellHook = ''
if [ ! -f Cargo.lock ] && [ -f Cargo.toml ]; then
echo "[flake] Cargo.lock missing — running 'cargo generate-lockfile'..."
cargo generate-lockfile
fi
'';
}
);
}
);
}