installPhase cannot execute binary - execve cannot find the file

I am trying to package a locally built version of python3 and creating my own nix package.

I have it completely working as a stand-alone package - when I start nix-shell, I can run the binary and run it with strace and it works perfectly both on MacOs and Linux

Then I wanted to install some additional modules as part of the installPhase of my derivation by doing pip install. I cannot run the binary as part of the install phase as I am getting an error. The error only occurs on Linux distribution and does not happen on MacOS - which lets me launch the binary properly during the installPhase. The ldd output and print-interpreter, --print-soname --print-rpath and --print-needed output between the install phase output and the output after starting the nix-shell.

The derivation is:
{ pkgs, cayman_openssl3, localModules }:

let
addLocalModules = import ./add_local_modules.nix;
build = “sb-67336917”;
pythonSettings = if pkgs.stdenv.system == “x86_64-darwin” then
{
arch = “apple_mac64+nix”;
sha = “sha256:13kf1k1ppp923s0ash2zkzws62rvx6qsd279krbzah2i2b2iiyx3”;
extra_commands = ‘’
# update the dylib location
echo install_name_tool
-change /Volumes/build/mts/release/${build}/cayman_python/build/release/apple_mac64+nix/python/install/lib/libpython3.11.dylib $out/python/lib/libpython3.11.dylib
$out/python/bin/python3.11
install_name_tool
-change /Volumes/build/mts/release/${build}/cayman_python/build/release/apple_mac64+nix/python/install/lib/libpython3.11.dylib $out/python/lib/libpython3.11.dylib
$out/python/bin/python3.11
echo install_name_tool
-id $out/python/lib/libpython3.11.dylib $out/python/lib/libpython3.11.dylib
install_name_tool
-id $out/python/lib/libpython3.11.dylib $out/python/lib/libpython3.11.dylib
echo install_name_tool
-change @GOBUILD_CAYMAN_OPENSSL_ROOT@/apple_macos-universal/lib/libssl.3.dylib ${cayman_openssl3}/openssl/lib/libssl.3.dylib
-change @GOBUILD_CAYMAN_OPENSSL_ROOT@/apple_macos-universal/lib/libcrypto.3.dylib ${cayman_openssl3}/openssl/lib/libcrypto.3.dylib
$out/python/lib/python3.11/lib-dynload/_ssl.cpython-311-darwin.so
install_name_tool
-change @GOBUILD_CAYMAN_OPENSSL_ROOT@/apple_macos-universal/lib/libssl.3.dylib ${cayman_openssl3}/openssl/lib/libssl.3.dylib
-change @GOBUILD_CAYMAN_OPENSSL_ROOT@/apple_macos-universal/lib/libcrypto.3.dylib ${cayman_openssl3}/openssl/lib/libcrypto.3.dylib
$out/python/lib/python3.11/lib-dynload/_ssl.cpython-311-darwin.so
echo install_name_tool
-change @GOBUILD_CAYMAN_OPENSSL_ROOT@/apple_macos-universal/lib/libcrypto.3.dylib ${cayman_openssl3}/openssl/lib/libcrypto.3.dylib
$out/python/lib/python3.11/lib-dynload/_hashlib.cpython-311-darwin.so
install_name_tool
-change @GOBUILD_CAYMAN_OPENSSL_ROOT@/apple_macos-universal/lib/libcrypto.3.dylib ${cayman_openssl3}/openssl/lib/libcrypto.3.dylib
$out/python/lib/python3.11/lib-dynload/_hashlib.cpython-311-darwin.so
‘’;
} else {
arch = “lin64+glibc217+gcc12+nix”;
sha = “sha256:1xfjcqiz71na0rdfqz9l2n2bbiqkdavvxfcpnrm2m6lrgqq9k8hf”;
extra_commands = ‘’
# Remove broken symlink
rm $out/python/lib/libpython3.11.so
# Re-create symlink
ln -s $out/python/lib/libpython3.11.so.1.0 $out/python/lib/libpython3.11.so
# patch executable .so locations
echo patchelf --replace-needed libpython3.11.so.1.0 $out/python/lib/libpython3.11.so.1.0 $out/python/bin/python3.11
patchelf --replace-needed libpython3.11.so.1.0 $out/python/lib/libpython3.11.so.1.0 $out/python/bin/python3.11
echo patchelf --replace-needed libz.so.1 ${pkgs.zlib}/lib/libz.so.1 $out/python/lib/libpython3.11.so.1.0
patchelf --replace-needed libz.so.1 ${pkgs.zlib}/lib/libz.so.1 $out/python/lib/libpython3.11.so.1.0
echo patchelf
–replace-needed libssl.so.3 ${cayman_openssl3}/openssl/lib/libssl.so.3
–replace-needed libcrypto.so.3 ${cayman_openssl3}/openssl/lib/libcrypto.so.3
$out/python/lib/python3.11/lib-dynload/_ssl.cpython-311-x86_64-linux-gnu.so
patchelf
–replace-needed libssl.so.3 ${cayman_openssl3}/openssl/lib/libssl.so.3
–replace-needed libcrypto.so.3 ${cayman_openssl3}/openssl/lib/libcrypto.so.3
$out/python/lib/python3.11/lib-dynload/_ssl.cpython-311-x86_64-linux-gnu.so
echo patchelf
–replace-needed libcrypto.so.3 ${cayman_openssl3}/openssl/lib/libcrypto.so.3
$out/python/lib/python3.11/lib-dynload/_hashlib.cpython-311-x86_64-linux-gnu.so
patchelf
–replace-needed libcrypto.so.3 ${cayman_openssl3}/openssl/lib/libcrypto.so.3
$out/python/lib/python3.11/lib-dynload/_hashlib.cpython-311-x86_64-linux-gnu.so
‘’;
};
addLocalModulesCommands = addLocalModules { modules=localModules; };
in
pkgs.stdenv.mkDerivation rec {
pname = “python”;
version = “3.11”;

src = fetchTarball {
url=“http://build-squid.eng.vmware.com/build/mts/release/${build}/publish/python-${pythonSettings.arch}.tgz”;
sha256= pythonSettings.sha;
};

buildInputs = [
cayman_openssl3
# TODO (MGIT-423): Update to use cayman_zlib instead of nixos.zlib
pkgs.zlib
pkgs.strace
];

installPhase = ‘’
mkdir -p $out/bin
mkdir -p $out/python
# Remove broken links
rm bin/2to3
rm bin/idle3
rm bin/pydoc3
rm -f bin/python
rm bin/python3
rm bin/python3-config
rm -f bin/python3-intel64
cp -rf * $out/python
# Setup basic links into the $out/bin directory
ln -s $out/python/bin/python3.11 $out/bin/python3.11
ln -s $out/python/bin/2to3-3.11 $out/bin/2to3-3.11
ln -s $out/python/bin/pip3.11 $out/bin/pip3.11
ln -s $out/python/bin/pydoc3.11 $out/bin/pydoc3.11
ln -s $out/python/bin/python3.11-config $out/bin/python3.11-config
ln -s $out/python/bin/idle3.11 $out/bin/idle3.11
# Restore python3 link to the python directory
# ln -s $out/python/bin/python3.11 $out/python/bin/python3
# Extra spellings for python
ln -s $out/bin/python3.11 $out/bin/python3
ln -s $out/bin/python3 $out/bin/python
# Extra spellings for 2to3
ln -s $out/bin/2to3-3.11 $out/bin/2to3
# Extra spellings for pip
ln -s $out/bin/pip3.11 $out/bin/pip3
ln -s $out/bin/pip3 $out/bin/pip
# Extra spellings for pydoc
ln -s $out/bin/pydoc3.11 $out/bin/pydoc3
ln -s $out/bin/pydoc3 $out/bin/pydoc
# Extra spellings for python-config
ln -s $out/bin/python3.11-config $out/bin/python3-config
ln -s $out/bin/python3-config $out/bin/python-config
# Extra spellings for idle
ln -s $out/bin/idle3.11 $out/bin/idle3
ln -s $out/bin/idle3 $out/bin/idle
# Fix #! lines
sed -i “s|#!.$|#!env python3.11|" $out/python/bin/2to3-3.11
sed -i "s|#!.
$|#!env python3.11|” $out/python/bin/idle3.11
sed -i “s|#!.$|#!env python3.11|" $out/python/bin/pip3
sed -i "s|#!.
$|#!env python3.11|” $out/python/bin/pip3.11
sed -i “s|#!.$|#!env python3.11|" $out/python/bin/pydoc3.11
sed -i "s|#!.
$|#!env python3.11|” $out/python/bin/python3.11-config
${pythonSettings.extra_commands}
echo ldd $out/python/bin/python3.11
ldd $out/python/bin/python3.11
echo patchelf --print-interpreter --print-soname --print-rpath --print-needed $out/python/bin/python3.11
patchelf --print-interpreter --print-soname --print-rpath --print-needed $out/python/bin/python3.11
${pkgs.strace}/bin/strace $out/python/bin/python3.11
‘’;
}
The strace output shows that the execve system call cannot find the file - even though it is readable by ldd.

Here is the output produced that shows the error:
ldd /nix/store/yi5j7cf5xx031k2cbar6mkk41hss9x7y-python-3.11/python/bin/python3.11
linux-vdso.so.1 (0x00007ffff7fc8000)
/nix/store/yi5j7cf5xx031k2cbar6mkk41hss9x7y-python-3.11/python/lib/libpython3.11.so.1.0 (0x00007ffff79f7000)
libc.so.6 => /nix/store/46m4xx889wlhsdj72j38fnlyyvvvvbyb-glibc-2.37-8/lib/libc.so.6 (0x00007ffff7811000)
/nix/store/69jpyha5zbll6ppqzhbihhp51lac1hrp-zlib-1.2.13/lib/libz.so.1 (0x00007ffff77f2000)
libpthread.so.0 => /nix/store/46m4xx889wlhsdj72j38fnlyyvvvvbyb-glibc-2.37-8/lib/libpthread.so.0 (0x00007ffff77ed000)
libdl.so.2 => /nix/store/46m4xx889wlhsdj72j38fnlyyvvvvbyb-glibc-2.37-8/lib/libdl.so.2 (0x00007ffff77e8000)
libutil.so.1 => /nix/store/46m4xx889wlhsdj72j38fnlyyvvvvbyb-glibc-2.37-8/lib/libutil.so.1 (0x00007ffff77e1000)
libm.so.6 => /nix/store/46m4xx889wlhsdj72j38fnlyyvvvvbyb-glibc-2.37-8/lib/libm.so.6 (0x00007ffff7701000)
/lib64/ld-linux-x86-64.so.2 => /nix/store/46m4xx889wlhsdj72j38fnlyyvvvvbyb-glibc-2.37-8/lib64/ld-linux-x86-64.so.2 (0x00007ffff7fca000)
patchelf --print-interpreter --print-soname --print-rpath --print-needed /nix/store/yi5j7cf5xx031k2cbar6mkk41hss9x7y-python-3.11/python/bin/python3.11
/lib64/ld-linux-x86-64.so.2
$ORIGIN/…/lib
/nix/store/yi5j7cf5xx031k2cbar6mkk41hss9x7y-python-3.11/python/lib/libpython3.11.so.1.0
libc.so.6
execve(“/nix/store/yi5j7cf5xx031k2cbar6mkk41hss9x7y-python-3.11/python/bin/python3.11”, [“/nix/store/yi5j7cf5xx031k2cbar6m”…], 0x7fffffffc590 /* 79 vars */) = -1 ENOENT (No such file or directory)
/nix/store/fqj5bn47jb3dhijjgdrwhbzsbjf7831v-strace-6.4/bin/strace: exec: No such file or directory
+++ exited with 1 +++
error: builder for ‘/nix/store/jb8641gyfagdq547iqx2d8c6bib2lyg9-python-3.11.drv’ failed with exit code 1;

Same here. Maybe it’s a bug: