Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a `nix-shell` with a default.nix file?

Tags:

python

nix

I'm trying to understand how nix works. For that purposed I tried to create a simple environment to run jupyter notebooks.

When I run the command:

nix-shell -p "\
    with import <nixpkgs> {};\
    python35.withPackages (ps: [\
        ps.numpy\
        ps.toolz\
        ps.jupyter\
    ])\
    "

I get what I expected -- a shell in an environment with python and the all packages listed installed, and the all expected commands accessible in the path:

[nix-shell:~/dev/hurricanes]$ which python
/nix/store/5scsbf8z3jnz8ardch86mhr8xcyc8jr2-python3-3.5.3-env/bin/python

[nix-shell:~/dev/hurricanes]$ which jupyter
/nix/store/5scsbf8z3jnz8ardch86mhr8xcyc8jr2-python3-3.5.3-env/bin/jupyter

[nix-shell:~/dev/hurricanes]$ jupyter notebook
[I 22:12:26.191 NotebookApp] Serving notebooks from local directory: /home/calsaverini/dev/hurricanes
[I 22:12:26.191 NotebookApp] 0 active kernels 
[I 22:12:26.191 NotebookApp] The Jupyter Notebook is running at: http://localhost:8888/?token=7424791f6788af34f4c2616490b84f0d18353a4d4e60b2b5

So, I created a new folder with a single default.nix file with the following contents:

with import <nixpkgs> {};

python35.withPackages (ps: [
  ps.numpy 
  ps.toolz
  ps.jupyter
])

When I run nix-shell in this folder though, it seems like everything is installed but the PATHs are not set:

[nix-shell:~/dev/hurricanes]$ which python
/usr/bin/python

[nix-shell:~/dev/hurricanes]$ which jupyter

[nix-shell:~/dev/hurricanes]$ jupyter
The program 'jupyter' is currently not installed. You can install it by typing:
sudo apt install jupyter-core

By what I read here I was expecting the two situations to be equivalent. What did I do wrong?

like image 673
Rafael S. Calsaverini Avatar asked Sep 08 '17 01:09

Rafael S. Calsaverini


1 Answers

Your default.nix file is supposed to hold the information to build a derivation when calling it with nix-build. When calling it with nix-shell, it just sets the shell in a way that the derivation is buildable. In particular, it sets the PATH variable to contain everything that is listed in the buildInput attribute:

with import <nixpkgs> {};

stdenv.mkDerivation {

  name = "my-env";

  # src = ./.;

  buildInputs =
    python35.withPackages (ps: [
      ps.numpy 
      ps.toolz
      ps.jupyter
    ]);

}

Here, I've commented out the src attribute which is required if you want to run nix-build but isn't necessary when your are just running nix-shell.

In your last sentence, I suppose you are referring more precisely to this: https://github.com/NixOS/nixpkgs/blob/master/doc/languages-frameworks/python.section.md#load-environment-from-nix-expression I don't understand this advice: to me it just looks plain false.

like image 99
Zimm i48 Avatar answered Oct 04 '22 10:10

Zimm i48