1"""
2This is a Nix-specific module for discovering modules built with Nix.
3
4The module recursively adds paths that are on `NIX_PYTHONPATH` to `sys.path`. In
5order to process possible `.pth` files `site.addsitedir` is used.
6
7The paths listed in `PYTHONPATH` are added to `sys.path` afterwards, but they
8will be added before the entries we add here and thus take precedence.
9
10Note the `NIX_PYTHONPATH` environment variable is unset in order to prevent leakage.
11
12Similarly, this module listens to the environment variable `NIX_PYTHONEXECUTABLE`
13and sets `sys.executable` to its value.
14"""
15import site
16import sys
17import os
18import functools
19
20paths = os.environ.pop('NIX_PYTHONPATH', None)
21if paths:
22 functools.reduce(lambda k, p: site.addsitedir(p, k), paths.split(':'), site._init_pathinfo())
23
24executable = os.environ.pop('NIX_PYTHONEXECUTABLE', None)
25if 'PYTHONEXECUTABLE' not in os.environ and executable:
26 sys.executable = executable