Skip to content

Commit 9556f07

Browse files
Allow Cargo.lock hash to be provided when using importCargoLock
Add optional cargoLockHash parameter that makes Cargo.lock extraction a fixed-output derivation. Use pkgsBuildHost rather than pkgs for build -- as a FOD, this doesn't change the build hash. This allows platforms that are doing cross-evalution but not cross-compilation to generate identical derivation hashes between cross-evaluation and local build, so long as they (1) provide a hash for Cargo.lock files, and (2) ensure that `pkgsBuildHost` is set to reflect the system doing the evaluation.
1 parent 7ea903d commit 9556f07

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

build/hacks/default.nix

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,18 @@ in
107107
«derivation /nix/store/g3z1zlmc0sqpd6d5ccfrx3c4w4nv5dzr-cryptography-43.0.0.drv»
108108
```
109109
110+
# Cross-platform evaluation example: needs cargoLockHash
111+
112+
```nix
113+
importCargoLock {
114+
prev = prev.cryptography;
115+
# Lock file relative to source root
116+
lockFile = "src/rust/Cargo.lock";
117+
# Provide the SRI hash of the Cargo.lock file for cross-platform evaluation
118+
cargoLockHash = "sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=";
119+
}
120+
```
121+
110122
# Type
111123
112124
```
@@ -127,6 +139,9 @@ in
127139
lockFile
128140
: Path to Cargo.lock (defaults to `${cargoRoot}/Cargo.lock`)
129141
142+
cargoLockHash
143+
: Optional SRI hash of the Cargo.lock file (enables cross-platform evaluation)
144+
130145
doUnpack
131146
: Whether to unpack sources using an intermediate derivation
132147
@@ -149,6 +164,7 @@ in
149164
unpackDerivationArgs ? { },
150165
cargoRoot ? ".",
151166
lockFile ? "${cargoRoot}/Cargo.lock",
167+
cargoLockHash ? null,
152168
cargo ? pkgs.cargo,
153169
rustc ? pkgs.rustc,
154170
pkg-config ? pkgs.pkg-config,
@@ -175,13 +191,33 @@ in
175191
}
176192
);
177193

194+
# Determine the lock file path
195+
# If cargoLockHash is provided and we need to unpack, extract just the Cargo.lock
196+
# as a fixed-output derivation for cross-platform evaluation
197+
actualLockFile =
198+
if !doUnpack || cargoLockHash == null then
199+
# Use the lock file from src (either local path or unpacked archive)
200+
if lib.hasPrefix "/" lockFile then lockFile else "${src}/${lockFile}"
201+
else
202+
# Create fixed-output derivation for just the Cargo.lock file
203+
# Use pkgsBuildHost to ensure this runs on the build platform
204+
pkgs.pkgsBuildHost.runCommand "${prev.src.name}-cargo-lock"
205+
{
206+
outputHash = cargoLockHash;
207+
outputHashMode = "flat";
208+
nativeBuildInputs = with pkgs.pkgsBuildHost; [ gnutar gzip bzip2 xz ];
209+
}
210+
''
211+
tar -xaf ${prev.src} --to-stdout --no-wildcards-match-slash --wildcards "*/${lib.removePrefix "./" lockFile}" >"$out" && test -s "$out"
212+
'';
213+
178214
in
179215
assert isDerivation prev;
180216
prev.overrideAttrs (old: {
181217
inherit cargoRoot src;
182218
cargoDeps = pkgs.rustPlatform.importCargoLock (
183219
{
184-
lockFile = "${src}/${lockFile}";
220+
lockFile = actualLockFile;
185221
}
186222
// importCargoLockArgs
187223
);

0 commit comments

Comments
 (0)