-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathflake.nix
More file actions
337 lines (306 loc) · 13.2 KB
/
flake.nix
File metadata and controls
337 lines (306 loc) · 13.2 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
{
description = "a minimal WASM interpreter";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-25.11";
typix = {
url = "github:loqusion/typix";
inputs.nixpkgs.follows = "nixpkgs";
};
utils.url = "git+https://github.com/numtide/flake-utils.git";
devshell.url = "github:numtide/devshell";
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs.nixpkgs.follows = "nixpkgs";
};
treefmt-nix = {
url = "github:numtide/treefmt-nix";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs =
{
self,
nixpkgs,
utils,
devshell,
treefmt-nix,
...
}@inputs:
{
overlays.default = import ./overlay.nix;
}
//
utils.lib.eachSystem
[
"x86_64-linux"
"i686-linux"
"aarch64-linux"
]
(
system:
let
lib = nixpkgs.lib;
pkgs = import nixpkgs {
inherit system;
overlays = [
# import oxalica's rust overlay for version specific Rust toolchains
(import inputs.rust-overlay)
# inject dependencies for our overlay.nix
(final: prev: {
inherit (inputs.typix.lib.${system}) buildTypstProject;
})
# import our overlay for the package in pkgs/
self.overlays.default
# add the devshell overlay for the devshell defined below
devshell.overlays.default
];
};
# universal formatter
treefmtEval = treefmt-nix.lib.evalModule pkgs ./treefmt.nix;
# rust target name of the `system`
rust-target = pkgs.pkgsStatic.stdenv.targetPlatform.rust.rustcTarget;
# parsed contents of Cargo.toml
cargoToml = lib.trivial.importTOML ./Cargo.toml;
# minimum rust version that we support according to Cargo.toml
msrv = cargoToml.package.rust-version;
# Rust distribution for our hostSystem
rust-toolchain-nixpkgs-current = pkgs.rust-bin.stable.${pkgs.rustc.version}.default.override {
extensions = [ "rust-src" ];
targets = [
rust-target
"wasm32-unknown-unknown"
"thumbv6m-none-eabi" # for no_std test
"i686-unknown-linux-musl" # to test if we can run on 32 Bit architectures
];
};
# Rust toolchain for the MSRV
rust-toolchain-msrv = pkgs.rust-bin.stable.${msrv}.default.override {
extensions = [ "rust-src" ];
targets = [
rust-target
"wasm32-unknown-unknown"
"thumbv6m-none-eabi" # for no_std test
"i686-unknown-linux-musl" # to test if we can run on 32 Bit architectures
];
};
in
{
# packages from `pkgs/`, injected into the `pkgs` via our `overlay.nix`
packages = pkgs.wasm-interpreter-pkgs;
# a devshell with all the necessary bells and whistles
devShells.default = (
pkgs.devshell.mkShell {
imports = [ "${devshell}/extra/git/hooks.nix" ];
name = "wasm-interpreter";
packagesFrom = [
self.packages.${system}.report
self.packages.${system}.requirements
self.packages.${system}.whitepaper
];
packages = with pkgs; [
stdenv.cc
coreutils
rust-toolchain-nixpkgs-current # also contains clippy
rust-analyzer
cargo-audit
cargo-expand
cargo-flamegraph
cargo-llvm-cov
cargo-outdated
cargo-show-asm
cargo-watch
critcmp # compare criterion.rs benchmark results
wabt
# utilities
treefmtEval.config.build.wrapper
];
env = [
{
name = "LLVM_COV";
value = self.packages.${system}.coverage.LLVM_COV;
}
{
name = "LLVM_PROFDATA";
value = self.packages.${system}.coverage.LLVM_PROFDATA;
}
];
git.hooks = {
enable = true;
pre-commit.text = "nix flake check '.?submodules=1'";
};
commands = [
{
name = "bench-against-main";
command = ''
cd -- "$PRJ_ROOT"
COMMON_BENCH_CMD=(cargo bench --package benchmark --bench general_purpose --)
GIT_WORKTREE_PATH=.benchmark-main-worktree
# add a git worktree for the main branch
if [ ! -d "$GIT_WORKTREE_PATH" ]
then
echo "creating new git worktree"
git worktree add --force "$GIT_WORKTREE_PATH" main
else
echo "found existing git worktree"
fi
# reset worktree to current origin main (but don't fetch)
git -C "$GIT_WORKTREE_PATH" reset --hard origin/main
# check if benchmark re-run is required
CURRENT_MAIN_COMMIT="$(git -C "$GIT_WORKTREE_PATH" rev-parse HEAD)"
LAST_BENCHMARKED_COMMIT="$(cat "$GIT_WORKTREE_PATH/target/LAST_BENCHMARKED_COMMIT" 2> /dev/null || true)"
# bench main as baseline if it wasn't already benched
if [ "$CURRENT_MAIN_COMMIT" == "$LAST_BENCHMARKED_COMMIT" ]
then
echo "skipping main baseline benchmark, the commit was already benched: $LAST_BENCHMARKED_COMMIT"
else
echo "benchmarking main baseline with commit: $CURRENT_MAIN_COMMIT"
pushd -- "$GIT_WORKTREE_PATH"
cargo clean
"''${COMMON_BENCH_CMD[@]}" --save-baseline benchmark-main.baseline
popd
echo "$CURRENT_MAIN_COMMIT" > "$GIT_WORKTREE_PATH/target/LAST_BENCHMARKED_COMMIT"
fi
# bench current workdir state
echo "benchmarking current workdir state"
"''${COMMON_BENCH_CMD[@]}" --save-baseline benchmark-current.baseline
# delete old current dev branch benchmark results
find "$GIT_WORKTREE_PATH/target/" -type d -name 'benchmark-current.baseline' -delete
# copy results from current to main worktree target/ dir
find target/ -type d -name 'benchmark-current.baseline' -exec cp --recursive --no-target-directory -- {} "$GIT_WORKTREE_PATH/"{} \;
# compare results
critcmp --target-dir "$GIT_WORKTREE_PATH"/target -- "benchmark-main.baseline" "benchmark-current.baseline"
'';
help = "benchmark the current HEAD against the main branch";
category = "benchmark";
}
{
name = "bench-flamegraph";
command = ''
cd -- "$PRJ_ROOT"
CARGO_PROFILE_BENCH_DEBUG=true cargo flamegraph --package benchmark --bench general_purpose -- --bench
'';
help = "run the benchmarks and draw a flamegraph";
category = "benchmark";
}
{
name = "requirements-export-excel";
command = ''
strictdoc export --output-dir "$PRJ_ROOT/requirements/export" \
--formats=excel \
"$PRJ_ROOT/requirements"
'';
help = "export the requirements to requirements/export";
category = "requirements";
}
{
name = "requirements-export-html";
command = ''
strictdoc export --output-dir "$PRJ_ROOT/requirements/export" \
--formats=html \
"$PRJ_ROOT/requirements"
'';
help = "export the requirements to requirements/export";
category = "requirements";
}
{
name = "requirements-web-server";
command = ''
strictdoc server "$PRJ_ROOT/requirements"
'';
help = "start the requirements editor web-ui";
category = "requirements";
}
{
name = "cargo-watch-doc";
command = ''
cargo watch --shell 'cargo doc --document-private-items'
'';
help = "start cargo watch loop for documentation";
}
{
name = "whitepaper-watch";
command = ''
typst watch --root "$PRJ_ROOT/pkgs/whitepaper" "$PRJ_ROOT/pkgs/whitepaper/main.typ"
'';
help = "start typst watch loop for the whitepaper";
}
{
name = "generate-testsuite-report";
# TODO maybe accept the name of the target branch as an argument and use it instead of `main`
command = ''
cd -- "$PRJ_ROOT"
TESTSUITE_SAVE=1 cargo test -- spec_tests --show-output
cp -- testsuite_results.json new.json
trap -- "rm --recursive --force -- \"$PRJ_ROOT/.main_clone\"" EXIT
git clone --depth=1 --single-branch --no-tags --recursive --branch=main -- "file://$PRJ_ROOT" "$PRJ_ROOT/.main_clone"
pushd -- .main_clone
TESTSUITE_SAVE=1 cargo test -- spec_tests --show-output
mv -- testsuite_results.json ../old.json
popd
cargo run --package=compare-testsuite-rs -- old.json new.json > testsuite_report.md
'';
help = "generates a comparison document for the official wasm testsuite w.r.t. project main branch";
}
];
}
);
# a simple devShell to compile with the MSRV
devShells.msrv = pkgs.mkShell {
inputsFrom = [ self.checks.${system}.wasm-interpreter-msrv ];
};
# a devShell for nightly based commands
#
#
# Run this to get advanced coverage information
# RUST_LOG=trace cargo llvm-cov --branch --doctests --release
#
#
# Run this to find unsafe `unsafe`:
# cargo miri nextest run
#
#
# Run this to find unused dependencies:
# cargo udeps --workspace --benches --tests
devShells.nightly = pkgs.mkShell {
inputsFrom = [ self.checks.${system}.wasm-interpreter-msrv ];
nativeBuildInputs = with pkgs; [
((rust-bin.selectLatestNightlyWith (toolchain: toolchain.default)).override {
extensions = [
"miri-preview"
"rust-analysis"
"rust-src"
];
})
cargo-llvm-cov
cargo-udeps
cargo-nextest
];
env = { inherit (pkgs.cargo-llvm-cov) LLVM_COV LLVM_PROFDATA; };
};
# for `nix fmt`
formatter = treefmtEval.config.build.wrapper;
# always check these
checks = {
# check that all files are properly formatted
formatting = treefmtEval.config.build.check self;
# check that the Minimum Supported Rust Version (MSRV) we promise does actually compile
wasm-interpreter-msrv = self.packages.${system}.wasm-interpreter.override {
# rustPlatform based on the MSRV we promise
rustPlatform = pkgs.makeRustPlatform {
cargo = rust-toolchain-msrv;
rustc = rust-toolchain-msrv;
};
# we do neither need documentation nor a JUnit test report
doDoc = false;
useNextest = false;
};
# check that the requirements can be parsed
requirements = pkgs.runCommand "check-requirement" { nativeBuildInputs = [ pkgs.strictdoc ]; } ''
shopt -s globstar
strictdoc passthrough ${./.}/requirements/**.sdoc
touch $out
'';
};
}
);
}