core/pkgs/by-name/py/python/wrapper.nix

83 lines
2.2 KiB
Nix
Raw Normal View History

2024-06-30 08:16:52 +00:00
{
lib,
stdenv,
buildEnv,
makeBinaryWrapper,
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
# manually pased
python,
requiredPythonModules,
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
# extra opts
extraLibs ? [ ],
extraOutputsToInstall ? [ ],
postBuild ? "",
ignoreCollisions ? false,
permitUserSite ? false,
# Wrap executables with the given argument.
makeWrapperArgs ? [ ],
}:
2024-05-02 00:46:19 +00:00
# Create a python executable that knows about additional packages.
let
2024-06-30 08:16:52 +00:00
env =
let
paths = requiredPythonModules (extraLibs ++ [ python ]);
pythonPath = "${placeholder "out"}/${python.sitePackages}";
pythonExecutable = "${placeholder "out"}/bin/${python.executable}";
in
buildEnv {
name = "${python.name}-env";
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
inherit paths;
inherit ignoreCollisions;
extraOutputsToInstall = [ "out" ] ++ extraOutputsToInstall;
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
nativeBuildInputs = [ makeBinaryWrapper ];
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
postBuild =
''
if [ -L "$out/bin" ]; then
unlink "$out/bin"
fi
mkdir -p "$out/bin"
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
for path in ${lib.concatStringsSep " " paths}; do
if [ -d "$path/bin" ]; then
cd "$path/bin"
for prg in *; do
if [ -f "$prg" ]; then
rm -f "$out/bin/$prg"
if [ -x "$prg" ]; then
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
2024-05-02 00:46:19 +00:00
fi
done
2024-06-30 08:16:52 +00:00
''
+ postBuild;
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
inherit (python) meta;
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
passthru = python.passthru // {
interpreter = "${env}/bin/${python.executable}";
inherit python;
env = stdenv.mkDerivation {
name = "interactive-${python.name}-environment";
nativeBuildInputs = [ env ];
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
buildCommand = ''
echo >&2 ""
echo >&2 "*** Python 'env' attributes are intended for interactive nix-shell sessions, not for building! ***"
echo >&2 ""
exit 1
'';
};
};
2024-05-02 00:46:19 +00:00
};
2024-06-30 08:16:52 +00:00
in
env