buildRustPackage: dont rely on NIX_BUILD_TOP in cargoSetupPostPatchHook

This breaks the builder when a nix-shell or keepBuildTree is used. The
issue occurs because paths to cargo lockfiles are read with NIX_BUILD_TOP,
which is not reliable.

This breaks a nix-shell because NIX_BUILD_TOP simply is not set, causing
an invalid path to be used. This can be worked around using
NIX_BUILD_TOP=$PWD, but that obviously is not great.

This breaks keepBuildTree because it changes the working directory to a
different path than NIX_BUILD_TOP. Since the lockfiles are copied based
on the working directory, but read based on NIX_BUILD_TOP, this causes
the hook to not be able to find them.

This was solved by both reading these files based on the working directory,
using absolute paths to avoid having to traverse back in the directory tree.

Fixes: #138554
This commit is contained in:
Ivar Scholten 2022-09-12 13:50:18 +02:00 committed by Bjørn Forsman
parent bf9ff0c687
commit 987d32bbac

View file

@ -7,23 +7,23 @@ cargoSetupPostUnpackHook() {
# this for us automatically.
if [ -z $cargoVendorDir ]; then
unpackFile "$cargoDeps"
export cargoDepsCopy=$(stripHash $cargoDeps)
export cargoDepsCopy="$(realpath "$(stripHash $cargoDeps)")"
else
cargoDepsCopy="$sourceRoot/${cargoRoot:+$cargoRoot/}${cargoVendorDir}"
cargoDepsCopy="$(realpath "$(pwd)/$sourceRoot/${cargoRoot:+$cargoRoot/}${cargoVendorDir}")"
fi
if [ ! -d .cargo ]; then
mkdir .cargo
fi
config="$(pwd)/$cargoDepsCopy/.cargo/config";
config="$cargoDepsCopy/.cargo/config";
if [[ ! -e $config ]]; then
config=@defaultConfig@
fi;
tmp_config=$(mktemp)
substitute $config $tmp_config \
--subst-var-by vendor "$(pwd)/$cargoDepsCopy"
--subst-var-by vendor "$cargoDepsCopy"
cat ${tmp_config} >> .cargo/config
cat >> .cargo/config <<'EOF'
@ -39,8 +39,8 @@ EOF
cargoSetupPostPatchHook() {
echo "Executing cargoSetupPostPatchHook"
cargoDepsLockfile="$NIX_BUILD_TOP/$cargoDepsCopy/Cargo.lock"
srcLockfile="$NIX_BUILD_TOP/$sourceRoot/${cargoRoot:+$cargoRoot/}/Cargo.lock"
cargoDepsLockfile="$cargoDepsCopy/Cargo.lock"
srcLockfile="$(pwd)/${cargoRoot:+$cargoRoot/}Cargo.lock"
echo "Validating consistency between $srcLockfile and $cargoDepsLockfile"
if ! @diff@ $srcLockfile $cargoDepsLockfile; then