Skip to content

Python: add sitecustomize.py, listen to NIX_PYTHONPATH - #64634

Merged
FRidh merged 2 commits into
NixOS:stagingfrom
FRidh:sitecustomize
Jul 13, 2019
Merged

FRidh merged 2 commits into
NixOS:stagingfrom
FRidh:sitecustomize

Conversation

@FRidh

@FRidh FRidh commented Jul 11, 2019

Copy link
Copy Markdown
Member
Motivation for this change

Split from #25985.

Add a sitecustomize.py file that listens to NIX_PYTHONPATH and unsets it afterwards. This gives the same feature as both PYTHONHOME and PYTHONPATH, however, without preventing the user from using those environment variables, and without leaking the variable.

This variable is now used in python.buildEnv, thereby solving issues with a leaking PYTHONHOME which could break nested Python programs/environments.

In the future we may choose to use this as well in buildPythonPackage.

Things done
  • Tested using sandboxing (nix.useSandbox on NixOS, or option sandbox in nix.conf on non-NixOS)
  • Built on platform(s)
    • NixOS
    • macOS
    • other Linux distributions
  • Tested via one or more NixOS test(s) if existing and applicable for the change (look inside nixos/tests)
  • Tested compilation of all pkgs that depend on this change using nix-shell -p nix-review --run "nix-review wip"
  • Tested execution of all binary files (usually in ./result/bin/)
  • Determined the impact on package closure size (by running nix path-info -S before and after)
  • Ensured that relevant documentation is up to date
  • Fits CONTRIBUTING.md.

@ofborg ofborg Bot added 6.topic: python Python is a high-level, general-purpose programming language. 10.rebuild-darwin: 501+ This PR causes many rebuilds on Darwin and should normally target the staging branches. 10.rebuild-darwin: 5001+ This PR causes many rebuilds on Darwin and must target the staging branches. 10.rebuild-linux: 501+ This PR causes many rebuilds on Linux and should normally target the staging branches. 10.rebuild-linux: 5001+ This PR causes many rebuilds on Linux and must target the staging branches. labels Jul 11, 2019
@risicle

risicle commented Jul 11, 2019

Copy link
Copy Markdown
Contributor

This is a really neat idea - I've been doing something similar in a much hackier way to get virtualenvs to work with less hassle - simply using sitecustomize.py to re-sort all of the paths beginning with /nix/ to the "end" of sys.path.

Moving towards preferring PYTHONPATH provided packages over nix-provided ones... is there a worry this will threaten the "integrity" (for want of a better word) of nix-provided python applications?

@FRidh

FRidh commented Jul 12, 2019

Copy link
Copy Markdown
Member Author

I don't follow you. This does not use PYTHONPATH. We may eventual use this also in other wrappers but I don't see any negative changes there.

@abbradar abbradar left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additionally, this commit introduces NIX_PYTHON_SCRIPT_NAME which
can be used for setting sys.argv[0].

Not in the commit, a rebasing mistake?

I needed to read on the previous discussions since I have completely forgotten what's the problem we are trying to solve :D

It's better than using PYTHONHOME. I like the idea of unsetting the variable; if we apply it to NIX_PYTHON_SCRIPT_NAME won't this solve its leakiness? I don't see any other problems with it, maybe I missed anything in #25985?

We still don't solve crazy cases like (5) from #25985 (comment) but that's a clear improvement.

@abbradar

abbradar commented Jul 12, 2019 •

Copy link
Copy Markdown
Member

And if we move to using them both in buildPythonPackage we finally can get rid of this abomination!. The world would definitely become a better place. So I'm all for going all in, again unless I miss anything.

FRidh added 2 commits July 12, 2019 13:26
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`.
@FRidh

FRidh commented Jul 12, 2019

Copy link
Copy Markdown
Member Author
  1. should simply not be done.

I've updated the commit message. Yes, we could use NIX_PYTHON_SCRIPT_NAME but I won't to test a bit more with exec -a, whether it really really does not work.

@risicle

risicle commented Jul 12, 2019 •

Copy link
Copy Markdown
Contributor

@FRidh my point is just that users may have custom entries in their runtime environment's PYTHONPATH and if these now get prioritized in front of NIX_PYTHONPATH entries, $ some-python-based-cli-tool may have one of its dependencies instead resolved to the runtime environment's and break.

It's possible I've misunderstood how this is being handled with makeWrapper though.

@FRidh

FRidh commented Jul 13, 2019

Copy link
Copy Markdown
Member Author

Ah, now I understand. No, this won't change.

@nixos-discourse

Copy link
Copy Markdown

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

6.topic: python Python is a high-level, general-purpose programming language. 10.rebuild-darwin: 501+ This PR causes many rebuilds on Darwin and should normally target the staging branches. 10.rebuild-darwin: 5001+ This PR causes many rebuilds on Darwin and must target the staging branches. 10.rebuild-linux: 501+ This PR causes many rebuilds on Linux and should normally target the staging branches. 10.rebuild-linux: 5001+ This PR causes many rebuilds on Linux and must target the staging branches.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants