From c6bd0ded16718ec1441856c501bde9074dd8c8f9 Mon Sep 17 00:00:00 2001 From: Djordje Nedic Date: Tue, 22 Oct 2024 00:19:14 +0200 Subject: [PATCH] fix(tools): Detect Nix use and work around it for installation Nix will create a new derivation in the store for python packages, which leads to mismatch between the system prefixes, invalidating a widely used check for being in a virtual environment. This adds a Nix-specific fix for virtualenv detection. Closes https://github.com/espressif/esp-idf/pull/14435 --- tools/idf_tools.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tools/idf_tools.py b/tools/idf_tools.py index a928c0465671..17617c15056d 100755 --- a/tools/idf_tools.py +++ b/tools/idf_tools.py @@ -2574,7 +2574,10 @@ def action_install_python_env(args): # type: ignore reinstall = args.reinstall idf_python_env_path, _, virtualenv_python, idf_version = get_python_env_path() - is_virtualenv = hasattr(sys, 'real_prefix') or (hasattr(sys, 'base_prefix') and sys.base_prefix != sys.prefix) + nix_store = os.environ.get('NIX_STORE') + is_nix = nix_store is not None and sys.base_prefix.startswith(nix_store) and sys.prefix.startswith(nix_store) + + is_virtualenv = not is_nix and (hasattr(sys, 'real_prefix') or (hasattr(sys, 'base_prefix') and sys.base_prefix != sys.prefix)) if is_virtualenv and (not os.path.exists(idf_python_env_path) or reinstall): fatal('This script was called from a virtual environment, can not create a virtual environment again') raise SystemExit(1)