2017-09-08 34 views
2

我想了解nix是如何工作的。爲了這個目的,我嘗試創建一個運行jupyter筆記本的簡單環境。如何使用default.nix文件運行`nix-shell`?

當我運行命令:

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

我得到了我的預期 - 在與Python的環境中路徑訪問的外殼和安裝列出的所有包,以及所有預期的命令:

[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 

所以,我創建了一個新的文件夾與單個文件default.nix具有以下內容:

with import <nixpkgs> {}; 

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

當我在此文件夾中運行nix-shell雖然,好像是安裝一切,但PATH s的未設置:

[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 

通過什麼我read here我期待這兩種情況是等價的。我做錯了什麼?

回答

2

您的default.nix文件應該保存信息以在與nix-build調用時建立派生。當使用nix-shell來調用它時,它只是以可派生的方式設置外殼。特別是,它設置PATH變量包含在buildInput屬性中列出的所有內容:

with import <nixpkgs> {}; 

stdenv.mkDerivation { 

    name = "my-env"; 

    # src = ./.; 

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

} 

這裏,我註釋掉,如果你想運行nix-build這是必需的,但沒有必要在src屬性當你剛剛運行nix-shell

在你最後一句話中,我想你更準確地指出了這一點:https://github.com/NixOS/nixpkgs/blob/master/doc/languages-frameworks/python.md#load-environment-from-nix-expression 我不明白這個建議:對我來說,它看起來很簡單。

+1

感謝您的澄清。事實上,這是導致我錯誤地認爲我擁有正確配置的部分。謝謝。 –