-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathflake.nix
More file actions
203 lines (183 loc) · 6.26 KB
/
flake.nix
File metadata and controls
203 lines (183 loc) · 6.26 KB
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
{
description = "Ix Nix flake (Lean4 + C + Rust)";
nixConfig = {
extra-substituters = [
"https://cache.garnix.io"
];
extra-trusted-public-keys = [
"cache.garnix.io:CTFPyKSLcx5RMJKfLo5EEPUObbA78b0YQ2DTCJXqr9g="
];
};
inputs = {
# System packages, follows lean4-nix so we stay in sync
nixpkgs.follows = "lean4-nix/nixpkgs";
# Lean 4 & Lake
lean4-nix.url = "github:lenianiva/lean4-nix";
# Helper: flake-parts for easier outputs
flake-parts.url = "github:hercules-ci/flake-parts";
# Rust-related inputs
fenix = {
url = "github:nix-community/fenix";
# Follow lean4-nix nixpkgs so we stay in sync
inputs.nixpkgs.follows = "lean4-nix/nixpkgs";
};
crane.url = "github:ipetkov/crane";
# Blake3 Rust bindings for Lean
blake3-lean = {
url = "github:argumentcomputer/Blake3.lean";
# System packages, follows lean4-nix so we stay in sync
inputs.lean4-nix.follows = "lean4-nix";
};
};
outputs = inputs @ {
nixpkgs,
flake-parts,
lean4-nix,
fenix,
crane,
blake3-lean,
...
}:
flake-parts.lib.mkFlake {inherit inputs;} {
# Systems we want to build for
systems = [
"aarch64-darwin"
"aarch64-linux"
"x86_64-darwin"
"x86_64-linux"
];
perSystem = {
system,
pkgs,
...
}: let
# Pins the Rust toolchain
rustToolchain = fenix.packages.${system}.fromToolchainFile {
file = ./rust-toolchain.toml;
sha256 = "sha256-sqSWJDUxc+zaz1nBWMAJKTAGBuGWP25GCftIOlCEAtA=";
};
# Rust package
craneLib = (crane.mkLib pkgs).overrideToolchain rustToolchain;
src = craneLib.cleanCargoSource ./.;
craneArgs = {
inherit src;
strictDeps = true;
# build.rs uses LEAN_SYSROOT to locate lean/lean.h for bindgen
LEAN_SYSROOT = "${pkgs.lean.lean-all}";
# bindgen needs libclang to parse C headers
LIBCLANG_PATH = "${pkgs.llvmPackages.libclang.lib}/lib";
buildInputs =
[]
++ pkgs.lib.optionals pkgs.stdenv.isDarwin [
# Additional darwin specific inputs can be set here
pkgs.libiconv
];
};
cargoArtifacts = craneLib.buildDepsOnly craneArgs;
# Test build: parallel + test-ffi (only used by ixTest)
rustPkg = craneLib.buildPackage (craneArgs
// {
inherit cargoArtifacts;
cargoExtraArgs = "--locked --features parallel,test-ffi";
});
# Release build without test-ffi (for distribution)
rustPkgRelease = craneLib.buildPackage (craneArgs
// {
inherit cargoArtifacts;
cargoExtraArgs = "--locked --features parallel";
});
# Lake package
lake2nix = pkgs.callPackage lean4-nix.lake {};
lakeDeps = lake2nix.buildDeps {
src = ./.;
depOverrideDeriv = {
Blake3 = blake3-lean.packages.${system}.rust;
};
};
# Shared Lake build args: patches out the Cargo build (Crane handles it)
mkLakeBuildArgs = rustLib: {
inherit lakeDeps;
src = ./.;
# Don't build the `ix_rs` static lib with Lake, since we build it with Crane
postPatch = ''
substituteInPlace lakefile.lean --replace-fail 'proc { cmd := "cargo"' '--proc { cmd := "cargo"'
'';
# Symlink the Crane-built static lib to where Lake expects it
postConfigure = ''
mkdir -p target/release
ln -s ${rustLib}/lib/libix_rs.a target/release/
'';
buildInputs = [pkgs.gmp pkgs.lean.lean-all pkgs.rsync];
};
# Release build args (no test-ffi symbols)
lakeBuildArgs = mkLakeBuildArgs rustPkgRelease;
# Test build args (includes test-ffi symbols)
lakeTestBuildArgs = mkLakeBuildArgs rustPkg;
ixLib = lake2nix.mkPackage (lakeBuildArgs
// {
name = "Ix";
buildLibrary = true;
});
lakeBinArgs =
lakeBuildArgs
// {
lakeArtifacts = ixLib;
# Binaries that import Ix.Meta need .olean files at runtime via LEAN_PATH
installArtifacts = true;
};
leanPath = pkgs.lib.concatStringsSep ":" (
map (d: "${d}/.lake/build/lib/lean") ([ixLib] ++ builtins.attrValues lakeDeps)
);
wrapBin = drv:
pkgs.runCommand drv.name {nativeBuildInputs = [pkgs.makeWrapper];} ''
mkdir -p $out/bin
for f in ${drv}/bin/*; do
[ -x "$f" ] || continue
makeWrapper "$f" "$out/bin/$(basename "$f")" \
--set LEAN_SYSROOT "${pkgs.lean.lean-all}" \
--set LEAN_PATH "${drv}/.lake/build/lib/lean:${leanPath}"
done
'';
ixCLI = wrapBin (lake2nix.mkPackage (lakeBinArgs // {name = "ix";}));
# Test binary links rustPkg (with test-ffi) instead of rustPkgRelease
ixTest = wrapBin (lake2nix.mkPackage (lakeTestBuildArgs
// {
name = "IxTests";
installArtifacts = true;
}));
ZKVotingProver = wrapBin (lake2nix.mkPackage (lakeBinArgs
// {
name = "Apps.ZKVoting.Prover";
installArtifacts = true;
}));
in {
# Lean overlay
_module.args.pkgs = import nixpkgs {
inherit system;
overlays = [(lean4-nix.readToolchainFile ./lean-toolchain)];
};
packages = {
default = ixLib;
ix = ixCLI;
test = ixTest;
zkv-prover = ZKVotingProver // {meta.mainProgram = "Apps-ZKVoting-Prover";};
};
# Provide a unified dev shell with Lean + Rust
devShells.default = pkgs.mkShell {
# Add libclang for FFI with rust-bindgen
LIBCLANG_PATH = "${pkgs.llvmPackages.libclang.lib}/lib";
packages = with pkgs; [
pkg-config
openssl
clang
rustToolchain
rust-analyzer
lean.lean-all # Includes Lean compiler, lake, stdlib, etc.
cargo-deny
valgrind
];
};
formatter = pkgs.alejandra;
};
};
}