【问题标题】:How to run a `nix-shell` with a default.nix file?如何使用 default.nix 文件运行“nix-shell”?
【发布时间】:2017-09-08 01:14:02
【问题描述】:

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

当我运行命令时:

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

我得到了我所期望的——一个环境中的 shell,其中包含 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 时,似乎所有内容都已安装,但未设置 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】:

    您的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 我不明白这个建议:对我来说它看起来完全是错误的。

    【讨论】:

    • 感谢您的澄清。事实上,这就是导致我错误地认为我的配置正确的部分。谢谢。
    猜你喜欢
    • 2018-03-30
    • 2021-12-20
    • 2017-11-22
    • 1970-01-01
    • 1970-01-01
    • 2017-02-02
    • 1970-01-01
    • 2021-12-21
    • 1970-01-01
    相关资源
    最近更新 更多