mirror of
https://github.com/NixOS/nixpkgs.git
synced 2025-01-20 03:43:45 +00:00
Python: add sitecustomize.py, listen to NIX_PYTHONPATH
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`.
This commit is contained in:
parent
02afb228e2
commit
46409b5c32
@ -256,6 +256,11 @@ in with passthru; stdenv.mkDerivation ({
|
||||
|
||||
inherit passthru;
|
||||
|
||||
postFixup = ''
|
||||
# Include a sitecustomize.py file. Note it causes an error when it's in postInstall with 2.7.
|
||||
cp ${../../sitecustomize.py} $out/${sitePackages}/sitecustomize.py
|
||||
'';
|
||||
|
||||
enableParallelBuilding = true;
|
||||
|
||||
doCheck = false; # expensive, and fails
|
||||
|
@ -221,6 +221,9 @@ in with passthru; stdenv.mkDerivation {
|
||||
find $out/lib/python*/config-* -type f -print -exec nuke-refs -e $out '{}' +
|
||||
find $out/lib -name '_sysconfigdata*.py*' -print -exec nuke-refs -e $out '{}' +
|
||||
|
||||
# Include a sitecustomize.py file
|
||||
cp ${../sitecustomize.py} $out/${sitePackages}/sitecustomize.py
|
||||
|
||||
# Determinism: rebuild all bytecode
|
||||
# We exclude lib2to3 because that's Python 2 code which fails
|
||||
# We rebuild three times, once for each optimization level
|
||||
|
@ -137,6 +137,9 @@ in with passthru; stdenv.mkDerivation rec {
|
||||
|
||||
# Python on Nix is not manylinux1 compatible. https://github.com/NixOS/nixpkgs/issues/18484
|
||||
echo "manylinux1_compatible=False" >> $out/lib/${libPrefix}/_manylinux.py
|
||||
|
||||
# Include a sitecustomize.py file
|
||||
cp ${../sitecustomize.py} $out/${sitePackages}/sitecustomize.py
|
||||
'';
|
||||
|
||||
inherit passthru;
|
||||
|
@ -84,6 +84,10 @@ in with passthru; stdenv.mkDerivation {
|
||||
echo "Removing bytecode"
|
||||
find . -name "__pycache__" -type d -depth -exec rm -rf {} \;
|
||||
popd
|
||||
|
||||
# Include a sitecustomize.py file
|
||||
cp ${../sitecustomize.py} $out/${sitePackages}/sitecustomize.py
|
||||
|
||||
'';
|
||||
|
||||
doInstallCheck = true;
|
||||
|
18
pkgs/development/interpreters/python/sitecustomize.py
Normal file
18
pkgs/development/interpreters/python/sitecustomize.py
Normal file
@ -0,0 +1,18 @@
|
||||
"""
|
||||
This is a Nix-specific module for discovering modules built with Nix.
|
||||
|
||||
The module 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.
|
||||
|
||||
Note the `NIX_PYTHONPATH` environment variable is unset in order to prevent leakage.
|
||||
"""
|
||||
import site
|
||||
import os
|
||||
import functools
|
||||
|
||||
paths = os.environ.pop('NIX_PYTHONPATH', None)
|
||||
if paths:
|
||||
functools.reduce(lambda k, p: site.addsitedir(p, k), paths.split(':'), site._init_pathinfo())
|
Loading…
Reference in New Issue
Block a user