如何使用default.nix文件运行`nix-shell`?

Raf*_*ini 7 python nix

我试图理解nix是如何工作的.为此,我尝试创建一个简单的环境来运行jupyter笔记本.

当我运行命令时:

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

我得到了我的期望 - 在python和安装的所有软件包的环境中的shell,以及路径中可访问的所有预期命令:

[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
Run Code Online (Sandbox Code Playgroud)

因此,我创建了一个default.nix包含以下内容的单个文件的新文件夹:

with import <nixpkgs> {};

python35.withPackages (ps: [
  ps.numpy 
  ps.toolz
  ps.jupyter
])
Run Code Online (Sandbox Code Playgroud)

当我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
Run Code Online (Sandbox Code Playgroud)

通过我在这里读到的,我期待这两种情况是等价的.我做错了什么?

Zim*_*i48 8

您的default.nix文件应该保存信息以在使用时构建派生nix-build.在调用它时nix-shell,它只是以可构建派生的方式设置shell.特别是,它将PATH变量设置为包含buildInput属性中列出的所有内容:

with import <nixpkgs> {};

stdenv.mkDerivation {

  name = "my-env";

  # src = ./.;

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

}
Run Code Online (Sandbox Code Playgroud)

在这里,我已经注释掉了src想要运行时所需的属性,nix-build但是当你刚刚运行时则没有必要nix-shell.

在你的最后一句话中,我想你更准确地指的是这个:https://github.com/NixOS/nixpkgs/blob/master/doc/languages-frameworks/python.md#load-environment-from-nix-expression 我不明白这个建议:对我而言,这看起来很简单.