about summary refs log tree commit diff
path: root/pkgs/development/interpreters/python
diff options
context:
space:
mode:
authorFrederik Rietdijk <fridh@fridh.nl>2020-03-14 16:22:36 +0100
committeradisbladis <adisbladis@gmail.com>2020-03-14 21:39:32 +0000
commit7447fff95a4e091dc9ebe961d46cb4fbc32e69d1 (patch)
treea2363bb9301dda4d8c89b275f2a5411945adc13b /pkgs/development/interpreters/python
parent753122388d0080c8566872cb977f5841b22f1b4e (diff)
downloadnixlib-7447fff95a4e091dc9ebe961d46cb4fbc32e69d1.tar
nixlib-7447fff95a4e091dc9ebe961d46cb4fbc32e69d1.tar.gz
nixlib-7447fff95a4e091dc9ebe961d46cb4fbc32e69d1.tar.bz2
nixlib-7447fff95a4e091dc9ebe961d46cb4fbc32e69d1.tar.lz
nixlib-7447fff95a4e091dc9ebe961d46cb4fbc32e69d1.tar.xz
nixlib-7447fff95a4e091dc9ebe961d46cb4fbc32e69d1.tar.zst
nixlib-7447fff95a4e091dc9ebe961d46cb4fbc32e69d1.zip
Fix sys.prefix in case of a Nix env
The prefix will now be correct in case of Nix env.

Note, however, that creating a venv from a Nix env still does not function. This does not seem to be possible
with the current approach either, because venv will copy or symlink our Python wrapper. In case it symlinks
(the default) it won't see a pyvenv.cfg. If it is copied I think it should function but it does not...
Diffstat (limited to 'pkgs/development/interpreters/python')
-rw-r--r--pkgs/development/interpreters/python/sitecustomize.py21
-rw-r--r--pkgs/development/interpreters/python/tests/test_python.py4
2 files changed, 17 insertions, 8 deletions
diff --git a/pkgs/development/interpreters/python/sitecustomize.py b/pkgs/development/interpreters/python/sitecustomize.py
index e59d6070cb6f..72ce951328f1 100644
--- a/pkgs/development/interpreters/python/sitecustomize.py
+++ b/pkgs/development/interpreters/python/sitecustomize.py
@@ -21,10 +21,19 @@ paths = os.environ.pop('NIX_PYTHONPATH', None)
 if paths:
     functools.reduce(lambda k, p: site.addsitedir(p, k), paths.split(':'), site._init_pathinfo())
 
-prefixes = os.environ.pop('NIX_PYTHONPREFIX', None)
-if prefixes:
-    site.PREFIXES.extend(prefixes.split(':'))
+# Check whether we are in a venv. 
+# Note Python 2 does not support base_prefix so we assume we are not in a venv.
+in_venv = sys.version_info.major == 3 and sys.prefix != sys.base_prefix
 
-executable = os.environ.pop('NIX_PYTHONEXECUTABLE', None)
-if 'PYTHONEXECUTABLE' not in os.environ and executable:
-    sys.executable = executable
+if not in_venv:
+    executable = os.environ.pop('NIX_PYTHONEXECUTABLE', None)
+    prefix = os.environ.pop('NIX_PYTHONPREFIX', None)
+
+    if 'PYTHONEXECUTABLE' not in os.environ and executable is not None:
+        sys.executable = executable
+    if prefix is not None:
+        # Because we cannot check with Python 2 whether we are in a venv, 
+        # creating a venv from a Nix env won't work as well with Python 2.
+        # Also, note that sysconfig does not like it when sys.prefix is set to None
+        sys.prefix = sys.exec_prefix = prefix
+        site.PREFIXES.insert(0, prefix)
diff --git a/pkgs/development/interpreters/python/tests/test_python.py b/pkgs/development/interpreters/python/tests/test_python.py
index 93aa2bb2b9ef..011978c62547 100644
--- a/pkgs/development/interpreters/python/tests/test_python.py
+++ b/pkgs/development/interpreters/python/tests/test_python.py
@@ -27,7 +27,7 @@ class TestCasePython(unittest.TestCase):
     def test_interpreter(self):
         self.assertEqual(sys.executable, INTERPRETER)
 
-    @unittest.skipIf(IS_NIXENV or IS_PYPY, "Prefix is incorrect and needs to be fixed.")
+    @unittest.skipIf(IS_PYPY, "Prefix is incorrect and needs to be fixed.")
     def test_prefix(self):
         self.assertEqual(sys.prefix, ENV)
         self.assertEqual(sys.prefix, sys.exec_prefix)
@@ -37,7 +37,7 @@ class TestCasePython(unittest.TestCase):
 
     @unittest.skipIf(IS_PYPY or sys.version_info.major==2, "Python 2 does not have base_prefix")
     def test_base_prefix(self):
-        if IS_VENV:
+        if IS_VENV or IS_NIXENV:
             self.assertNotEqual(sys.prefix, sys.base_prefix)
         else:
             self.assertEqual(sys.prefix, sys.base_prefix)