diff --git a/flake.lock b/flake.lock index c99d876..2b3144d 100644 --- a/flake.lock +++ b/flake.lock @@ -1,5 +1,20 @@ { "nodes": { + "crane": { + "locked": { + "lastModified": 1774313767, + "narHash": "sha256-hy0XTQND6avzGEUFrJtYBBpFa/POiiaGBr2vpU6Y9tY=", + "owner": "ipetkov", + "repo": "crane", + "rev": "3d9df76e29656c679c744968b17fbaf28f0e923d", + "type": "github" + }, + "original": { + "owner": "ipetkov", + "repo": "crane", + "type": "github" + } + }, "fenix": { "inputs": { "nixpkgs": [ @@ -72,6 +87,7 @@ }, "root": { "inputs": { + "crane": "crane", "fenix": "fenix", "flake-parts": "flake-parts", "nixpkgs": "nixpkgs" diff --git a/flake.nix b/flake.nix index 128bb0c..dda35ea 100644 --- a/flake.nix +++ b/flake.nix @@ -18,12 +18,14 @@ url = "github:nix-community/fenix"; inputs.nixpkgs.follows = "nixpkgs"; }; + crane.url = "github:ipetkov/crane"; }; outputs = inputs @ { self, nixpkgs, flake-parts, fenix, + crane, }: flake-parts.lib.mkFlake {inherit inputs;} { @@ -68,18 +70,20 @@ fetchSubmodules = true; }; + craneLib = (crane.mkLib pkgs).overrideToolchain rustToolchain; + zisk-toolchain = pkgs.callPackage ./pkgs/zisk-toolchain.nix {}; cargo-zisk = pkgs.callPackage ./pkgs/cargo-zisk.nix { - inherit ziskSrc proofmanSrc zisk-toolchain; + inherit craneLib ziskSrc proofmanSrc zisk-toolchain; }; ziskemu = pkgs.callPackage ./pkgs/ziskemu.nix { - inherit ziskSrc proofmanSrc; + inherit craneLib ziskSrc proofmanSrc; }; proving-key = pkgs.callPackage ./pkgs/proving-key.nix { inherit cargo-zisk; }; zisk-home = pkgs.callPackage ./pkgs/zisk-home.nix { - inherit cargo-zisk zisk-toolchain ziskemu; + inherit cargo-zisk zisk-toolchain ziskemu craneLib proofmanSrc; ziskSrc = ziskSrcLite; }; rustup-shim = pkgs.callPackage ./pkgs/rustup-shim.nix { diff --git a/pkgs/cargo-zisk.nix b/pkgs/cargo-zisk.nix index ea38b24..5a8e910 100644 --- a/pkgs/cargo-zisk.nix +++ b/pkgs/cargo-zisk.nix @@ -1,61 +1,46 @@ { lib, stdenv, - rustPlatform, pkgs, makeWrapper, + craneLib, ziskSrc, proofmanSrc, zisk-toolchain, }: let - common = import ./common.nix {inherit lib stdenv pkgs proofmanSrc;}; + common = import ./common.nix {inherit lib stdenv pkgs craneLib ziskSrc proofmanSrc;}; in - rustPlatform.buildRustPackage rec { - pname = "cargo-zisk"; - version = "0.16.1"; + craneLib.buildPackage (common.commonArgs + // { + inherit (common) cargoArtifacts; + pname = "cargo-zisk"; - src = ziskSrc; - cargoHash = "sha256-DTD9NeTfhatR9gCIaZXoIpiXLyY0/hiauSSxsc9FZq8="; + cargoExtraArgs = "-p cargo-zisk -p zisk-core -p zisk-distributed-coordinator -p zisk-distributed-worker"; - cargoBuildFlags = [ - "--package" - "cargo-zisk" - "--package" - "zisk-core" - "--package" - "zisk-distributed-coordinator" - "--package" - "zisk-distributed-worker" - ]; - - postPatch = - common.pil2StarkPostPatch - + '' - # Remove rustup-specific +zisk arguments (we'll use RUSTC env var instead) + postPatch = '' + # Remove rustup-specific +zisk arguments (we use RUSTC env var instead) sed -i 's/\["+zisk", "build"\]/["build"]/g' cli/src/commands/build.rs sed -i 's/\["+zisk", "run"\]/["run"]/g' cli/src/commands/run.rs sed -i 's/\["+zisk", "build"\]/["build"]/g' ziskbuild/src/command.rs ''; - nativeBuildInputs = - common.nativeBuildInputs - ++ [ - pkgs.pkgsCross.riscv64-embedded.buildPackages.gcc - makeWrapper - ]; - - inherit (common) buildInputs LIBCLANG_PATH LD_LIBRARY_PATH; + nativeBuildInputs = + common.commonArgs.nativeBuildInputs + ++ [ + pkgs.pkgsCross.riscv64-embedded.buildPackages.gcc + makeWrapper + ]; - postInstall = '' - wrapProgram $out/bin/cargo-zisk \ - --set RUSTC "${zisk-toolchain}/bin/rustc" \ - --prefix LD_LIBRARY_PATH : "${common.LD_LIBRARY_PATH}" + postInstall = '' + wrapProgram $out/bin/cargo-zisk \ + --set RUSTC "${zisk-toolchain}/bin/rustc" \ + --prefix LD_LIBRARY_PATH : "${common.commonArgs.LD_LIBRARY_PATH}" - for bin in riscv2zisk zisk-coordinator zisk-worker; do - if [ -f "$out/bin/$bin" ]; then - wrapProgram $out/bin/$bin \ - --prefix LD_LIBRARY_PATH : "${common.LD_LIBRARY_PATH}" - fi - done - ''; - } + for bin in riscv2zisk zisk-coordinator zisk-worker; do + if [ -f "$out/bin/$bin" ]; then + wrapProgram $out/bin/$bin \ + --prefix LD_LIBRARY_PATH : "${common.commonArgs.LD_LIBRARY_PATH}" + fi + done + ''; + }) diff --git a/pkgs/common.nix b/pkgs/common.nix index eedef74..4e8d5fa 100644 --- a/pkgs/common.nix +++ b/pkgs/common.nix @@ -1,66 +1,86 @@ -# Shared build configuration for zisk packages that depend on pil2-proofman +# Shared Crane build configuration for zisk packages { lib, stdenv, pkgs, + craneLib, + ziskSrc, proofmanSrc, }: rec { - # pil2-proofman's build.rs expects pil2-stark at CARGO_MANIFEST_DIR/../../pil2-stark. - # In Nix vendored builds the manifest lives under /build/--vendor/source-git-0/, - # so we copy pil2-stark into the vendor root and patch the missing include. - pil2StarkPostPatch = '' - pil2dir="/build/$pname-$version-vendor/pil2-stark" - cp -r --no-preserve=mode ${proofmanSrc}/pil2-stark "$pil2dir" - mkdir -p "$pil2dir/.git" - for f in \ - src/rapidsnark/binfile_utils.hpp \ - src/rapidsnark/thread_utils.hpp \ - src/rapidsnark/binfile_writer.hpp - do - sed -i '1i #include ' "$pil2dir/$f" - done - ''; + version = "0.16.1"; - nativeBuildInputs = with pkgs; [ - pkg-config - protobuf - nasm - clang - gnumake - cmake - llvmPackages.openmp - ]; + # Pre-built pil2-stark with libstarks.a (build.rs sees it exists and skips make) + pil2Stark = pkgs.callPackage ./pil2-stark.nix {inherit proofmanSrc;}; - buildInputs = with pkgs; [ - grpc - gmp - jq - libsodium - libpqxx - libuuid - openssl - postgresql - secp256k1 - nlohmann_json - libgit2 - zlib - mkl - mpi - ]; + cargoVendorDir = craneLib.vendorCargoDeps { + src = ziskSrc; + # Patch proofman-starks-lib-c build.rs to accept PIL2_STARK_DIR env var. + # The upstream hardcodes ../../pil2-stark which breaks in vendored builds. + overrideVendorGitCheckout = _ps: drv: + drv.overrideAttrs (old: { + postInstall = + (old.postInstall or "") + + '' + for f in $out/proofman-starks-lib-c-*/build.rs; do + [ -f "$f" ] || continue + sed -i 's@let pil2_stark_path = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../pil2-stark");@let pil2_stark_path = std::env::var("PIL2_STARK_DIR").map(std::path::PathBuf::from).unwrap_or_else(|_| Path::new(env!("CARGO_MANIFEST_DIR")).join("../../pil2-stark"));@' "$f" + done + ''; + }); + }; - LIBCLANG_PATH = "${pkgs.llvmPackages.libclang.lib}/lib"; + commonArgs = { + pname = "zisk"; + inherit version cargoVendorDir; + src = ziskSrc; + strictDeps = true; + doCheck = false; + PIL2_STARK_DIR = "${pil2Stark}"; - LD_LIBRARY_PATH = lib.makeLibraryPath (with pkgs; - [ - zlib - stdenv.cc.cc.lib - openssl + nativeBuildInputs = with pkgs; [ + pkg-config + protobuf + nasm + clang + gnumake + cmake + llvmPackages.openmp + ]; + + buildInputs = with pkgs; [ + grpc gmp + jq libsodium + libpqxx + libuuid + openssl postgresql - llvmPackages.openmp - ] - ++ lib.optionals stdenv.isLinux [ + secp256k1 + nlohmann_json + libgit2 + zlib + mkl mpi - ]); + ]; + + LIBCLANG_PATH = "${pkgs.llvmPackages.libclang.lib}/lib"; + + LD_LIBRARY_PATH = lib.makeLibraryPath (with pkgs; + [ + zlib + stdenv.cc.cc.lib + openssl + gmp + libsodium + libgit2 + postgresql + llvmPackages.openmp + ] + ++ lib.optionals stdenv.isLinux [ + mpi + ]); + }; + + cargoArtifacts = craneLib.buildDepsOnly commonArgs; } diff --git a/pkgs/pil2-stark.nix b/pkgs/pil2-stark.nix new file mode 100644 index 0000000..64b85b2 --- /dev/null +++ b/pkgs/pil2-stark.nix @@ -0,0 +1,45 @@ +# Pre-build pil2-stark with libstarks.a so proofman-starks-lib-c's build.rs +# finds the library and skips C++ compilation. +{ + stdenv, + proofmanSrc, + pkg-config, + nasm, + gmp, + libsodium, + nlohmann_json, + openssl, + mpi, + llvmPackages, +}: +stdenv.mkDerivation { + pname = "pil2-stark"; + version = "0.16.1"; + + src = "${proofmanSrc}/pil2-stark"; + + nativeBuildInputs = [pkg-config nasm]; + buildInputs = [gmp libsodium nlohmann_json openssl mpi llvmPackages.openmp]; + + postPatch = '' + # Patch C++ headers missing include + for f in \ + src/rapidsnark/binfile_utils.hpp \ + src/rapidsnark/thread_utils.hpp \ + src/rapidsnark/binfile_writer.hpp + do + sed -i '1i #include ' "$f" + done + ''; + + buildPhase = '' + make -j starks_lib + ''; + + # Output a minimal pil2-stark tree: just what build.rs checks for + installPhase = '' + mkdir -p $out/.git $out/lib/include + cp lib/libstarks.a $out/lib/ + cp lib/include/starks_lib.h $out/lib/include/ + ''; +} diff --git a/pkgs/zisk-home.nix b/pkgs/zisk-home.nix index 2762ff6..b7db314 100644 --- a/pkgs/zisk-home.nix +++ b/pkgs/zisk-home.nix @@ -1,32 +1,31 @@ { + lib, stdenv, + pkgs, cargo-zisk, ziskemu, zisk-toolchain, ziskSrc, - rustPlatform, + craneLib, + proofmanSrc, nasm, gmp, }: let - # Build libziskclib from the zisk Rust workspace - ziskcLib = rustPlatform.buildRustPackage { - pname = "zisk-libs"; - version = "0.16.1"; - src = ziskSrc; - cargoHash = "sha256-DTD9NeTfhatR9gCIaZXoIpiXLyY0/hiauSSxsc9FZq8="; + common = import ./common.nix {inherit lib stdenv pkgs craneLib ziskSrc proofmanSrc;}; - # Only build the ziskclib library - buildPhase = '' - cargo build --release --lib -p ziskclib - ''; + # Build libziskclib from the zisk Rust workspace, reusing shared deps + ziskcLib = craneLib.buildPackage (common.commonArgs + // { + inherit (common) cargoArtifacts; + pname = "zisk-libs"; - installPhase = '' - mkdir -p $out - cp target/release/libziskclib.a $out - ''; + cargoExtraArgs = "--lib -p ziskclib"; - doCheck = false; - }; + installPhaseCommand = '' + mkdir -p $out + cp target/release/libziskclib.a $out/ + ''; + }); in stdenv.mkDerivation { name = "zisk-home"; diff --git a/pkgs/ziskemu.nix b/pkgs/ziskemu.nix index 7f53cf3..7118ef8 100644 --- a/pkgs/ziskemu.nix +++ b/pkgs/ziskemu.nix @@ -1,29 +1,23 @@ { lib, stdenv, - rustPlatform, pkgs, + craneLib, ziskSrc, proofmanSrc, }: let - common = import ./common.nix {inherit lib stdenv pkgs proofmanSrc;}; + common = import ./common.nix {inherit lib stdenv pkgs craneLib ziskSrc proofmanSrc;}; in - rustPlatform.buildRustPackage rec { - pname = "ziskemu"; - version = "0.16.1"; + craneLib.buildPackage (common.commonArgs + // { + inherit (common) cargoArtifacts; + pname = "ziskemu"; - src = ziskSrc; - cargoHash = "sha256-DTD9NeTfhatR9gCIaZXoIpiXLyY0/hiauSSxsc9FZq8="; + cargoExtraArgs = "-p ziskemu"; - buildAndTestSubdir = "emulator"; - - postPatch = common.pil2StarkPostPatch; - - nativeBuildInputs = - common.nativeBuildInputs - ++ [ - pkgs.gcc - ]; - - inherit (common) buildInputs LIBCLANG_PATH LD_LIBRARY_PATH; - } + nativeBuildInputs = + common.commonArgs.nativeBuildInputs + ++ [ + pkgs.gcc + ]; + })