abe61db737
This commit, together with https://github.com/NixOS/nixpkgs/pull/442540, changes the way python environments are built: * When generating wrappers for python executables, we inherit argv[0] from the wrapper. This causes python to initialize its configuration in the environment with all the correct paths. * We also resolve argv[0] to absolute path when invoking python from PATH. This helps set python's prefix correctly on Darwin. The end result is that python environments no longer appear to be venvs, and behave more like a vanilla python installation. In addition it's possible to create a venv using an environment and use packages from both the environment and the venv.
93 lines
2.7 KiB
Nix
93 lines
2.7 KiB
Nix
{
|
|
lib,
|
|
stdenv,
|
|
buildEnv,
|
|
runCommand,
|
|
makeBinaryWrapper,
|
|
|
|
# manually pased
|
|
python,
|
|
requiredPythonModules,
|
|
|
|
# extra opts
|
|
extraLibs ? [ ],
|
|
extraOutputsToInstall ? [ ],
|
|
postBuild ? "",
|
|
ignoreCollisions ? false,
|
|
permitUserSite ? false,
|
|
# Wrap executables with the given argument.
|
|
makeWrapperArgs ? [ ],
|
|
}:
|
|
|
|
# Create a python executable that knows about additional packages.
|
|
let
|
|
env =
|
|
let
|
|
paths = requiredPythonModules (extraLibs ++ [ python ]) ++ [
|
|
(runCommand "bin" { } ''
|
|
mkdir -p $out/bin
|
|
'')
|
|
];
|
|
pythonPath = "${placeholder "out"}/${python.sitePackages}";
|
|
pythonExecutable = "${placeholder "out"}/bin/${python.executable}";
|
|
in
|
|
buildEnv {
|
|
name = "${python.name}-env";
|
|
|
|
inherit paths;
|
|
inherit ignoreCollisions;
|
|
extraOutputsToInstall = [ "out" ] ++ extraOutputsToInstall;
|
|
|
|
nativeBuildInputs = [ makeBinaryWrapper ];
|
|
|
|
postBuild = ''
|
|
for path in ${lib.concatStringsSep " " paths}; do
|
|
if [ -d "$path/bin" ]; then
|
|
cd "$path/bin"
|
|
for prg in *; do
|
|
if [ -f "$prg" ] && [ -x "$prg" ]; then
|
|
rm -f "$out/bin/$prg"
|
|
if [ "$prg" = "${python.executable}" ]; then
|
|
makeWrapper "${python.interpreter}" "$out/bin/$prg" \
|
|
--inherit-argv0 \
|
|
--resolve-argv0 \
|
|
${lib.optionalString (!permitUserSite) ''--set PYTHONNOUSERSITE "true"''} \
|
|
${lib.concatStringsSep " " makeWrapperArgs}
|
|
elif [ "$(readlink "$prg")" = "${python.executable}" ]; then
|
|
ln -s "${python.executable}" "$out/bin/$prg"
|
|
else
|
|
makeWrapper "$path/bin/$prg" "$out/bin/$prg" \
|
|
--set NIX_PYTHONPREFIX "$out" \
|
|
--set NIX_PYTHONEXECUTABLE ${pythonExecutable} \
|
|
--set NIX_PYTHONPATH ${pythonPath} \
|
|
${lib.optionalString (!permitUserSite) ''--set PYTHONNOUSERSITE "true"''} \
|
|
${lib.concatStringsSep " " makeWrapperArgs}
|
|
fi
|
|
fi
|
|
done
|
|
fi
|
|
done
|
|
''
|
|
+ postBuild;
|
|
|
|
inherit (python) meta;
|
|
|
|
passthru = python.passthru // {
|
|
interpreter = "${env}/bin/${python.executable}";
|
|
inherit python;
|
|
env = stdenv.mkDerivation {
|
|
name = "interactive-${python.name}-environment";
|
|
nativeBuildInputs = [ env ];
|
|
|
|
buildCommand = ''
|
|
echo >&2 ""
|
|
echo >&2 "*** Python 'env' attributes are intended for interactive nix-shell sessions, not for building! ***"
|
|
echo >&2 ""
|
|
exit 1
|
|
'';
|
|
};
|
|
};
|
|
};
|
|
in
|
|
env
|