-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathflake.nix
445 lines (393 loc) · 16.5 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
{
description = "RCL";
# Pin to a Nixpkgs version that has the same rustc as in rust-toolchain.toml.
inputs.nixpkgs.url = "nixpkgs/dfcffbd74fd6f0419370d8240e445252a39f4d10";
inputs.rust-overlay.url = "github:oxalica/rust-overlay";
inputs.rust-overlay.inputs.nixpkgs.follows = "nixpkgs";
outputs = { self, nixpkgs, rust-overlay }:
let
supportedSystems = [ "x86_64-linux" "x86_64-darwin" "aarch64-linux" "aarch64-darwin" ];
# Ridiculous boilerplate required to make flakes somewhat usable.
forEachSystem = f:
nixpkgs.lib.zipAttrsWith
(name: values: builtins.foldl' (x: y: x // y) {} values)
(map
(k: builtins.mapAttrs (name: value: { "${k}" = value; }) (f k))
supportedSystems
);
# The source of truth for the version number is Cargo.rcl.
cargoToml = builtins.fromTOML (builtins.readFile ./Cargo.toml);
version = cargoToml.package.version;
in
forEachSystem (system:
let
name = "rcl";
overlays = [ rust-overlay.overlays.default ];
pkgs = import nixpkgs { inherit overlays system; };
python = pkgs.python311.override {
packageOverrides = self: super: {
# This package is not in Nixpkgs, define it here.
# I should consider upstreaming it.
types-pygments = self.buildPythonPackage rec {
pname = "types-Pygments";
version = "2.14.0.0";
format = "setuptools";
nativeBuildInputs = with self; [
types-setuptools
types-docutils
];
src = self.fetchPypi {
inherit pname version;
hash = "sha256-G5R3cD3VeyzCU6Frii7WppK/zDO7OQWdEAiqnLA/xng=";
};
};
# Build a custom version of Pygments that has our lexer enabled.
# This enables MkDocs to highlight RCL code blocks.
pygments = super.pygments.overridePythonAttrs (attrs: {
postPatch = (attrs.postPatch or "") +
''
# Copy our RCL lexer into the Pygments source tree, next to
# the other lexers.
cp ${./grammar/pygments/rcl.py} pygments/lexers/rcl.py
# Regenerate pygments/lexers/_mapping.py, which contains all
# supported languages.
python scripts/gen_mapfiles.py
'';
});
};
};
pythonEnv = python.withPackages (ps: [
ps.mkdocs
ps.mypy
ps.pygments
ps.types-pygments
# These two need to be in here for PyCharm to be able to find
# dependencies from our fake virtualenv.
ps.pip
ps.setuptools
]);
# Define a custom toolchain from our toolchain file. For most
# derivations we don't use it, we instead use the rustc from Nixpkgs,
# to avoid unnecessary fetching/rebuilding. But for WASM, we need a
# specific nightly toolchain with support for this target.
rustWasm = pkgs.rust-bin.selectLatestNightlyWith (toolchain:
toolchain.default.override {
extensions = [ "rust-src" ];
targets = [ "wasm32-unknown-unknown" ];
}
);
rustSources = pkgs.lib.sourceFilesBySuffices ./. [
".rs"
"Cargo.lock"
"Cargo.toml"
];
treeSitterSources = pkgs.lib.sourceFilesBySuffices ./grammar/tree-sitter-rcl [
".json"
".txt"
".scm"
"Cargo.toml"
"grammar.js"
];
pythonSources = pkgs.lib.sourceFilesBySuffices ./. [ ".py" ".pyi" ];
rclTomlSources = pkgs.lib.sourceFilesBySuffices ./. [ ".rcl" ".toml" ];
goldenSources = ./golden;
treeSitterRcl = pkgs.stdenv.mkDerivation {
pname = "tree-sitter-rcl";
inherit version;
src = treeSitterSources;
nativeBuildInputs = [ pkgs.nodejs pkgs.tree-sitter ];
doCheck = true;
buildPhase = "tree-sitter generate";
checkPhase =
''
# Tree sitter wants to write to ~/.config by default, but that
# does not exist in the sandbox. Give it a directory to write to.
mkdir tree-sitter-home
export TREE_SITTER_DIR=tree-sitter-home
export TREE_SITTER_LIBDIR=tree-sitter-home
tree-sitter generate --build
tree-sitter test
'';
installPhase =
''
mkdir -p $out/lib
cp tree-sitter-home/rcl.so $out/lib
mkdir -p $out/dev/bindings
cp -r bindings/rust $out/dev/bindings
cp -r src $out/dev
cp -r queries $out/dev
cp Cargo.toml $out/dev
'';
};
rustSourcesAll = pkgs.runCommand "rcl-src-all" {}
''
mkdir -p $out/grammar/tree-sitter-rcl/src/tree_sitter
cp -r ${treeSitterRcl}/dev/src/{parser.c,node-types.json} $out/grammar/tree-sitter-rcl/src
cp -r ${treeSitterRcl}/dev/src/tree_sitter/parser.h $out/grammar/tree-sitter-rcl/src/tree_sitter/parser.h
cp -r ${treeSitterRcl}/dev/queries $out/grammar/tree-sitter-rcl
cp -r ${rustSources}/* $out
'';
rcl = pkgs.rustPlatform.buildRustPackage rec {
inherit name version;
src = rustSources;
cargoLock.lockFile = ./Cargo.lock;
};
coverageBuild = rcl.overrideAttrs (old: {
name = "rcl-coverage";
buildType = "debug";
RUSTFLAGS = "-C instrument-coverage -C link-dead-code -C debug-assertions";
# The tests already get executed by default when we build a Rust
# package, and because of the RUSTFLAGS we set, the tests already
# produce coverage too. We just need to copy those files into the
# output such that the coverage report can include them. We also
# need the test binaries for this, and the Rust installPhase sets
# $releaseDir to the target directory.
postInstall =
''
mkdir -p $out/prof
cp *.profraw $out/prof
find $releaseDir/deps \
-maxdepth 1 \
-type f \
-executable \
-print0 | xargs -0 cp --target-directory=$out/bin
'';
});
pyrcl = pkgs.rustPlatform.buildRustPackage rec {
inherit version;
name = "pyrcl";
src = rustSources;
nativeBuildInputs = [python];
cargoLock.lockFile = ./Cargo.lock;
buildAndTestSubdir = "pyrcl";
postInstall =
''
mv $out/lib/libpyrcl.so $out/lib/rcl.so
cp ${./pyrcl}/rcl.pyi $out/lib/rcl.pyi
'';
};
rcl-wasm = pkgs.rustPlatform.buildRustPackage rec {
inherit version;
name = "rcl-wasm";
src = rustSources;
cargoLock.lockFile = ./Cargo.lock;
buildAndTestSubdir = "wasm";
doCheck = false; # We already test the non-wasm build.
nativeBuildInputs = [
pkgs.binaryen
pkgs.esbuild
pkgs.wasm-bindgen-cli
rustWasm
];
buildPhase =
''
cargo build \
--manifest-path wasm/Cargo.toml \
--profile=release-wasm \
--target=wasm32-unknown-unknown \
-Z build-std=std,panic_abort \
-Z build-std-features=panic_immediate_abort
wasm-opt -Oz \
target/wasm32-unknown-unknown/release-wasm/rcl_wasm.wasm \
--output target/rcl.wasm
wasm-bindgen \
--out-dir $out \
--target no-modules \
--no-typescript \
target/rcl.wasm
cat ${./wasm/src/rcl_dom.js} $out/rcl.js | esbuild --minify > $out/bundle.js
mv $out/bundle.js $out/rcl.js
'';
installPhase = "echo 'Skipping default install phase.'";
};
website = pkgs.stdenv.mkDerivation {
pname = "rcl-website";
inherit version;
src = ./website;
nativeBuildInputs = [ pkgs.brotli ];
doCheck = false;
buildPhase =
''
mkdir -p $out
cp $src/* $out
# Put the artifacts at an input-addressible path, so we don't
# have issues with stale cache entries. We can use anything that
# changes on release, and one of those things is the Nix base32
# hash of the wasm module. This has the advantage that it remains
# unchanged if we change the webpage. 8 characters is probably
# enough to avoid collisions. To make the path less cryptic, we
# also put the human-readable version name in there.
hash="v${version}-$(basename ${rcl-wasm} | cut --bytes 1-8)"
mkdir -p $out/$hash
cp ${rcl-wasm}/* $out/$hash
sed --in-place "s|rcl\.js|$hash/rcl.js|" $out/index.html
# Pre-compress all assets for use with brotli_static in Nginx.
for f in $(find $out -type f); do brotli -9 $f; done
'';
};
fuzzers = pkgs.rustPlatform.buildRustPackage rec {
name = "rcl-fuzzers";
inherit version;
src = rustSourcesAll;
cargoLock.lockFile = ./Cargo.lock;
buildAndTestSubdir = "fuzz";
};
fuzzers-coverage = fuzzers.overrideAttrs (old: {
name = "rcl-fuzzers-coverage";
buildType = "debug";
RUSTFLAGS = "-C instrument-coverage -C link-dead-code -C debug-assertions";
});
in
rec {
devShells.default = pkgs.mkShell {
name = "rcl";
nativeBuildInputs = [
# For consistency we could take `python.pkgs.black`, but it
# rebuilds half the Python universe, so instead we take the
# cached version that does not depend on our patched pygments.
pkgs.python311Packages.black
pkgs.binaryen
pkgs.esbuild
pkgs.grcov
pkgs.maturin
pkgs.nodejs # Required for tree-sitter.
pkgs.rustup
pkgs.tree-sitter
pkgs.wasm-bindgen-cli
pythonEnv
];
# Put something in .venv that looks enough like a traditional
# virtualenv that it works with PyCharm autocomplete and jump to
# definition and such.
shellHook =
''
mkdir -p .venv/bin
ln -sf ${pythonEnv}/bin/python .venv/bin/python
cat <<EOF > .venv/pyvenv.cfg
home = ${pythonEnv}/bin
executable = ${pythonEnv}/bin/python
version = ${pythonEnv.python.version}
include-system-site-packages = false
EOF
'';
};
checks = rec {
inherit fuzzers;
debugBuild = packages.default.overrideAttrs (old: {
name = "check-test";
buildType = "debug";
RUSTFLAGS = "-C debug-assertions";
});
golden = pkgs.runCommand
"check-golden"
{ buildInputs = [ python ]; }
''
RCL_BIN=${debugBuild}/bin/rcl python3 ${goldenSources}/run.py
touch $out
'';
examples = pkgs.runCommand
"check-examples"
{ buildInputs = []; }
''
cd ${./examples}/;
for f in *.rcl; do
${debugBuild}/bin/rcl evaluate $f
done
touch $out
'';
grammar = pkgs.runCommand
"check-grammar"
{ buildInputs = [ pkgs.bison ]; }
''
bison -Wcounterexamples,error=all ${./grammar/bison/grammar.y} --output $out
'';
fmtRust = pkgs.runCommand
"check-fmt-rust"
{ buildInputs = [ pkgs.cargo pkgs.rustfmt ]; }
''
cargo fmt --manifest-path ${rustSources}/Cargo.toml -- --check
touch $out
'';
fmtPython = pkgs.runCommand
"check-fmt-python"
{ buildInputs = [ pkgs.black ]; }
''
black --check --diff ${pythonSources}
touch $out
'';
fmtRcl = pkgs.runCommand
"check-fmt-rcl"
{ buildInputs = [ debugBuild ]; }
''
rcl format --check ${rclTomlSources}/**.rcl | tee $out
'';
buildRcl = pkgs.runCommand
"check-rcl-build"
{ buildInputs = [ debugBuild ]; }
''
rcl build --check --directory ${rclTomlSources} | tee $out
'';
typecheckPython = pkgs.runCommand
"check-typecheck-python"
{ buildInputs = [ pythonEnv ]; }
''
# We split this check in two because there are multiple modules
# named `rcl`, and they conflict if we typecheck in one go.
mypy --strict --exclude pyrcl ${pythonSources}
mypy --strict ${pythonSources}/pyrcl
touch $out
'';
pyrclTest = pkgs.runCommand
"check-pyrcl-test"
{ buildInputs = [ pkgs.python3 ]; }
''
cd ${./pyrcl}
PYTHONPATH=${pyrcl}/lib python3 ./test.py
touch $out
'';
};
packages = {
inherit fuzzers-coverage rcl pyrcl treeSitterRcl website;
default = rcl;
wasm = rcl-wasm;
coverage = pkgs.runCommand
"rcl-coverage"
{ buildInputs = [ python pkgs.grcov ]; }
''
export bintools=${pkgs.rustc.llvmPackages.bintools-unwrapped}/bin
# Run the golden tests to generate the .profraw files.
RCL_BIN=${coverageBuild}/bin/rcl python3 ${goldenSources}/run.py
# Also run `rcl build` to make sure we cover that part of the application.
${coverageBuild}/bin/rcl build --check --directory ${rclTomlSources}
# Copy in the .profraw files from the tests.
cp ${coverageBuild}/prof/*.profraw .
# During the build, source file names get included as
# "source/src/lib.rs" etc. But when grcov runs, even if we
# provide --source-dir, inside that source dir is only a
# directory "src", not "sources/src", so it fails to find any
# files. To work around that, make a directory link "sources".
ln -s ${rustSources} source
grcov . \
--source-dir source \
--binary-path ${coverageBuild}/bin \
--excl-line '(#\[derive|unreachable!|panic!|std::process::exit|debug_assert_ne!)\(' \
--excl-start 'coverage:off' \
--excl-stop 'coverage:on' \
--llvm-path $bintools \
--prefix-dir source \
--llvm \
--output-types html \
--output-path $out
# Also output the raw LLVM summary. This can be useful for
# diffing, or for debugging to identify which files are traced.
$bintools/llvm-profdata merge -sparse *.profraw -o rcl.profdata
$bintools/llvm-cov report \
--instr-profile=rcl.profdata \
--ignore-filename-regex=/cargo-vendor-dir \
${coverageBuild}/bin/rcl* \
> $out/summary.txt
'';
};
}
);
}