如何 运行 带有 default.nix 文件的 `nix-shell`?

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

我正在尝试了解 nix 的工作原理。为此,我尝试为 运行 jupyter notebooks 创建一个简单的环境。

当我运行命令时:

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

我得到了我期望的结果——在 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

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

with import <nixpkgs> {};

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

当我 运行 nix-shell 在此文件夹中时,似乎所有内容都已安装但未设置 PATHs:

[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 我原以为这两种情况是等价的。我做错了什么?

您的 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
    ]);

}

在这里,我已经注释掉了 src 属性,如果您想要 运行 nix-build,则该属性是必需的,但如果您只是 运行,则不需要宁nix-shell.

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