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

问题描述 投票:6回答:1

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

当我运行命令时:

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我期待这两种情况是相同的。我做错了什么?

python nix
1个回答
7
投票

您的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我不明白这个建议:对我而言,它看起来很简单。

© www.soinside.com 2019 - 2024. All rights reserved.