-
Notifications
You must be signed in to change notification settings - Fork 0
/
flake.nix
312 lines (251 loc) · 10.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
{
description = "Experiments with Nix, Rust, Wasm, and npm.";
inputs = {
hacknix.url = "github:hackworthltd/hacknix";
nixpkgs.follows = "hacknix/nixpkgs";
flake-parts.url = "github:hercules-ci/flake-parts";
systems.url = "github:nix-systems/default";
gitignore.url = "github:hercules-ci/gitignore.nix";
gitignore.inputs.nixpkgs.follows = "nixpkgs";
flake-compat.url = "github:edolstra/flake-compat";
flake-compat.flake = false;
pre-commit-hooks-nix.url = "github:cachix/pre-commit-hooks.nix";
pre-commit-hooks-nix.inputs.nixpkgs.follows = "nixpkgs";
treefmt-nix.url = "github:numtide/treefmt-nix";
treefmt-nix.inputs.nixpkgs.follows = "nixpkgs";
crane.url = "github:ipetkov/crane";
crane.inputs.nixpkgs.follows = "nixpkgs";
rust-overlay.url = "github:oxalica/rust-overlay";
rust-overlay.inputs.nixpkgs.follows = "nixpkgs";
advisory-db.url = "github:rustsec/advisory-db";
advisory-db.flake = false;
};
outputs = inputs:
inputs.flake-parts.lib.mkFlake { inherit inputs; } {
systems = [ "x86_64-linux" "aarch64-darwin" ];
imports = [
inputs.treefmt-nix.flakeModule
inputs.pre-commit-hooks-nix.flakeModule
];
perSystem = { config, self', pkgs, lib, system, ... }:
let
pkgs = import inputs.nixpkgs {
inherit system;
overlays = [
inputs.hacknix.overlays.default
inputs.rust-overlay.overlays.default
];
};
rustToolchain =
(pkgs.rust-bin.fromRustupToolchainFile (./rust-toolchain.toml)).override {
extensions = [
"rust-src"
"rust-analyzer"
"clippy"
];
};
rustWasmToolchain = rustToolchain.override {
targets = [ "wasm32-unknown-unknown" ];
};
pname = "nrwn-workspace";
npm-scope = "hackworthltd";
cargoToml = builtins.fromTOML (builtins.readFile ./Cargo.toml);
cargoLock = builtins.fromTOML (builtins.readFile ./Cargo.lock);
wasm-bindgen-cli =
let
wasmBindgenCargoVersions = builtins.map ({ version, ... }: version) (builtins.filter ({ name, ... }: name == "wasm-bindgen") cargoLock.package);
wasmBindgenVersion = assert builtins.length wasmBindgenCargoVersions == 1; builtins.elemAt wasmBindgenCargoVersions 0;
in
pkgs.wasm-bindgen-cli.override {
version = wasmBindgenVersion;
hash = "sha256-1VwY8vQy7soKEgbki4LD+v259751kKxSxmo/gqE6yV0=";
cargoHash = "sha256-aACJ+lYNEU8FFBs158G1/JG8sc6Rq080PeKCMnwdpH0=";
};
inherit (cargoToml.workspace.package) version;
craneLib = (inputs.crane.mkLib pkgs).overrideToolchain rustToolchain;
craneLibWasm = (inputs.crane.mkLib pkgs).overrideToolchain rustWasmToolchain;
src = craneLib.cleanCargoSource (craneLib.path ./.);
cargoExtraArgs = "--frozen --offline";
commonArgs = {
inherit pname version src cargoExtraArgs;
strictDeps = true;
buildInputs = [
] ++ pkgs.lib.optionals pkgs.stdenv.isDarwin [
pkgs.libiconv
];
};
cargoArtifacts = craneLib.buildDepsOnly commonArgs;
individualCrateArgs = commonArgs // {
inherit cargoArtifacts;
doCheck = false;
};
wasmArgs = commonArgs // {
CARGO_BUILD_TARGET = "wasm32-unknown-unknown";
};
cargoArtifactsWasm = craneLibWasm.buildDepsOnly wasmArgs;
individualCrateArgsWasm = wasmArgs // {
cargoArtifacts = cargoArtifactsWasm;
doCheck = false;
};
# For the moment, it's not possible to run
# `wasm32-unknown-unknown` benchmarks, so we run them
# native.
mkBench = pkgName: crateArgs: (craneLib.mkCargoDerivation (crateArgs // {
doInstallCargoArtifacts = false;
# Use `bencher` format so we can use `cargo
# bench`-compatible tooling.
buildPhaseCargoCommand = ''
mkdir -p $out
cargo bench --bench=${pkgName} --frozen --offline -- --output-format=bencher | tee $out/bench.txt
'';
nativeBuildInputs = [
pkgs.coreutils
pkgs.gnuplot
];
})).overrideAttrs
(oldAttrs: {
requiredSystemFeatures = (oldAttrs.requiredSystemFeatures or [ ]) ++ [ "benchmark" ];
});
mkWasmNpmPackage = pkgName: crateArgs: craneLibWasm.mkCargoDerivation (crateArgs // {
doInstallCargoArtifacts = false;
buildPhaseCargoCommand = ''
WASM_PACK_CACHE=.wasm-pack-cache wasm-pack build --out-dir $out/pkg --scope "${npm-scope}" --mode no-install --target bundler --release ${pkgName} --profile release --frozen --offline
'';
nativeBuildInputs = [
pkgs.binaryen
pkgs.wasm-pack
wasm-bindgen-cli
];
});
mkWasmCheck = pkgName: crateArgs: craneLibWasm.mkCargoDerivation (crateArgs // {
doInstallCargoArtifacts = false;
buildPhaseCargoCommand = ''
WASM_PACK_CACHE=.wasm-pack-cache wasm-pack test --chrome --headless --mode no-install --release ${pkgName} --profile release --frozen --offline
'';
nativeBuildInputs = [
pkgs.binaryen
pkgs.wasm-pack
wasm-bindgen-cli
# We would prefer to use `geckodriver`, but it hangs on
# our tests in the Nix sandbox, for some reason.
pkgs.chromedriver
pkgs.chromium
];
});
greetCrateArgs = baseArgs: pname: baseArgs // {
inherit pname;
cargoExtraArgs = "${cargoExtraArgs} --package greet";
inherit (craneLib.crateNameFromCargoToml { cargoToml = ./greet/Cargo.toml; }) version;
};
golCrateArgs = baseArgs: pname: baseArgs // {
inherit pname;
cargoExtraArgs = "${cargoExtraArgs} --package gol";
inherit (craneLib.crateNameFromCargoToml { cargoToml = ./gol/Cargo.toml; }) version;
};
greet-crate = craneLib.buildPackage (greetCrateArgs individualCrateArgs "${pname}-greet");
greet-crate-wasm = craneLibWasm.buildPackage (greetCrateArgs individualCrateArgsWasm "${pname}-greet-wasm");
greet-crate-wasm-check = mkWasmCheck "greet" (greetCrateArgs individualCrateArgsWasm "${pname}-greet-wasm-check");
greet-crate-wasm-npm = mkWasmNpmPackage "greet" (greetCrateArgs individualCrateArgsWasm "${pname}-greet-wasm-npm");
gol-crate = craneLib.buildPackage (golCrateArgs individualCrateArgs "${pname}-gol");
gol-crate-wasm = craneLibWasm.buildPackage (golCrateArgs individualCrateArgsWasm "${pname}-gol-wasm");
gol-crate-wasm-check = mkWasmCheck "gol" (golCrateArgs individualCrateArgsWasm "${pname}-gol-wasm-check");
gol-crate-wasm-npm = mkWasmNpmPackage "gol" (golCrateArgs individualCrateArgsWasm "${pname}-gol-wasm-npm");
gol-crate-bench = mkBench "gol" (golCrateArgs individualCrateArgs "${pname}-gol");
inputsFrom = [
config.treefmt.build.devShell
config.pre-commit.devShell
];
devShellPackages = with pkgs; [
cargo-watch
critcmp
gnuplot
nil
];
in
{
checks = {
nwrn-workspace-clippy = craneLib.cargoClippy (commonArgs // {
inherit cargoArtifacts;
cargoClippyExtraArgs = "--all-targets -- --deny warnings";
});
nwrn-workspace-audit = craneLib.cargoAudit {
inherit (inputs) advisory-db;
inherit src;
};
} // (pkgs.lib.optionalAttrs (system == "x86_64-linux") {
inherit greet-crate-wasm-check;
inherit gol-crate-wasm-check;
});
packages = {
default = gol-crate-wasm-npm;
inherit greet-crate gol-crate;
inherit greet-crate-wasm gol-crate-wasm;
inherit greet-crate-wasm-npm gol-crate-wasm-npm;
} // (pkgs.lib.optionalAttrs (system == "x86_64-linux") {
inherit gol-crate-bench;
});
treefmt.config = {
projectRootFile = "flake.nix";
programs = {
nixpkgs-fmt.enable = true;
rustfmt.enable = true;
};
};
pre-commit = {
check.enable = true;
settings = {
hooks = {
treefmt.enable = true;
actionlint.enable = true;
};
};
};
devShells.default = craneLib.devShell {
inherit inputsFrom;
packages = devShellPackages;
};
devShells.wasm = craneLibWasm.devShell {
inherit inputsFrom;
packages = devShellPackages ++ (with pkgs; [
binaryen
# Prefer `geckodriver` to `chromedriver` for interactive
# development.
geckodriver
nodejs_20
wasm-pack
] ++ [
wasm-bindgen-cli
]);
};
};
flake =
let
pkgs = import inputs.nixpkgs
{
system = "x86_64-linux";
overlays = [
inputs.hacknix.overlays.default
inputs.rust-overlay.overlays.default
];
};
in
{
hydraJobs = {
inherit (inputs.self) checks;
inherit (inputs.self) packages;
inherit (inputs.self) devShells;
required = pkgs.releaseTools.aggregate {
name = "required-nix-ci";
constituents = builtins.map builtins.attrValues (with inputs.self.hydraJobs; [
packages.x86_64-linux
packages.aarch64-darwin
checks.x86_64-linux
checks.aarch64-darwin
]);
meta.description = "Required Nix CI builds";
};
};
ciJobs = pkgs.lib.flakes.recurseIntoHydraJobs inputs.self.hydraJobs;
};
};
}