From 4242a3a8c3505ca7b259a9ff5d8085bd2d3205ee Mon Sep 17 00:00:00 2001 From: Gary Guo Date: Tue, 2 Jan 2024 13:46:16 +0000 Subject: [PATCH] Remove custom python override It turns out the python override is fixing the wrong problem. All module not found issue comes from the fact that `PATH` is overriden and wrong python3 binary is being used when a program is called from fusesoc. The change to `PATH` is caused by the python program wrapper, but in this case it's not necessary for fusesoc. Simply stopping wrapping fusesoc is enough to fix the issue. --- pkgs/python_ot/default.nix | 10 +++++++- pkgs/python_ot/python.nix | 20 --------------- pkgs/python_ot/sitecustomize.py | 44 --------------------------------- 3 files changed, 9 insertions(+), 65 deletions(-) delete mode 100644 pkgs/python_ot/python.nix delete mode 100644 pkgs/python_ot/sitecustomize.py diff --git a/pkgs/python_ot/default.nix b/pkgs/python_ot/default.nix index 7f6ae40..4cea96c 100644 --- a/pkgs/python_ot/default.nix +++ b/pkgs/python_ot/default.nix @@ -59,13 +59,21 @@ preferWheel = true; }; }; + + # Nix wraps python programs which causes PATH to change. We want to keep + # PATH in case a program needs to invoke user-defined programs. + dontwrap-overlay = final: prev: { + fusesoc = prev.fusesoc.overridePythonAttrs { + dontWrapPythonPrograms = true; + }; + }; in poetry2nix.mkPoetryEnv { projectDir = ./.; - python = import ./python.nix {python = python3;}; overrides = [ preferwheel-overlay buildreqs-overlay + dontwrap-overlay poetry2nix.defaultPoetryOverrides ]; } diff --git a/pkgs/python_ot/python.nix b/pkgs/python_ot/python.nix deleted file mode 100644 index 20948a7..0000000 --- a/pkgs/python_ot/python.nix +++ /dev/null @@ -1,20 +0,0 @@ -# Copyright lowRISC Contributors. -# Licensed under the MIT License, see LICENSE for details. -# SPDX-License-Identifier: MIT -# -# Hack, see https://github.com/charlottia/hdx/pull/1 -{...} @ inputs: let - python = - (inputs.python.override { - includeSiteCustomize = false; - }) - .overrideAttrs (final: prev: { - postInstall = - prev.postInstall - + '' - # Override sitecustomize.py with our NIX_PYTHONPATH-preserving variant. - cp ${./sitecustomize.py} $out/${final.passthru.sitePackages}/sitecustomize.py - ''; - }); -in - python.override {self = python;} diff --git a/pkgs/python_ot/sitecustomize.py b/pkgs/python_ot/sitecustomize.py deleted file mode 100644 index fa7ea3e..0000000 --- a/pkgs/python_ot/sitecustomize.py +++ /dev/null @@ -1,44 +0,0 @@ -# Copyright lowRISC Contributors -# Copyright Nixpkgs/NixOS Contributors -# Licensed under the MIT License, see LICENSE for details. -# SPDX-License-Identifier: MIT - -""" -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. - -Similarly, this module listens to the environment variable `NIX_PYTHONEXECUTABLE` -and sets `sys.executable` to its value. -""" -import site -import sys -import os -import functools - -paths = os.environ.get('NIX_PYTHONPATH') # Upstream version uses pop which removes the env -if paths: - functools.reduce(lambda k, p: site.addsitedir(p, k), paths.split(':'), site._init_pathinfo()) - -# Check whether we are in a venv or virtualenv. -# For Python 3 we check whether our `base_prefix` is different from our current `prefix`. -# For Python 2 we check whether the non-standard `real_prefix` is set. -# https://stackoverflow.com/questions/1871549/determine-if-python-is-running-inside-virtualenv -in_venv = (sys.version_info.major == 3 and sys.prefix != sys.base_prefix) or (sys.version_info.major == 2 and hasattr(sys, "real_prefix")) - -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: - # Sysconfig does not like it when sys.prefix is set to None - sys.prefix = sys.exec_prefix = prefix - site.PREFIXES.insert(0, prefix)