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

标签 python nix

我正在尝试了解 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 时,似乎一切都已安装,但未设置 PATH:

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

关于python - 如何使用 default.nix 文件运行 `nix-shell`?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46107199/

相关文章:

Nix 和服务的运行和停止

nix - <nixpkgs> 字符串/值在 Nix 中是什么意思?

nix - 如何在家庭管理器中使用覆盖从不稳定安装 jbake

python - python中的点对点SSL连接

python - self.__class__ 的错误类继承?

python - 从字符串中添加不同的项目到列表中

python - 警告 :tensorflow:TensorFlowDNNRegressor class is deprecated. 请考虑使用 DNNRegressor 作为替代方案

ruby - Nix:用 Ruby 编译 Vim

nix - 漂亮的 nix attrsets 打印

python - 如何从谷歌趋势中提取标题/文本并通过 Selenium 和 Python 打印它们