-
Notifications
You must be signed in to change notification settings - Fork 121
/
flake.nix
171 lines (168 loc) · 6.82 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
{
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" ];
extensions = [ "rust-src" ];
});
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"
}.
'';
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 {
shellHook =
''
RUSTUP_HOME=$(pwd)/.rustup
export RUSTUP_HOME
rustup toolchain link nix ${rust-channel}
'';
packages = with pkgs;
[ nodejs
nodePackages.npm
typescript
nodePackages.typescript-language-server
#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
(writeShellApplication
{ name = "rustup";
text =
''
if [ "$1" = run ] && [ "$2" = nightly-2023-09-01 ]
then
${rustup}/bin/rustup run nix "''${@:3}"
else
${rustup}/bin/rustup "$@"
fi
'';
}
)
rustup
wasm-pack
binaryen # provides wasm-opt
dune_3
] ++ commonOverrides.buildInputs ;
});
};
# TODO build from ./ocaml root, not ./. (after fixing a bug in dune-nix)
packages = {
kim = pkgs.kimchi-rust-wasm;
inherit dune-description;
bindings = prj.pkgs.o1js_bindings;
ocaml-js = prj.pkgs.__ocaml-js__;
default = pkgs.buildNpmPackage
{ name = "o1js";
src = ./.;
npmDepsHash = "sha256-++MTGDUVBccYN8LA2Xb0FkbrZ14ZyVCrDPESXa52AwQ=";
# TODO ideally re-build bindings here
};
};
});
}