-
Notifications
You must be signed in to change notification settings - Fork 144
/
Copy pathflake.nix
352 lines (344 loc) · 13.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
{
description = "o1js - TypeScript framework for zk-SNARKs and zkApps";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-23.11-small";
mina.url = "path:src/mina";
nixpkgs-mozilla.url = "github:mozilla/nixpkgs-mozilla";
nixpkgs-mozilla.flake = false;
describe-dune.url = "github:o1-labs/describe-dune";
describe-dune.inputs.nixpkgs.follows = "nixpkgs";
describe-dune.inputs.flake-utils.follows = "flake-utils";
dune-nix.url = "github:o1-labs/dune-nix";
dune-nix.inputs.nixpkgs.follows = "nixpkgs";
dune-nix.inputs.flake-utils.follows = "flake-utils";
flake-utils.url = "github:numtide/flake-utils";
};
outputs = { self, nixpkgs, flake-utils, ... }@inputs:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = (nixpkgs.legacyPackages."${system}".extend
(import inputs.nixpkgs-mozilla)
).extend inputs.mina.overlays.rust;
dune-nix = inputs.dune-nix.lib.${system};
describe-dune = inputs.describe-dune.defaultPackage.${system};
dune-description = pkgs.stdenv.mkDerivation {
pname = "dune-description";
version = "dev";
src = with pkgs.lib.fileset;
(toSource {
root = ./src/bindings;
fileset = unions [
./src/bindings/ocaml/dune
./src/bindings/ocaml/lib/dune
./src/bindings/ocaml/dune-project
./src/bindings/ocaml/jsoo_exports/dune
];
});
phases = [ "unpackPhase" "buildPhase" ];
buildPhase = ''
${describe-dune}/bin/describe-dune > $out
'';
};
desc = builtins.fromJSON (builtins.readFile dune-description);
allOcamlDeps_ = pkgs.lib.concatMap
(duneSpec:
pkgs.lib.concatMap (unitSpec: unitSpec.deps or [ ])
(duneSpec.units or [ ]))
desc;
allOcamlDeps =
builtins.map (d: builtins.head (pkgs.lib.splitString "." d))
allOcamlDeps_;
mina = inputs.mina.packages."${system}";
minaDeps_ =
builtins.intersectAttrs (pkgs.lib.genAttrs allOcamlDeps (_: { }))
mina.info.raw.deps.units;
minaDeps = builtins.attrNames (builtins.foldl'
(acc: pkg: acc // dune-nix.deps.packageDeps minaDeps_ "pkgs" pkg)
minaDeps_
(builtins.attrNames minaDeps_));
commonOverrides = {
DUNE_PROFILE = "dev";
buildInputs = [ mina.base-libs ] ++ mina.external-libs
++ pkgs.lib.attrVals minaDeps mina.pkgs;
};
info = dune-nix.info desc;
allDeps = dune-nix.allDeps info;
noTestSkipping = _: false;
prj_ = dune-nix.outputs' commonOverrides
(pkgs.lib.fileset.toSource {
root = ./src/bindings;
fileset = ./src/bindings/ocaml;
})
allDeps
info
noTestSkipping
prj;
prj = prj_ // {
pkgs = prj_.pkgs // {
__ocaml-js__ = prj_.pkgs.__ocaml-js__.overrideAttrs {
PREBUILT_KIMCHI_BINDINGS_JS_WEB =
"${mina.files.src-lib-crypto-kimchi_bindings-js-web}/src/lib/crypto/kimchi_bindings/js/web";
PREBUILT_KIMCHI_BINDINGS_JS_NODE_JS =
"${mina.files.src-lib-crypto-kimchi_bindings-js-node_js}/src/lib/crypto/kimchi_bindings/js/node_js";
};
};
};
rust-channel =
((pkgs.rustChannelOf
{
channel = "nightly";
date = "2023-09-01";
sha256 = "sha256-zek9JAnRaoX8V0U2Y5ssXVe9tvoQ0ERGXfUCUGYdrMA=";
}).rust.override
{
targets = [
"wasm32-unknown-unknown"
"x86_64-unknown-linux-gnu"
"aarch64-apple-darwin"
"x86_64-apple-darwin"
];
extensions = [ "rust-src" ];
});
rust-channel-direct = builtins.fetchTarball {
url = "https://static.rust-lang.org/dist/rust-std-1.72.0-wasm32-unknown-unknown.tar.gz";
sha256 =
if pkgs.stdenv.isDarwin
then "sha256:1mv8skl4l2q782741r1yakbf0y4q6v9358fm91r45gj97j20il1y"
else "sha256:07nxw3m4jpciaqfxn4b95bkhl58zxh3a0kpf5vprfx4r0zdrmql1";
};
toolchain = pkgs.symlinkJoin {
name = "toolchain";
paths = [ rust-channel "${rust-channel-direct}/rust-std-wasm32-unknown-unknown" ];
};
rust-platform = pkgs.makeRustPlatform
{
cargo = rust-channel;
rustc = rust-channel;
};
bindings-pkgs = with pkgs;
[
nodejs
nodePackages.npm
#nodePackages.prettier
typescript
nodePackages.typescript-language-server
rustup
wasm-pack
binaryen # provides wasm-opt
dune_3
] ++ commonOverrides.buildInputs;
inherit (nixpkgs) lib;
# All the submodules required by .gitmodules
submodules = map builtins.head (builtins.filter lib.isList
(map (builtins.match " path = (.*)")
(lib.splitString "\n" (builtins.readFile ./.gitmodules))));
# Warn about missing submodules
requireSubmodules =
let
ref = r: "[34;1m${r}[31;1m";
command = c: "[37;1m${c}[31;1m";
in
lib.warnIf
(
!builtins.all (x: x)
(map (x: builtins.pathExists ./${x} && builtins.readDir ./${x} != { })
submodules)
) ''
Some submodules are missing, you may get errors. Consider one of the following:
- run ${command "./pin.sh"} and use "${
ref "o1js"
}" flake ref, e.g. ${command "nix develop o1js"} or ${
command "nix build o1js"
};
- use "${ref "git+file://$PWD?submodules=1"}";
- use "${
ref "git+https://github.com/o1-labs/o1js?submodules=1"
}";
- use non-flake commands like ${command "nix-build"} and ${
command "nix-shell"
}.
'';
o1js-npm-deps = pkgs.buildNpmPackage
{
name = "o1js";
src = with pkgs.lib.fileset;
(toSource {
root = ./.;
fileset = unions [
./package.json
./package-lock.json
];
});
# If you get ERROR: npmDepsHash is out of date
# you can update the hash with `nix run o1js#update-npm-deps`.
# Failing that you can remove the hash from ./npmDepsHash and try again
# which should get an error message with the correct hash
# You can also just push and CI should suggest a fix which updates the hash
npmDepsHash = builtins.readFile ./npmDepsHash;
dontNpmBuild = true;
installPhase = ''
runHook preInstall
mkdir -p $out/lib
cp -r node_modules $out/lib
runHook postInstall
'';
};
#Rustup doesn't allow local toolchains to contain 'nightly' in the name
#so the toolchain is linked with the name nix and rustup is wrapped in a shellscript
#which calls the nix toolchain instead of the nightly one
rustupWrapper = with pkgs; writeShellApplication
{
name = "rustup";
#Skip check on darwin because shellcheck doesn't build
checkPhase = if pkgs.stdenv.isDarwin then "" else null;
text =
''
if [ "$1" = run ] && { [ "$2" = nightly-2023-09-01 ] || [[ "$2" =~ 1.72-x86_64* ]]; }
then
echo using nix toolchain
${rustup}/bin/rustup run nix "''${@:3}"
else
echo using plain rustup "$@"
${rustup}/bin/rustup "$@"
fi
'';
};
test-vectors = rust-platform.buildRustPackage {
src = pkgs.lib.sourceByRegex ./src/mina/src
[
"^lib(/crypto(/proof-systems(/.*)?)?)?$"
];
sourceRoot = "source/lib/crypto/proof-systems/poseidon/export_test_vectors";
patchPhase =
''
cp ${./src/mina/src/lib/crypto/proof-systems/Cargo.lock} .
'';
name = "export_test_vectors";
version = "0.1.0";
cargoSha256 = "";
CARGO_TARGET_DIR = "./target";
cargoLock = { lockFile = ./src/mina/src/lib/crypto/proof-systems/Cargo.lock; };
};
in
{
formatter = pkgs.nixfmt;
inherit mina;
devShells = {
# This seems to work better for macos
mina-shell = requireSubmodules inputs.mina.devShells."${system}".with-lsp;
default = requireSubmodules (pkgs.mkShell
(if pkgs.stdenv.isDarwin
# on macos use plain rustup
then { packages = bindings-pkgs; }
# on linux wrap rustup like in the derivation
else {
packages = [ rustupWrapper ] ++ bindings-pkgs;
shellHook = ''
RUSTUP_HOME=$(pwd)/.rustup
export RUSTUP_HOME
rustup toolchain link nix ${toolchain}
'';
}));
};
# TODO build from ./ocaml root, not ./. (after fixing a bug in dune-nix)
packages = {
inherit dune-description;
npm-deps = o1js-npm-deps;
o1js-bindings = requireSubmodules (pkgs.stdenv.mkDerivation {
name = "o1js_bindings";
src = with pkgs.lib.fileset;
(toSource {
root = ./.;
fileset = unions [
./src/mina
./src/bindings/scripts
./src/bindings/js
./src/bindings/crypto
./src/bindings/lib
./src/bindings/mina-transaction/gen/dune
(fileFilter (file: file.hasExt "js") ./src/bindings/mina-transaction)
./src/bindings/ocaml/lib
./src/bindings/ocaml/dune
./src/bindings/ocaml/dune-project
(fileFilter (file: file.hasExt "ml") ./src/bindings/ocaml)
./package.json
./package-lock.json
./src/bindings/ocaml/jsoo_exports
./dune-project
./.prettierrc.cjs
./src/build
./src/snarky.d.ts
];
});
inherit (inputs.mina.devShells."${system}".default)
PLONK_WASM_NODEJS
PLONK_WASM_WEB
MARLIN_PLONK_STUBS
;
PREBUILT_KIMCHI_BINDINGS_JS_WEB =
"${mina.files.src-lib-crypto-kimchi_bindings-js-web}/src/lib/crypto/kimchi_bindings/js/web";
PREBUILT_KIMCHI_BINDINGS_JS_NODE_JS =
"${mina.files.src-lib-crypto-kimchi_bindings-js-node_js}/src/lib/crypto/kimchi_bindings/js/node_js";
EXPORT_TEST_VECTORS = "${test-vectors}/bin/export_test_vectors";
SKIP_MINA_COMMIT = true;
JUST_BINDINGS = true;
buildInputs = (with pkgs;
[
rustupWrapper
bash
]) ++ bindings-pkgs;
patchPhase = ''
patchShebangs ./src/bindings/scripts/
patchShebangs ./src/bindings/crypto/test-vectors/
'';
buildPhase =
''
RUSTUP_HOME=$(pwd)/.rustup
export RUSTUP_HOME
rustup toolchain link nix ${toolchain}
cp -r ${o1js-npm-deps}/lib/node_modules/ .
mkdir -p src/bindings/compiled/node_bindings
echo '// this file exists to prevent TS from type-checking `o1js_node.bc.cjs`' \
> src/bindings/compiled/node_bindings/o1js_node.bc.d.cts
npm run build:update-bindings
mkdir -p $out/mina-transaction
pushd ./src/bindings
rm -rf ./compiled/_node_bindings
cp -Lr ./compiled $out
cp -Lr ./mina-transaction/gen $out/mina-transaction/
popd
'';
});
kimchi = pkgs.kimchi-rust-wasm;
ocaml-js = prj.pkgs.__ocaml-js__;
};
apps = {
update-npm-deps = {
type = "app";
program = "${pkgs.writeShellApplication
{ name = "update-npm-deps";
text =
''
${pkgs.prefetch-npm-deps}/bin/prefetch-npm-deps ./package-lock.json > npmDepsHash
'';
}}/bin/update-npm-deps";
};
update-bindings = {
type = "app";
program = "${pkgs.writeShellApplication
{ name = "update-bindings";
#Skip check on darwin because shellcheck doesn't build
checkPhase = if pkgs.stdenv.isDarwin then "" else null;
text =
''
cp -r ${self.packages."${system}".o1js-bindings}/* ./src/bindings
chmod +w -R src/bindings/compiled
MINA_COMMIT=$(git -C src/mina rev-parse HEAD)
echo "The mina commit used to generate the backends for node and web is" "$MINA_COMMIT" \
> src/bindings/MINA_COMMIT
'';
}}/bin/update-bindings";
};
};
});
}