forked from noir-lang/noir
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flake.nix
285 lines (225 loc) · 8.67 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
{
description = "Build the Noir programming language";
# All of these inputs (a.k.a. dependencies) need to align with inputs we
# use so they use the `inputs.*.follows` syntax to reference our inputs
inputs = {
nixpkgs = {
url = "github:NixOS/nixpkgs/nixos-23.05";
};
flake-utils = {
url = "github:numtide/flake-utils";
};
flake-compat = {
url = "github:edolstra/flake-compat";
flake = false;
};
fenix = {
url = "github:nix-community/fenix";
inputs = {
nixpkgs.follows = "nixpkgs";
};
};
crane = {
url = "github:ipetkov/crane";
inputs = {
nixpkgs.follows = "nixpkgs";
flake-utils.follows = "flake-utils";
flake-compat.follows = "flake-compat";
};
};
};
outputs =
{ self, nixpkgs, crane, flake-utils, fenix, ... }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs {
inherit system;
};
rustToolchain = fenix.packages.${system}.fromToolchainFile {
file = ./rust-toolchain.toml;
sha256 = "sha256-R0F0Risbr74xg9mEYydyebx/z0Wu6HI0/KWwrV30vZo=";
};
craneLib = (crane.mkLib pkgs).overrideToolchain rustToolchain;
# The `self.rev` property is only available when the working tree is not dirty
GIT_COMMIT = if (self ? rev) then self.rev else "unknown";
GIT_DIRTY = if (self ? rev) then "false" else "true";
extraBuildInputs = [ ] ++ pkgs.lib.optionals pkgs.stdenv.isDarwin [
# Need libiconv and apple Security on Darwin. See https://github.com/ipetkov/crane/issues/156
pkgs.libiconv
pkgs.darwin.apple_sdk.frameworks.Security
];
environment = {
# We enable backtraces on any failure for help with debugging
RUST_BACKTRACE = "1";
# We download the Wasm version of `acvm_backend` in the barretenberg releases for the ACVM `blackbox_solver`
BARRETENBERG_ARCHIVE = pkgs.fetchurl {
url = "https://github.com/AztecProtocol/barretenberg/releases/download/barretenberg-v0.4.5/acvm_backend.wasm.tar.gz";
sha256 = "sha256-xONt5pTKWf/YbVnX/NXl/VNBbtKd+CP7CLkB1jf0RHw=";
};
};
# Configuration shared between builds
config = {
# x-release-please-start-version
version = "0.22.0";
# x-release-please-end
src = pkgs.lib.cleanSourceWith {
src = craneLib.path ./.;
# Custom filter with various file extensions that we rely upon to build packages
# Currently: `.nr`, `.sol`, `.sh`, `.json`, `.md` and `.wasm`
filter = path: type:
(builtins.match ".*\.(nr|sol|sh|json|md|wasm)$" path != null) || (craneLib.filterCargoSources path type);
};
# TODO(#1198): It'd be nice to include these flags when running `cargo clippy` in a devShell.
cargoClippyExtraArgs = "--all-targets -- -D warnings";
# TODO(#1198): It'd be nice to include this flag when running `cargo test` in a devShell.
cargoTestExtraArgs = "--workspace";
};
# Combine the environment and other configuration needed for Crane to build our Rust packages
nativeConfig = environment // config // {
nativeBuildInputs = [ ];
buildInputs = [ ] ++ extraBuildInputs;
};
# Combine the environmnet and other configuration needed for Crane to build our Wasm packages
wasmConfig = environment // config // {
CARGO_TARGET_DIR = "./target";
nativeBuildInputs = with pkgs; [
which
git
jq
rustToolchain
wasm-bindgen-cli
binaryen
];
buildInputs = [ ] ++ extraBuildInputs;
};
# Build *just* the cargo dependencies, so we can reuse all of that work between runs
native-cargo-artifacts = craneLib.buildDepsOnly (nativeConfig // {
pname = "nargo";
});
noir-wasm-cargo-artifacts = craneLib.buildDepsOnly (wasmConfig // {
pname = "noir_wasm";
});
noirc-abi-wasm-cargo-artifacts = craneLib.buildDepsOnly (wasmConfig // {
pname = "noirc_abi_wasm";
});
acvm-js-cargo-artifacts = craneLib.buildDepsOnly (wasmConfig // {
pname = "acvm_js";
});
nargo = craneLib.buildPackage (nativeConfig // {
pname = "nargo";
inherit GIT_COMMIT GIT_DIRTY;
cargoArtifacts = native-cargo-artifacts;
# We don't want to run tests because they don't work in the Nix sandbox
doCheck = false;
});
noir_wasm = craneLib.buildPackage (wasmConfig // {
pname = "noir_wasm";
inherit GIT_COMMIT GIT_DIRTY;
cargoArtifacts = noir-wasm-cargo-artifacts;
buildPhaseCargoCommand = ''
bash compiler/wasm/buildPhaseCargoCommand.sh release
'';
installPhase = ''
bash compiler/wasm/installPhase.sh
'';
# We don't want to run tests because they don't work in the Nix sandbox
doCheck = false;
});
noirc_abi_wasm = craneLib.buildPackage (wasmConfig // rec {
pname = "noirc_abi_wasm";
inherit GIT_COMMIT GIT_DIRTY;
cargoArtifacts = noirc-abi-wasm-cargo-artifacts;
cargoExtraArgs = "--package ${pname} --target wasm32-unknown-unknown";
buildPhaseCargoCommand = ''
bash tooling/noirc_abi_wasm/buildPhaseCargoCommand.sh release
'';
installPhase = ''
bash tooling/noirc_abi_wasm/installPhase.sh
'';
# We don't want to run tests because they don't work in the Nix sandbox
doCheck = false;
});
acvm_js = craneLib.buildPackage (wasmConfig // rec {
pname = "acvm_js";
inherit GIT_COMMIT GIT_DIRTY;
cargoArtifacts = acvm-js-cargo-artifacts;
cargoExtraArgs = "--package ${pname} --target wasm32-unknown-unknown";
buildPhaseCargoCommand = ''
bash acvm-repo/acvm_js/buildPhaseCargoCommand.sh release
'';
installPhase = ''
bash acvm-repo/acvm_js/installPhase.sh
'';
# We don't want to run tests because they don't work in the Nix sandbox
doCheck = false;
});
wasm-bindgen-cli = pkgs.callPackage ./wasm-bindgen-cli.nix {
rustPlatform = pkgs.makeRustPlatform {
rustc = rustToolchain;
cargo = rustToolchain;
};
};
in
{
# We use `checks` to run `cargo clippy` and `cargo fmt` since we disable checks in the primary derivations
checks = {
cargo-clippy = craneLib.cargoClippy (nativeConfig // {
pname = "noir";
inherit GIT_COMMIT GIT_DIRTY;
cargoArtifacts = native-cargo-artifacts;
});
cargo-fmt = craneLib.cargoFmt (nativeConfig // {
pname = "noir";
inherit GIT_COMMIT GIT_DIRTY;
cargoArtifacts = native-cargo-artifacts;
});
};
packages = {
default = nargo;
# Nix flakes cannot build more than one derivation in one command (see https://github.com/NixOS/nix/issues/5591)
# so we use `symlinkJoin` to build everything as the "all" package.
all = pkgs.symlinkJoin { name = "all"; paths = [ nargo noir_wasm noirc_abi_wasm acvm_js ]; };
all_wasm = pkgs.symlinkJoin { name = "all_wasm"; paths = [ noir_wasm noirc_abi_wasm acvm_js ]; };
# We also export individual packages to enable `nix build .#nargo -L`, etc.
inherit nargo;
inherit noir_wasm;
inherit noirc_abi_wasm;
inherit acvm_js;
# We expose the `*-cargo-artifacts` derivations so we can cache our cargo dependencies in CI
inherit native-cargo-artifacts;
inherit noir-wasm-cargo-artifacts;
inherit noirc-abi-wasm-cargo-artifacts;
inherit acvm-js-cargo-artifacts;
};
# Setup the environment to match the environment settings, the inputs from our checks derivations,
# and extra tooling via `nativeBuildInputs`
devShells.default = pkgs.mkShell (environment // {
inputsFrom = [
nargo
noir_wasm
noirc_abi_wasm
acvm_js
];
# Additional tools that weren't included as `nativeBuildInputs` of any of the derivations in `inputsFrom`
nativeBuildInputs = with pkgs; [
# Rust toolchain
rustToolchain
# Other tools
starship
yarn
nodejs-18_x
# Used by the `bb` binary
curl
gzip
# This ensures the right lldb is in the environment for running rust-lldb
llvmPackages.lldb
# Nix tools
nil
nixpkgs-fmt
];
shellHook = ''
eval "$(starship init bash)"
'';
});
});
}