Why doesn't nix-shell -E install python packages

-p works fine:

% nix-shell -p 'python38.withPackages(ps: with ps; [ numpy toolz ])' --run 'python3 -c "import numpy; print(numpy)"'
<module 'numpy' from '/nix/store/fgrhd0k14195zb9gnsrw71bybi6b00q8-python3-3.8.8-env/lib/python3.8/site-packages/numpy/__init__.py'>

Why doesn’t -E do the same thing?

nix-shell -E 'let pkgs = (import <nixpkgs> {}); in pkgs.python3.withPackages(ps: with ps; [numpy])' --run 'python3 -c "import numpy"'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'numpy'

Because the latter is not equivalent. It provides an environment suitable to build the given expression. Not an environment that has what is in the result of the expression.

man nix-shell even has some shirt section how to translate -p to -E using mkShell (IIRC) and it’s buildInputs.

Add .env

$ nix-shell -E '(let pkgs = (import <nixpkgs> {}); in pkgs.python3.withPackages(ps: with ps; [numpy])).env' --run 'python3 -c "import numpy; print(numpy)"'
2 Likes

shirt section?

1 Like

It was typed on a mobile, and that “corrected” “short” to “shirt” :smiley:

1 Like

well what ever it was, it gave me a much needed LOL, and we got a bit of nix tee’s marketing for free.