<nixpkgs> 字符串/值在 Nix 中意味着什么?

What does the <nixpkgs> string / value mean in Nix?

例如下面的例子(我假设是一个 nix 表达式):

(import <nixpkgs> {}).haskellPackages.ghcWithPackages (hpkgs: with hpkgs; [
    lens
    aeson
    turtle
])

<nixpkgs>指的是什么?我也看到它在其他情况下使用,例如:

nix-shell '<nixpkgs>' -A linuxPackages.kernel

<nixpkgs> 是一个 Nix 表达式,它通过查看 NIX_PATH 环境变量 and/or -I 选项中的 Nix 搜索路径来求值。

Nix manual 中有更详细的描述。

请注意,Nix 搜索路径在很多情况下都不实用。只能从外面传,很容易产生杂质。根据我的经验,通过显式参数传递或与固定点相关的函数(如 callPackage 和覆盖系统)可以更好地解决问题。

举个例子,NixOS只有一个额外的搜索路径参数,如果没有给出明确的配置,它只会在nixos/default.nix中被读取一次。这样,您就可以灵活地提供自己的配置,这就是为什么您 (nix-build) 和 hydra 可以自信地构建 NixOS VM 测试、可启动图像、docker 图像等

可以使用 nix repl:

计算值
 nix repl
Welcome to Nix version 2.1.3. Type :? for help.

nix-repl> <nixpkgs>
/nix/var/nix/profiles/per-user/root/channels/nixos

来自 Nix manual, 15.1. Values,部分 "Simple Values":

Paths can also be specified between angle brackets, e.g. . This means that the directories listed in the environment variable NIX_PATH will be searched for the given file or directory name.

来自 NixOS manual, Chapter 18. Common Environment Variables,部分 NIX_PATH

A colon-separated list of directories used to look up Nix expressions enclosed in angle brackets (i.e., ). For instance, the value

/home/eelco/Dev:/etc/nixos

will cause Nix to look for paths relative to /home/eelco/Dev and /etc/nixos, in that order. It is also possible to match paths against a prefix. For example, the value

nixpkgs=/home/eelco/Dev/nixpkgs-branch:/etc/nixos

will cause Nix to search for <nixpkgs/path> in /home/eelco/Dev/nixpkgs-branch/path and /etc/nixos/nixpkgs/path.

If a path in the Nix search path starts with http:// or https://, it is interpreted as the URL of a tarball that will be downloaded and unpacked to a temporary location. The tarball must consist of a single top-level directory. For example, setting NIX_PATH to

nixpkgs=https://github.com/NixOS/nixpkgs-channels/archive/nixos-15.09.tar.gz

tells Nix to download the latest revision in the Nixpkgs/NixOS 15.09 channel.

A following shorthand can be used to refer to the official channels:

nixpkgs=channel:nixos-15.09

The search path can be extended using the -I option, which takes precedence over NIX_PATH.

例子

1。 with import <nixpkgs> {}; /* rest of the expression */

在我的例子中,<nixpkgs>/nix/var/nix/profiles/per-user/root/channels/nixos:

$ echo $NIX_PATH 
#                                  VVVVVVV
/home/a_user/.nix-defexpr/channels:nixpkgs=/nix/var/nix/profiles/per-user/root/channels/nixos:nixos-config=/etc/nixos/configuration.nix:/nix/var/nix/profiles/per-user/root/channels
#                                  ^^^^^^^

因为 <nixpkgs> 评估为“一个目录,该目录中的文件 default.niximport 加载”。 (Nix manual, 15.4.1. Advanced Attributes,段导入路径,builtins.import路径)

$ ll /nix/var/nix/profiles/per-user/root/channels/nixos
lrwxrwxrwx 1 root root 80 Dec 31  1969 /nix/var/nix/profiles/per-user/root/channels/nixos -> /nix/store/ywlfq2ns4
a3fzb2ap74lvahmrg1p0lmk-nixos-19.03.172231.7b36963e7a7/nixos/

$ ll $(readlink -f /nix/var/nix/profiles/per-user/root/channels/nixos)
total 3308
dr-xr-xr-x  8 root root    4096 Dec 31  1969 ./
dr-xr-xr-x  4 root root    4096 Dec 31  1969 ../
# (...)
dr-xr-xr-x  7 root root    4096 Dec 31  1969 nixos/
dr-xr-xr-x 17 root root    4096 Dec 31  1969 pkgs/
-r--r--r--  1 root root    1097 Dec 31  1969 COPYING
-r--r--r--  1 root root     968 Dec 31  ---> default.nix <---
# (...)

如果我的理解是正确的,在 import 之后,使用空属性集 ({}) 评估提供的 Nix 表达式。结果是一个属性列表,with expression 将在本地词法范围内包含所有包含它的属性。

2。 nix repl '<nixpkgs/nixos>'

取自 NixOS manual, 5.3. Modularity 的示例,显示了 repl.

中的活动 NixOS 配置设置
# On NixOS 19.03

$ nix repl

Welcome to Nix version 2.2.2. Type :? for help.

nix-repl> <nixpkgs>
/nix/var/nix/profiles/per-user/root/channels/nixos

nix-repl> <nixpkgs/nixos>
/nix/var/nix/profiles/per-user/root/channels/nixos/nixos

在 repl 上加载系统 NixOS 配置:

nix-repl> :l <nixpkgs/nixos>
Added 6 variables.

从 Repl 上的 Nixpkgs 加载所有 Nix 表达式:

nix-repl> :l <nixpkgs>
Added 10089 variables.

或者直接加载它们 repl:

$ nix repl '<nixpkgs>'

Welcome to Nix version 2.2.2. Type :? for help.

Loading '<nixpkgs>'...
Added 10089 variables.

$ nix repl '<nixpkgs/nixos>'

Welcome to Nix version 2.2.2. Type :? for help.

Loading '<nixpkgs/nixos>'...
Added 6 variables.

备忘单:

nix-repl> :help
The following commands are available:

  <expr>        Evaluate and print expression
  <x> = <expr>  Bind expression to variable
  :a <expr>     Add attributes from resulting set to scope
  :b <expr>     Build derivation
  :i <expr>     Build derivation, then install result into current profile
  :l <path>     Load Nix expression and add it to scope
  :p <expr>     Evaluate and print expression recursively
  :q            Exit nix-repl
  :r            Reload all files
  :s <expr>     Build dependencies of derivation, then start nix-shell
  :t <expr>     Describe result of evaluation
  :u <expr>     Build derivation, then start nix-shell

因为 <nixpkgs/path> 约定(其中 path 等于 nixos) ,角度表达式的计算结果为 /nix/var/nix/profiles/per-user/root/channels/nixos/nixos。上面的 ll 输出还在 default.nix 上面显示了一个 nixos 文件夹,里面确实有另一个 default.nix 将被 nix repl 评估:

$ ll $(readlink -f /nix/var/nix/profiles/per-user/root/channels/nixos/nixos)
total 72
dr-xr-xr-x  7 root root  4096 Dec 31  1969 ./
dr-xr-xr-x  8 root root  4096 Dec 31  1969 ../
-r--r--r--  1 root root   886 Dec 31  ---> default.nix <---
-r--r--r--  1 root root   197 Dec 31  1969 README
-r--r--r--  1 root root  6074 Dec 31  1969 release-combined.nix
-r--r--r--  1 root root  9251 Dec 31  1969 release.nix
-r--r--r--  1 root root  2038 Dec 31  1969 release-small.nix

其他