-
-
Notifications
You must be signed in to change notification settings - Fork 19.8k
Python fix venv #358823
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
base: master
Are you sure you want to change the base?
Python fix venv #358823
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 |
|---|---|---|
|
|
@@ -42,7 +42,15 @@ let | |
| 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} | ||
|
Contributor
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. I think this is the only place where |
||
| if [ -f ".$prg-wrapped" ] && ( cat ".$prg-wrapped" | head -n 1 | grep -q "python" ) ; then | ||
| echo "#!${pythonExecutable}" >> "$out/bin/$prg" | ||
| echo "import os" >> "$out/bin/$prg" | ||
| echo 'os.environ["NIX_PYTHON_IN_ENV"] = "true"' >> "$out/bin/$prg" | ||
|
Contributor
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. Even if we assume that we really DO need to wrap/unwrap python scripts, I still don't like that we have to write to the environment here simply to disable a piece of code a few lines later in the same file. We are using the magic Since we control both the wrapping and the unwrapping logic, couldn't we massively simplify all this. Keep the preamble as it was before (always set
Contributor
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. Also, it might be better to replace the old shebang with the new one, instead of just prepending the new one. Prepending the new shebang means that the resulting derivation contains references to both the old and the new python executable, which might be confusing/suboptimal in terms of why-depends/requisites/closures.
Contributor
Author
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. Right the magic comment thing would work and is quite easy to do. I'm personally hesitant to do it that way because deleting anything carries the risk of breaking things. Could be that at some point something else in nixpkgs wraps a wrapped python executable and then this code tries to undo the preamble which proceeds to delete the other wrap because it ended up on the same line. I know it's very unlikely but it just doesn't seem like a clean approach, neither is my solution, it's awful in a different way. As for the still remaining reference, yeah you're right I didn't think of that. With my solution it's not possible to get rid of it. Thanks for the daily drive, I don't use any python things on my laptop sadly.
Contributor
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.
I don't think that this is possible, because that would imply that this hypothetical wrapping code inserts the code onto the same line with already existing code (which is broken anyway) instead of inserting a new line (like this wrapping code does). If anything, I think that my proposed solution might be slightly more robust, because it would actually verify (by checking for the presence of the magic comment) that the script that we are trying to unwrap was actually wrapped by Also, I noticed, that the current code doesn't clean up the
I think, that once we've verified that the Please, let me know if you are planning on adding any of the changes that I've suggested in the previous review. No pressure, if you are busy or unwilling to add these changes (I understand your reasoning for implementing the current approach, even if I still think that we ought to try to remove as much of this wrapping/unwrapping cruft as possible). I'd just like to know if it's worth waiting for you to add these changes or if I should make my own PR.
Contributor
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. Actually, now that I started thinking about the The "wrapping" performed in
The current "unwrapping" logic replaces However, this might be subtly wrong, since it only accounts for (2) in the original wrapping logic. At the very least, |
||
| cat ".$prg-wrapped" >> "$out/bin/$prg" | ||
| 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.
Honestly, I am still kind of conflicted about all this wrapping/unwrapping nonsense.
It seems to me that the
sys.argvandsite.addsitedirshenanigans here are basically doing the same thing as the removedsitecustomize.py. So shouldn't we also remove this "preamble" and instead just setsubstitutions.executableto an instance ofpythonthat already has all the required dependencies?Uh oh!
There was an error while loading. Please reload this page.
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.
I've thought about it some more, and it seems to me that the primary reason why we can't do this is that it would create a circular reference.
Ideally, instead of doing
site.addsitedirat runtime, the shebang in/nix/store/aaaa-python3.12-foo-1.0.0/bin/fooshould just be replaced with#!/nix/store/bbbb-python3-3.12.8-env/bin/python. Wherebbbbis an instance of python that would just "pick up" the correct environment (from/nix/store/bbbb-python3-3.12.8-env/lib/python3.12/site-packages).However,
/nix/store/bbbb-python3-3.12.8-env/lib/python3.12/site-packageswould also need to contain symlinks to/nix/store/aaaa-python3.12-foo-1.0.0/lib/python3.12/site-packages/libfoo(becausebin/fooalso needslibfoo). But this is not possible since this would create a circular dependencyaaaa (bin/foo) -> bbbb (bin/python) -> bbbb (lib/*/site-packages) -> aaaa (lib/*/site-packages/libfoo).I think that this could be solved without creating any circular references if #170577 (or something like it) were to be implemented. Alternatively, the whole environment could be included inside the
aaaaderivation (something like/nix/store/aaaa-python3.12-foo-1.0.0/nix-support/env/...).Honestly, all of this is kind of yucky 😒 (less yucky than the current situation, of course).
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.
You lost me :) i don't know nearly enough about python to effectively and correctly implement your suggestions. I went to fix the immediate problem if venvs being slightly broken, which I managed. I can implement the magic comment approach but as for fixing up the PR you linked, its probably beyond me
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.
IIRC, @cwp and some other people were also considering removing the wrapping/unwrapping logic, but it was never actually implemented. My comments above were mainly my way of documenting my thought process for why removing this extra wrapping is more complicated than it might seem at first glance.
So yeah, this is mostly me talking to myself, but also kind of hoping that somebody with a better understanding of the current
python3Packagesmachinery might swing by and sanity check my thoughts.Removing the preamble-wrap thing should probably be beyond the scope of this PR.
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.
Yeah no worries feel free to think out-loud, it helps and your input is very welcome. Might be good merge this, if no one has objections so that we have a partial if cursed solution already in. Dunno if anyone who knows enough about the python infra will show up soon.