如何使用default.nix文件运行`nix-shell`?
问题描述:
我想了解nix是如何工作的。为了这个目的,我尝试创建一个运行jupyter笔记本的简单环境。如何使用default.nix文件运行`nix-shell`?
当我运行命令:
nix-shell -p "\
with import <nixpkgs> {};\
python35.withPackages (ps: [\
ps.numpy\
ps.toolz\
ps.jupyter\
])\
"
我得到了我的预期 - 在与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
虽然,好像是安装一切,但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
通过什么我read here我期待这两种情况是等价的。我做错了什么?
答
您的default.nix
文件应该保存信息以在与nix-build
调用时建立派生。当使用nix-shell
来调用它时,它只是以可派生的方式设置外壳。特别是,它设置PATH变量包含在buildInput
属性中列出的所有内容:
with import <nixpkgs> {};
stdenv.mkDerivation {
name = "my-env";
# src = ./.;
buildInputs =
python35.withPackages (ps: [
ps.numpy
ps.toolz
ps.jupyter
]);
}
这里,我注释掉,如果你想运行nix-build
这是必需的,但没有必要在src
属性当你刚刚运行nix-shell
。
在你最后一句话中,我想你更准确地指出了这一点:https://github.com/NixOS/nixpkgs/blob/master/doc/languages-frameworks/python.md#load-environment-from-nix-expression 我不明白这个建议:对我来说,它看起来很简单。
感谢您的澄清。事实上,这是导致我错误地认为我拥有正确配置的部分。谢谢。 –