Python: Difference between revisions
m →Regression: remove unneeded text from command samples |
m →Using nix-ld: use <syntaxhighlight lang=console> for shell commands |
||
| (2 intermediate revisions by one other user not shown) | |||
| Line 195: | Line 195: | ||
(pkgs.writeShellScriptBin "python" '' | (pkgs.writeShellScriptBin "python" '' | ||
export LD_LIBRARY_PATH=$NIX_LD_LIBRARY_PATH | export LD_LIBRARY_PATH=$NIX_LD_LIBRARY_PATH | ||
exec ${pkgs.python3}/bin/python "$@" | exec -a "$0" ${pkgs.python3}/bin/python "$@" | ||
'') | '') | ||
]; | ]; | ||
| Line 207: | Line 207: | ||
# }; | # }; | ||
} | } | ||
</syntaxhighlight>While the above solution changes the NIX_LD_LIBRARY_PATH of python globally (which you may want to avoid since it also pollutes normal python), you can also create a new executable, say pythonld, that will set this environment variable like:<syntaxhighlight lang="nix"> | |||
{ | |||
programs.nix-ld = { | |||
enable = true; | |||
libraries = with pkgs; [ | |||
zlib zstd stdenv.cc.cc curl openssl attr libssh bzip2 libxml2 acl libsodium util-linux xz systemd | |||
]; | |||
}; | |||
# Usage: to create a venv, just do: | |||
# $ pythonld -m venv myvenv | |||
# $ source myvenv/bin/activate | |||
# $ pip install … | |||
# https://github.com/nix-community/nix-ld?tab=readme-ov-file#my-pythonnodejsrubyinterpreter-libraries-do-not-find-the-libraries-configured-by-nix-ld | |||
# Important: don't forget the -a "$0" or venv will skip your wrapper. | |||
environment.systemPackages = [ | |||
(pkgs.writeShellScriptBin "pythonld" '' | |||
export LD_LIBRARY_PATH=$NIX_LD_LIBRARY_PATH | |||
exec -a "$0" ${pkgs.python3}/bin/python "$@" | |||
'') | |||
]; | |||
} | |||
</syntaxhighlight>Then, you can use your own pythonld in a specific virtual env like:<syntaxhighlight lang="console"> | |||
$ pythonld -m venv myvenv | |||
$ source myvenv/bin/activate | |||
$ pip install … | |||
$ python | |||
… this python has dependencies installed via pip installed … | |||
</syntaxhighlight>If you want to also install system-wide nix dependencies like numpy so that you don't need to reinstall them in every venv, you can also install them system-wide via:<syntaxhighlight lang="nix"> | |||
{ | |||
programs.nix-ld = { | |||
enable = true; | |||
libraries = with pkgs; [ | |||
zlib zstd stdenv.cc.cc curl openssl attr libssh bzip2 libxml2 acl libsodium util-linux xz systemd | |||
]; | |||
}; | |||
# Usage: to create a venv, just do: | |||
# $ pythonld -m venv --system-site-packages myvenv | |||
# $ source myvenv/bin/activate | |||
# $ pip install … | |||
# https://github.com/nix-community/nix-ld?tab=readme-ov-file#my-pythonnodejsrubyinterpreter-libraries-do-not-find-the-libraries-configured-by-nix-ld | |||
# Important: don't forget the -a "$0" or venv will skip your wrapper. | |||
environment.systemPackages = [ | |||
(pkgs.writeShellScriptBin "pythonld" '' | |||
export LD_LIBRARY_PATH=$NIX_LD_LIBRARY_PATH | |||
exec -a "$0" ${(pkgs.python3.withPackages (ps: with ps; [ numpy ]))}/bin/python "$@" | |||
'') | |||
]; | |||
} | |||
</syntaxhighlight>And then use it like:<syntaxhighlight lang="console"> | |||
$ pythonld -m venv --system-site-packages myvenv | |||
$ source myvenv/bin/activate | |||
$ pip install … | |||
$ python | |||
… this python has dependencies installed via pip installed … | |||
</syntaxhighlight> | </syntaxhighlight> | ||