Get PYTHONPATH from pkgs.python3.withPackages


I have noticed that rycee’s home-manager does not set PYTHONPATH when adding pkgs.python3.withPackages to the program list while nix-shell does it properly.

How can I get the PYTHONPATH such as the one built by nix-shell in order to set it as an environment variable manually in my home.nix ?

For instance if I evaluate:

let pkgs = import <nixpkgs> {}; in pkgs.python3.withPackages (pp: with pp; [ pytorchWithCuda ])

is there an attribute to that set that can help me retrieve the python env librairies?

I could then build the PYTHONPATH with lib.makeSearchPath.


I can guess that it should be held in an extraLibs attribute, but using nix repl I can’t seem to find it.


1 Like

you can pass a list of Python packages to e.g. python3.pkgs.makePythonPath if you need a PYTHONPATH, but you should avoid using PYTHONPATH.

This is intentional. In general, you don’t want to packages from one environment to bleed over to another.

if you just want PYTHONPATH to be set, you could just use the raw python packages themselves, instead of:

pkgs.python3.withPackages (pp: with pp; [ pytorchWithCuda ])


with pkgs.python3.pkgs; [ pytorchWithCuda ]

the shellhook on the python packages will execute in the shell, and this will add them to PYTHONPATH

you should avoid using PYTHONPATH .

I know, but it’s a request from one of my users.
For more context, NixOS is the only OS on which I could successfully install NVIDIA, CUDA and PyTorch with CUDA support on my server (tried Ubuntu and Manjaro). Yet another testimony in favor of it haha.

with pkgs.python3.pkgs; [ pytorchWithCuda ]

the shellhook on the python packages will execute in the shell

I’ve tried it but it seems that the shell hook is not executed on login if it’s set up from home-manager.

I ended up doing something like this:

{ config, pkgs, ... }:

  python-with-packages = pkgs.python3.withPackages (pp: with pp; [

    programs.home-manager.enable = true;
    home.stateVersion = "19.09";

    home.packages = [

    home.sessionVariables = {
      PYTHONPATH = "${python-with-packages}/${python-with-packages.sitePackages}";

So then the user can create a venv

nix-shell --pure -p python3Packages.virtualenv --run "python -m venv .venv"

and have PyTorch with CUDA

$ source .venv/bin/activate
$ python -c "import torch; print(torch.cuda.is_available())"