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
Python: add sitecustomize.py, listen to NIX_PYTHONPATH and NIX_PYTHON_SCRIPT_NAME #25985
Conversation
@FRidh, thanks for your PR! By analyzing the history of the files in this pull request, we identified @domenkozar, @vcunat, @edolstra and @FRidh to be potential reviewers. |
What's the usecase? I'm afraid if this would lead to #17749. |
How about extending it to NIX_PYTHON${MAJOR}${MINOR}PATH? I'm looking for something to improve this config snippet:
|
In the PR you referred to you initially proposed using
I've thought of that before, but I don't think it is much of an improvement. It works, as long as we assume that all Instead, I would like to add |
@FRidh Indeed! An idea: we could also move our I'll review this properly a bit later today. |
That was also my idea! 😄 Unfortunately it is not possible though, because at that time |
ohh, but it is possible to use |
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'm quite intrigued by this -- can you try using sys.path_hooks
? If done correctly we can finally completely avoid patching Python programs. Imagine -- no more magicalSedExpression
! :D
@abbradar I checked out See the following code
Because Moving Therefore, if imports outside the hook are not considered, then I might as well just import
but that resulted in a stack overflow. It seems we'll be stuck with our |
@FRidh I succeeded doing this with the code provided in the mailing list;
I placed this into
Maybe when it's called from home directory |
@abbradar interesting. I didn't try the user site-packages, but instead modified the global So having the following in the user or system
Putting this together with the code from 4ee6ee37600c61df53aa7c51458ebdd13d40f3c7 doesn't work. However, if I remove 4ee6ee37600c61df53aa7c51458ebdd13d40f3c7 then this does work. |
Yay, success!
Notice how infinite recursion is avoided by removing the hook after |
Yay indeed! This is neat. The question is now how we look after these two env variables regarding leaking.
|
@FRidh Hmmm, indeed. I don't see any better way than patching Python modules in the case scripts reimport themselves (like before). We definitely don't want to leak both of those variables to child processes. |
@FRidh I feel stupid: actually if Python script reimports itself then all paths and fixed |
I've added two more commits
@abbradar when you say covered, do you mean our current implementation, or what we've been discussing here? |
@FRidh I mean what we have discussed. Let's again consider different cases when we use
So it seems that we can move to |
Yea, that's the really messy case. Luckily you won't find these in But now the case where I think this might break:
Unsetting is supported, we just need to test whether it works for us.
https://docs.python.org/2.7/library/os.html#os.environ |
I have a new idea on how can we make this work: what if we used custom Python wrappers? Like this:
Advantages:
Disadvantages:
Not sure how this could be fixed so it seems we are at least partially stuck with sed patching...Any idea on how to improve upon this? |
@abbradar nice idea, but I think you're missing a couple of cases here.
A nice test for the latter is
shows that In any case, I think the Python wrapper you suggest here we could have also as a "lightweight" alternative to the current About |
…_SCRIPT_NAME This commit adds a Nix-specific module that recursively adds paths that are on `NIX_PYTHONPATH` to `sys.path`. In order to process possible `.pth` files `site.addsitedir` is used. The paths listed in `PYTHONPATH` are added to `sys.path` afterwards, but they will be added before the entries we add here and thus take precedence. The reason for adding support for this environment variable is that we can set it in a wrapper without breaking support for `PYTHONPATH`. Additionally, this commit introduces `NIX_PYTHON_SCRIPT_NAME` which can be used for setting `sys.argv[0]`.
The NIX_PYTHONPATH part is now in #64634. |
Thank you for your contributions.
|
I marked this as stale due to inactivity. → More info |
Closing as it is basically abandoned, or solved by another PR. |
This pull request has been mentioned on NixOS Discourse. There might be relevant details there: https://discourse.nixos.org/t/terminal-emulator-leaks-environment-variables-to-shell/33673/11 |
This commit adds a Nix-specific module that recursively adds paths that
are on
NIX_PYTHONPATH
tosys.path
. In order to process possible.pth
filessite.addsitedir
is used.The paths listed in
PYTHONPATH
are added tosys.path
afterwards, butthey will be added before the entries we add here and thus take
precedence.
The reason for adding support for this environment variable is that we
can set it in a wrapper without breaking support for
PYTHONPATH
.Additionally, this commit introduces
NIX_PYTHON_SCRIPT_NAME
whichcan be used for setting
sys.argv[0]
.(nix.useSandbox on NixOS,
or option
build-use-sandbox
innix.conf
on non-NixOS)
nix-shell -p nox --run "nox-review wip"
./result/bin/
)