Skip to content

Commit 8f0dffb

Browse files
committed
fix: remove unnecessary modifiedSrc derivation to resolve IFD cross-compilation issues
The root issue was that the buildPgrxExtension was creating a modifiedSrc derivation to copy external Cargo.lock files for crane. This derivation was being created for each target platform during evaluation, causing system conflicts when evaluating aarch64-darwin packages on x86_64-linux systems. The solution is to remove the modifiedSrc logic entirely and rely on the existing postPatch mechanism in the extensions, which already handles external lockfiles by creating symlinks. Crane works perfectly with this approach and doesn't need the lockfiles copied during evaluation. This resolves the IFD (Import From Derivation) issues while maintaining crane's incremental build benefits and cross-compilation support.
1 parent 7e3bdbf commit 8f0dffb

File tree

1 file changed

+13
-32
lines changed

1 file changed

+13
-32
lines changed

nix/cargo-pgrx/buildPgrxExtension.nix

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
pkg-config,
3434
rustPlatform,
3535
stdenv,
36-
buildPackages,
3736
writeShellScriptBin,
3837
defaultBindgenHook,
3938
}:
@@ -45,6 +44,10 @@
4544
# Crane separates dependency builds from main crate builds, enabling better caching.
4645
# Both approaches accept the same arguments and produce compatible outputs.
4746
#
47+
# IMPORTANT: External Cargo.lock files are handled by extensions' postPatch phases,
48+
# not by copying during evaluation. This avoids IFD (Import From Derivation) issues
49+
# that caused cross-compilation failures when evaluating aarch64 packages on x86_64.
50+
#
4851
# Additional arguments:
4952
# - `postgresql` postgresql package of the version of postgresql this extension should be build for.
5053
# Needs to be the build platform variant.
@@ -144,37 +147,15 @@ let
144147
pg_ctl stop
145148
'';
146149

147-
# Crane-specific: handle external Cargo.lock files
148-
# Crane expects Cargo.lock at source root, but rustPlatform allows external lockFile paths.
149-
# Without this, crane fails with:
150-
# "error: unable to find Cargo.lock at /nix/store/...-source. please ensure one of the following:
151-
# - a Cargo.lock exists at the root of the source directory"
150+
# Crane-specific: Determine if we're using crane and handle cargo lock info
151+
# Note: External lockfiles are handled by extensions' postPatch, not here, to avoid
152+
# creating platform-specific derivations during evaluation (prevents IFD issues)
152153
useCrane = craneLib != null;
153154
cargoLockInfo = args.cargoLock or null;
154-
hasExternalLockFile = cargoLockInfo != null && cargoLockInfo ? lockFile;
155-
156-
# Copy external Cargo.lock into source directory to satisfy crane's requirements
157-
modifiedSrc =
158-
if useCrane && hasExternalLockFile then
159-
# Use buildPackages.stdenv to ensure this runs on the build platform
160-
# This derivation just copies files and should be buildable during evaluation
161-
buildPackages.stdenv.mkDerivation {
162-
name = "${args.pname or "source"}-with-lockfile";
163-
src = args.src;
164-
dontBuild = true;
165-
# Ensure this derivation runs locally during evaluation
166-
preferLocalBuild = true;
167-
allowSubstitutes = false;
168-
installPhase = ''
169-
cp -r $src $out
170-
chmod -R u+w $out
171-
${lib.optionalString (cargoLockInfo ? lockFile) ''
172-
cp ${cargoLockInfo.lockFile} $out/Cargo.lock
173-
''}
174-
'';
175-
}
176-
else
177-
args.src;
155+
156+
# External Cargo.lock files are handled by the extension's postPatch phase
157+
# which creates symlinks. Crane finds them during build, not evaluation.
158+
# This approach prevents IFD cross-compilation issues.
178159

179160
# Use rustPlatform.importCargoLock instead of crane's vendorCargoDeps for git dependencies.
180161
# crane's vendorCargoDeps uses builtins.fetchGit which only searches the default branch,
@@ -183,7 +164,7 @@ let
183164
# 'refs/heads/main' of repository 'https://github.com/burmecia/iceberg-rust'!"
184165
# rustPlatform.importCargoLock with allowBuiltinFetchGit searches all refs (branches/tags).
185166
cargoVendorDir =
186-
if useCrane && hasExternalLockFile && cargoLockInfo ? outputHashes then
167+
if useCrane && cargoLockInfo != null && cargoLockInfo ? outputHashes then
187168
rustPlatform.importCargoLock {
188169
lockFile = cargoLockInfo.lockFile;
189170
outputHashes = cargoLockInfo.outputHashes;
@@ -212,7 +193,7 @@ let
212193
commonArgs =
213194
argsForBuilder
214195
// {
215-
src = modifiedSrc;
196+
src = args.src; # Use original source - extensions handle external lockfiles via postPatch
216197
strictDeps = true;
217198

218199
buildInputs = (args.buildInputs or [ ]);

0 commit comments

Comments
 (0)