Skip to content

Commit 8e3169d

Browse files
committed
python: remove LD_LIBRARY_PATH hack from running python environment
Injecting `LD_LIBRARY_PATH` to the Python runtime environment is great to bypass the need of having to patch non-nix binaries loaded into that environment, however it breaks down, when Python executes any other program not compiled for the given Nix system, e.g. a shell script via `subprocess`. To work this around, `devenv` will inject a `pth`[^1] file to the virtual environment it creates, which mangles the `LD_LIBRARY_PATH` variable, undoing any changes to it made by `devenv` but preserving changes from other sources. Fixes cachix#1111 [^1]: https://docs.python.org/3/library/site.html
1 parent 5c046ee commit 8e3169d

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/modules/languages/python.nix

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ let
1414
python = cfg.package;
1515
requiredPythonModules = cfg.package.pkgs.requiredPythonModules;
1616
makeWrapperArgs = [
17+
"--set"
18+
"DEVENV_LD_LIBRARY_PATH_PREFIX"
19+
libraries
1720
"--prefix"
1821
"LD_LIBRARY_PATH"
1922
":"
@@ -39,6 +42,20 @@ let
3942
follows = [ "nixpkgs" ];
4043
};
4144

45+
pth_file =
46+
let
47+
exec_content = ''
48+
ld_library_path = os.environ.get("LD_LIBRARY_PATH")
49+
ld_library_path_prefix = os.environ.get("DEVENV_LD_LIBRARY_PATH_PREFIX")
50+
if ld_library_path and ld_library_path_prefix:
51+
if ld_library_path == ld_library_path_prefix:
52+
del os.environ["LD_LIBRARY_PATH"]
53+
else:
54+
os.environ["LD_LIBRARY_PATH"] = ld_library_path.removeprefix(ld_library_path_prefix + ":")
55+
'';
56+
in
57+
pkgs.writeText "devenv.pth" ''import os; exec("""${builtins.replaceStrings [ "\n" ] [ "\\n" ] exec_content}""")'';
58+
4259
initVenvScript =
4360
let
4461
USE_UV_SYNC = cfg.uv.sync.enable && builtins.compareVersions cfg.uv.package.version "0.4.4" >= 0;
@@ -80,6 +97,9 @@ let
8097
''
8198
}
8299
echo "${package.interpreter}" > "$VENV_PATH/.devenv_interpreter"
100+
${lib.optionalString pkgs.stdenv.isLinux ''
101+
ln -snf ${pth_file} "$VENV_PATH/${package.sitePackages}/devenv.pth"
102+
''}
83103
fi
84104
85105
source "$VENV_PATH"/bin/activate
@@ -157,6 +177,9 @@ let
157177
if "''${UV_SYNC_COMMAND[@]}"
158178
then
159179
echo "$ACTUAL_UV_CHECKSUM" > "$UV_CHECKSUM_FILE"
180+
${lib.optionalString pkgs.stdenv.isLinux ''
181+
ln -snf ${pth_file} "$VENV_PATH/${package.sitePackages}/devenv.pth"
182+
''}
160183
else
161184
echo "uv sync failed. Run 'uv sync' manually." >&2
162185
fi
@@ -206,6 +229,9 @@ let
206229
if ''${POETRY_INSTALL_COMMAND[@]}
207230
then
208231
echo "$ACTUAL_POETRY_CHECKSUM" > "$POETRY_CHECKSUM_FILE"
232+
${lib.optionalString pkgs.stdenv.isLinux ''
233+
ln -snf ${pth_file} ".venv/${package.sitePackages}/devenv.pth"
234+
''}
209235
else
210236
echo "Poetry install failed. Run 'poetry install' manually."
211237
fi

0 commit comments

Comments
 (0)