From 03fe0c24e1a6f4b754259d1fff058c9dfe12fb33 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Sun, 22 Nov 2026 22:01:07 -0600 Subject: [PATCH] pkgs/id: split dependency compilation out of the Nix build with crane (#8574) Convert pkgs/id/default.nix from a single buildRustPackage to the crane two-tier shape proven by atlas-search (#8560) and fleet (#8572): buildDepsOnly compiles the registry + vendored dependencies as their own derivation (id-deps), keyed only on the manifests and Cargo.lock with the workspace sources stubbed, and buildPackage builds the workspace against those artifacts from the full source tree. A one-line source change now rebuilds only the package derivation (dry-run verified: only the id-0.1.0 package derivation is listed; the id-deps derivation is untouched). Notes: - Callers may pass craneLib; standalone nix-build constructs one from the pinned crane rev the repo flakes already carry (01bc1d404a51a0a07e9d8759cd50a7903e218c82). - Crane takes the lockfile as a path and outputHashes as a separate argument, keyed by the lockfile's full git source string instead of crate name-version (same p2panda checkout, same hash). - The standalone check derivation (~line 82) is unchanged byte for byte and still instantiates; the package-root build.rs is stubbed by crane in the deps tier and runs for real in the package tier, whose source is the full tree exactly as the monolith saw it. - Tests run under the release profile in the crane path (crane reuses the dependency artifacts for the check phase) instead of the monolith's ci-check checkType. Timings on the reference laptop (both builds red only on the pre-existing #12772 failure, with an identical failure set - id-cli 743 passed / 1 failed): monolith baseline 45m16s; first crane build 94m40s (the deps tier's build+check+test passes and release-profile test compilation, under heavy concurrent load). The split's payoff is in rebuilds: the deps derivation is untouched by source changes. --- pkgs/id/default.nix | 142 +++++++++++++++++++++++++++++++------------- 1 file changed, 102 insertions(+), 40 deletions(-) diff --git a/pkgs/id/default.nix b/pkgs/id/default.nix index 3cd296e4af..0217640849 100644 --- a/pkgs/id/default.nix +++ b/pkgs/id/default.nix @@ -7,9 +7,24 @@ # # This derivation builds the release binary and runs the full test suite # during the check phase. +# +# Dependency compilation is split from the package build with crane +# (code#8392, following atlas-search #8560 and fleet #8572): crane's +# buildDepsOnly compiles every registry dependency as its OWN +# derivation keyed only on the manifests + Cargo.lock (crane stubs the +# workspace crates' sources in that tier), so a source-only change +# rebuilds just the package derivation instead of the whole dependency +# tree. Callers may pass their own craneLib; standalone nix-build use +# constructs one from the pinned crane rev the repo's flakes already +# carry. The package tier keeps the monolith's exact behaviour — +# same features, same test suite, same postCheck fmt/clippy — except +# that the tests run under the release profile (crane reuses the +# dependency artifacts for the check phase) instead of the ci-check +# profile the monolith's checkType selected. { pkgs ? import { }, + craneLib ? null, }: let @@ -23,24 +38,66 @@ let pname = cargoToml.package.name; inherit (cargoToml.package) version; -in -{ - # Main package build - default = pkgs.rustPlatform.buildRustPackage { - inherit pname version; - src = ./.; + # Crane, pinned to the rev the code repo's root lock already carries + # (the same rev atlas-search and fleet use). + craneLibEffective = + if craneLib != null then + craneLib + else + import (pkgs.fetchFromGitHub { + owner = "ipetkov"; + repo = "crane"; + rev = "01bc1d404a51a0a07e9d8759cd50a7903e218c82"; + hash = "sha256-8qxEFpj4dVmIuPn9j9z6NTbU+hrcGjBOvaxTzre5HmM="; + }) { inherit pkgs; }; + + # Crane takes the lockfile as a path and the git-dependency hashes + # as a separate outputHashes argument (where buildRustPackage nested + # them under cargoLock), keyed by the lockfile's full source string + # instead of crate name-version — same checkout, same hash. + cargoLockPath = ./Cargo.lock; + outputHashesAttrs = { + "git+https://github.com/p2panda/p2panda?rev=da80c05b95269926bf58121cf6a2d3dd5ca41dfc#da80c05b95269926bf58121cf6a2d3dd5ca41dfc" = + "sha256-yq+ySfo14ucU1BWOUdJM0n7/FL3+Ria3Z+MxMHFa4Ls="; + }; - buildFeatures = [ - "sandbox" - "world" + metaAttrs = with pkgs.lib; { + description = "A peer-to-peer file sharing CLI built with Iroh"; + homepage = "https://github.com/developing-today/code"; + license = with licenses; [ + mit + asl20 ]; + maintainers = [ ]; + }; + + # Two source tiers. The dependency tier carries only what can change + # dependency compilation: the workspace manifests and lockfile, the + # crate sources (crane stubs the workspace members' Rust targets in + # this tier, so a source edit leaves the tier's output untouched), + # the vendored path dependencies (compiled for real as dependencies), + # the package-root build.rs (stubbed by crane here; its real run — + # the web/dist asset check — happens in the package tier, whose + # source is the full tree), and .cargo/config.toml. + depsSrc = pkgs.lib.fileset.toSource { + root = ./.; + fileset = pkgs.lib.fileset.unions [ + ./Cargo.toml + ./Cargo.lock + ./crates + ./src + ./vendor + ./build.rs + ./.cargo + ]; + }; - cargoLock = { - lockFile = ./Cargo.lock; - outputHashes = { - "p2panda-core-0.7.1" = "sha256-yq+ySfo14ucU1BWOUdJM0n7/FL3+Ria3Z+MxMHFa4Ls="; - }; - }; + commonArgs = { + inherit pname version; + strictDeps = true; + cargoLock = cargoLockPath; + outputHashes = outputHashesAttrs; + cargoExtraArgs = "--locked --features sandbox,world"; nativeBuildInputs = with pkgs; [ pkg-config @@ -49,33 +106,38 @@ in buildInputs = with pkgs; [ openssl ]; + }; - # Run tests during build - doCheck = true; - - # Compile and run the test phase under the ci-check profile (#3195) - # instead of release: same opt-level, no LTO / codegen-units=1 / - # strip costs for test binaries. The release binary build - # (cargoBuildType) is unchanged. - checkType = "ci-check"; + # The dependency layer: every registry + vendored dependency compiled + # once, keyed on the deps tier above only. + cargoArtifacts = craneLibEffective.buildDepsOnly (commonArgs // { src = depsSrc; }); - # Additional check commands (run after cargoTest) - postCheck = '' - echo "Running additional checks..." - cargo fmt -- --check - cargo clippy --all-targets --all-features -- -D warnings - ''; - - meta = with pkgs.lib; { - description = "A peer-to-peer file sharing CLI built with Iroh"; - homepage = "https://github.com/developing-today/code"; - license = with licenses; [ - mit - asl20 - ]; - maintainers = [ ]; - }; - }; +in +{ + # Main package build + default = craneLibEffective.buildPackage ( + commonArgs + // { + # The package tier sees the full tree, exactly what the + # buildRustPackage build saw (tests read test-data/, the root + # build.rs checks web/dist, and so on). + src = ./.; + inherit cargoArtifacts; + + # Run tests during build (the test suite runs under the release + # profile here — see the header note). + doCheck = true; + + # Additional check commands (run after cargo test) + postCheck = '' + echo "Running additional checks..." + cargo fmt -- --check + cargo clippy --all-targets --all-features -- -D warnings + ''; + + meta = metaAttrs; + } + ); # Standalone check derivation - runs all quality checks check = pkgs.stdenv.mkDerivation {