-
-
Notifications
You must be signed in to change notification settings - Fork 14.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix Python venv creation #326094
base: master
Are you sure you want to change the base?
Fix Python venv creation #326094
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,14 +38,22 @@ let | |
is_nixenv = "False"; | ||
is_virtualenv = "False"; | ||
}; | ||
} // lib.optionalAttrs (!python.isPyPy && !stdenv.hostPlatform.isDarwin) { | ||
# Use virtualenv from a Nix env. | ||
# Fails on darwin with | ||
# virtualenv: error: argument dest: the destination . is not write-able at /nix/store | ||
nixenv-virtualenv = rec { | ||
env = runCommand "${python.name}-virtualenv" {} '' | ||
${pythonVirtualEnv.interpreter} -m virtualenv venv | ||
mv venv $out | ||
} // lib.optionalAttrs (!python.isPyPy) { | ||
# Use virtualenv with symlinks from a Nix env. | ||
nixenv-virtualenv-links = rec { | ||
env = runCommand "${python.name}-virtualenv-links" {} '' | ||
${pythonVirtualEnv.interpreter} -m virtualenv --system-site-packages --symlinks --no-seed $out | ||
''; | ||
interpreter = "${env}/bin/${python.executable}"; | ||
is_venv = "False"; | ||
is_nixenv = "True"; | ||
is_virtualenv = "True"; | ||
}; | ||
} // lib.optionalAttrs (!python.isPyPy) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it intentional that you repeat line 41 here? Could be one block |
||
# Use virtualenv with copies from a Nix env. | ||
nixenv-virtualenv-copies = rec { | ||
env = runCommand "${python.name}-virtualenv-copies" {} '' | ||
${pythonVirtualEnv.interpreter} -m virtualenv --system-site-packages --copies --no-seed $out | ||
''; | ||
interpreter = "${env}/bin/${python.executable}"; | ||
is_venv = "False"; | ||
|
@@ -61,27 +69,48 @@ let | |
is_nixenv = "True"; | ||
is_virtualenv = "False"; | ||
}; | ||
} // lib.optionalAttrs (python.isPy3k && (!python.isPyPy)) { | ||
# Venv built using plain Python | ||
} // lib.optionalAttrs (python.pythonAtLeast "3.8" && (!python.isPyPy)) { | ||
# Venv built using links to plain Python | ||
# Python 2 does not support venv | ||
# TODO: PyPy executable name is incorrect, it should be pypy-c or pypy-3c instead of pypy and pypy3. | ||
plain-venv = rec { | ||
env = runCommand "${python.name}-venv" {} '' | ||
${python.interpreter} -m venv $out | ||
plain-venv-links = rec { | ||
env = runCommand "${python.name}-venv-links" {} '' | ||
${python.interpreter} -m venv --system-site-packages --symlinks --without-pip $out | ||
''; | ||
interpreter = "${env}/bin/${python.executable}"; | ||
is_venv = "True"; | ||
is_nixenv = "False"; | ||
is_virtualenv = "False"; | ||
}; | ||
|
||
} // { | ||
} // lib.optionalAttrs (python.pythonAtLeast "3.8" && (!python.isPyPy)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it intentional that you repeat line 41 here? Could be one block Also a comment why it needs to be at least python 3.8 would be nice. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The reason for python 3.8 is probably because python 2 doesn't support
|
||
# Venv built using copies from plain Python | ||
# Python 2 does not support venv | ||
# TODO: PyPy executable name is incorrect, it should be pypy-c or pypy-3c instead of pypy and pypy3. | ||
plain-venv-copies = rec { | ||
env = runCommand "${python.name}-venv-copies" {} '' | ||
${python.interpreter} -m venv --system-site-packages --copies --without-pip $out | ||
''; | ||
interpreter = "${env}/bin/${python.executable}"; | ||
is_venv = "True"; | ||
is_nixenv = "False"; | ||
is_virtualenv = "False"; | ||
}; | ||
} // lib.optionalAttrs (python.pythonAtLeast "3.8") { | ||
# Venv built using Python Nix environment (python.buildEnv) | ||
# TODO: Cannot create venv from a nix env | ||
# Error: Command '['/nix/store/ddc8nqx73pda86ibvhzdmvdsqmwnbjf7-python3-3.7.6-venv/bin/python3.7', '-Im', 'ensurepip', '--upgrade', '--default-pip']' returned non-zero exit status 1. | ||
nixenv-venv = rec { | ||
env = runCommand "${python.name}-venv" {} '' | ||
${pythonEnv.interpreter} -m venv $out | ||
nixenv-venv-links = rec { | ||
env = runCommand "${python.name}-venv-links" {} '' | ||
${pythonEnv.interpreter} -m venv --system-site-packages --symlinks --without-pip $out | ||
''; | ||
interpreter = "${env}/bin/${pythonEnv.executable}"; | ||
is_venv = "True"; | ||
is_nixenv = "True"; | ||
is_virtualenv = "False"; | ||
}; | ||
} // lib.optionalAttrs (python.pythonAtLeast "3.8") { | ||
# Venv built using Python Nix environment (python.buildEnv) | ||
nixenv-venv-copies = rec { | ||
env = runCommand "${python.name}-venv-copies" {} '' | ||
${pythonEnv.interpreter} -m venv --system-site-packages --copies --without-pip $out | ||
''; | ||
interpreter = "${env}/bin/${pythonEnv.executable}"; | ||
is_venv = "True"; | ||
|
@@ -93,11 +122,33 @@ let | |
testfun = name: attrs: runCommand "${python.name}-tests-${name}" ({ | ||
inherit (python) pythonVersion; | ||
} // attrs) '' | ||
mkdir $out | ||
|
||
# set up the test files | ||
cp -r ${./tests/test_environments} tests | ||
chmod -R +w tests | ||
substituteAllInPlace tests/test_python.py | ||
${attrs.interpreter} -m unittest discover --verbose tests #/test_python.py | ||
mkdir $out | ||
|
||
# run the tests by invoking the interpreter via full path | ||
echo "absolute path: ${attrs.interpreter}" | ||
${attrs.interpreter} -m unittest discover --verbose tests 2>&1 | tee "$out/full.txt" | ||
|
||
# run the tests by invoking the interpreter via $PATH | ||
export PATH="$(dirname ${attrs.interpreter}):$PATH" | ||
echo "PATH: $(basename ${attrs.interpreter})" | ||
"$(basename ${attrs.interpreter})" -m unittest discover --verbose tests 2>&1 | tee "$out/path.txt" | ||
|
||
# make sure we get the right path when invoking through a result link | ||
ln -s "${attrs.env}" result | ||
relative="result/bin/$(basename ${attrs.interpreter})" | ||
expected="$PWD/$relative" | ||
actual="$(./$relative -c "import sys; print(sys.executable)" | tee "$out/result.txt")" | ||
if [ "$actual" != "$expected" ]; then | ||
echo "expected $expected, got $actual" | ||
exit 1 | ||
fi | ||
|
||
# if we got this far, the tests passed | ||
touch $out/success | ||
''; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,14 +35,22 @@ let | |
fi | ||
mkdir -p "$out/bin" | ||
|
||
rm -f $out/bin/.*-wrapped | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are we sure that every wrapped executable in all of A short comment explaining why this is needed and safe would be appreciated. Is it just for the check below? |
||
|
||
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} | ||
if [ -f ".$prg-wrapped" ]; then | ||
echo "#!${pythonExecutable}" > "$out/bin/$prg" | ||
sed -e '1d' -e '3d' ".$prg-wrapped" >> "$out/bin/$prg" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The main reason this original PR got reverted, was the assumption that you can undo every wrapper by deleting those lines which sometimes deleted legit python code like for pretalx. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And it still deletes part of
vs https://github.com/pretalx/pretalx/blob/v2024.1.0/src/manage.py missing the os import. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @RuRo suggests a somewhat hardened sed expression in https://discourse.nixos.org/t/how-to-build-python-virtualenv-with-packages-provided-by-python3-withpackages/24766/20
|
||
chmod +x "$out/bin/$prg" | ||
else | ||
makeWrapper "$path/bin/$prg" "$out/bin/$prg" --inherit-argv0 --resolve-argv0 ${lib.optionalString (!permitUserSite) ''--set PYTHONNOUSERSITE "true"''} ${lib.concatStringsSep " " makeWrapperArgs} | ||
fi | ||
fi | ||
fi | ||
done | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's an empty string left here, I think that's not needed?