Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python: remove LD_LIBRARY_PATH hack from running python environment #1562

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/modules/languages/python.nix
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ let
python = cfg.package;
requiredPythonModules = cfg.package.pkgs.requiredPythonModules;
makeWrapperArgs = [
"--set"
"DEVENV_LD_LIBRARY_PATH_PREFIX"
libraries
"--prefix"
"LD_LIBRARY_PATH"
":"
Expand All @@ -39,6 +42,20 @@ let
follows = [ "nixpkgs" ];
};

pth_file =
let
exec_content = ''
ld_library_path = os.environ.get("LD_LIBRARY_PATH")
ld_library_path_prefix = os.environ.get("DEVENV_LD_LIBRARY_PATH_PREFIX")
if ld_library_path and ld_library_path_prefix:
if ld_library_path == ld_library_path_prefix:
del os.environ["LD_LIBRARY_PATH"]
else:
os.environ["LD_LIBRARY_PATH"] = ld_library_path.removeprefix(ld_library_path_prefix + ":")
'';
in
pkgs.writeText "devenv.pth" ''import os; exec("""${builtins.replaceStrings [ "\n" ] [ "\\n" ] exec_content}""")'';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need exec? I thought it might be possible to be executed in pth as-is.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pth is executed line by line, so this is the only way to use multi-line constructs like if conditions


initVenvScript =
let
USE_UV_SYNC = cfg.uv.sync.enable && builtins.compareVersions cfg.uv.package.version "0.4.4" >= 0;
Expand Down Expand Up @@ -80,6 +97,9 @@ let
''
}
echo "${package.interpreter}" > "$VENV_PATH/.devenv_interpreter"
${lib.optionalString pkgs.stdenv.isLinux ''
ln -snf ${pth_file} "$VENV_PATH/${package.sitePackages}/devenv.pth"
''}
fi

source "$VENV_PATH"/bin/activate
Expand Down Expand Up @@ -157,6 +177,9 @@ let
if "''${UV_SYNC_COMMAND[@]}"
then
echo "$ACTUAL_UV_CHECKSUM" > "$UV_CHECKSUM_FILE"
${lib.optionalString pkgs.stdenv.isLinux ''
ln -snf ${pth_file} "$VENV_PATH/${package.sitePackages}/devenv.pth"
''}
else
echo "uv sync failed. Run 'uv sync' manually." >&2
exit 1
Expand Down Expand Up @@ -186,6 +209,10 @@ let

# Make sure poetry's venv uses the configured Python executable.
${cfg.poetry.package}/bin/poetry env use --no-interaction --quiet ${package.interpreter}

${lib.optionalString pkgs.stdenv.isLinux ''
ln -snf ${pth_file} ".venv/${package.sitePackages}/devenv.pth"
''}
}

function _devenv_poetry_install
Expand Down
11 changes: 11 additions & 0 deletions tests/python-library-path-poetry/.test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
python - <<EOF
import os

assert "LD_LIBRARY_PATH" not in os.environ
EOF

LD_LIBRARY_PATH=set-from-shell python - <<EOF
import os

assert os.environ["LD_LIBRARY_PATH"] == "set-from-shell"
EOF
9 changes: 9 additions & 0 deletions tests/python-library-path-poetry/devenv.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{ pkgs, ... }:

{
languages.python = {
enable = true;
poetry.enable = true;
libraries = [ pkgs.file ];
};
}
9 changes: 9 additions & 0 deletions tests/python-library-path-poetry/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[tool.poetry]
name = "python-ld-library-path"
version = "0.1.0"
description = ""
authors = ["Your Name <[email protected]>"]

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
13 changes: 13 additions & 0 deletions tests/python-library-path-uv/.test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
python - <<EOF
import os

assert "LD_LIBRARY_PATH" not in os.environ
EOF


LD_LIBRARY_PATH=set-from-shell python - <<EOF
import os
import sys
print(sys.prefix)
assert os.environ["LD_LIBRARY_PATH"] == "set-from-shell"
EOF
10 changes: 10 additions & 0 deletions tests/python-library-path-uv/devenv.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{ pkgs, ... }:

{
languages.python = {
enable = true;
venv.enable = true;
uv.enable = true;
libraries = [ pkgs.file ];
};
}
11 changes: 11 additions & 0 deletions tests/python-library-path/.test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
python - <<EOF
import os

assert "LD_LIBRARY_PATH" not in os.environ
EOF

LD_LIBRARY_PATH=set-from-shell python - <<EOF
import os

assert os.environ["LD_LIBRARY_PATH"] == "set-from-shell"
EOF
9 changes: 9 additions & 0 deletions tests/python-library-path/devenv.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{ pkgs, ... }:

{
languages.python = {
enable = true;
venv.enable = true;
libraries = [ pkgs.file ];
};
}