mirror of
https://github.com/ryan4yin/nixos-and-flakes-book.git
synced 2024-11-24 17:13:12 +01:00
feat: lazy evaluation (#121)
This commit is contained in:
parent
48a392d190
commit
32b31a6485
@ -185,11 +185,16 @@ sudo nixos-rebuild switch --flake github:owner/repo#your-hostname
|
||||
|
||||
### 3. Simple Introduction to `nixpkgs.lib.nixosSystem` Function {#simple-introduction-to-nixpkgs-lib-nixos-system}
|
||||
|
||||
By default, a flake will look for a `flake.nix` file in the root directory of each of its dependencies and execute its `outputs` function.
|
||||
The attribute set returned by this function is then passed as a parameter to the flake's own `outputs` function, allowing us to use the content provided by each dependency in our outputs.
|
||||
**A Flake can depend on other Flakes to utilize the features they provide.**
|
||||
|
||||
By default, a flake searches for a `flake.nix` file in the root directory of each of its dependencies (i.e., each item in `inputs`) and lazily evaluates their `outputs` functions. It then passes the attribute set returned by these functions as arguments to its own `outputs` function, enabling us to use the features provided by the other flakes within our current flake.
|
||||
|
||||
In the example in this section, [nixpkgs/flake.nix] will be executed when we run `sudo nixos-rebuild switch`. We can see from its source code that its `outputs` definition includes the `lib` attribute, which is used in our example:
|
||||
More precisely, the evaluation of the `outputs` function for each dependency is lazy. This means that a flake's `outputs` function is only evaluated when it is actually used, thereby avoiding unnecessary calculations and improving efficiency.
|
||||
|
||||
The description above may be a bit confusing, so let's take a look at the process with the `flake.nix` example used in this section.
|
||||
Our `flake.nix` declares the `inputs.nixpkgs` dependency, so that [nixpkgs/flake.nix] will be evaluated when we run the `sudo nixos-rebuild switch` command.
|
||||
|
||||
From the source code of the Nixpkgs repository, we can see that its flake outputs definition includes the `lib` attribute, and in our example, we use the `lib` attribute's `nixosSystem` function to configure our NixOS system:
|
||||
|
||||
```nix{8-13}
|
||||
{
|
||||
|
@ -179,9 +179,16 @@ sudo nixos-rebuild switch --flake github:owner/repo#your-hostname
|
||||
|
||||
### 3. `nixpkgs.lib.nixosSystem` 函数的简单介绍 {#simple-introduction-to-nixpkgs-lib-nixos-system}
|
||||
|
||||
默认情况下,flake 会在其每个依赖项的根目录下寻找 `flake.nix` 文件并执行它的 `outputs` 函数,并将该函数返回的 attribute set 作为参数传递给它自身的 `outputs` 函数,这样我们就能在 outputs 中使用各依赖项提供的内容了。
|
||||
**一个 Flake 可以依赖其他 Flakes,从而使用它们提供的功能**。
|
||||
|
||||
在我们这一节的例子中,[nixpkgs/flake.nix] 会在我们执行 `sudo nixos-rebuild swtich` 时被执行,我们能从其源码中看到它 outputs 的定义中有 `lib` 这个属性,我们的例子中就使用了 `lib` 属性中的 `nixosSystem` 这个函数:
|
||||
默认情况下,一个 flake 会在其每个依赖项(即 `inputs` 中的每一项)的根目录下寻找 `flake.nix` 文件并**懒惰求值**(lazy evaluation)它们的 `outputs` 函数,接着将这些函数返回的 attribute sets 作为参数传递给它自身的 `outputs` 函数,这样我们就能在当前 flake 中使用它所依赖的其他 flakes 提供的功能了。
|
||||
|
||||
更精确地说,对每个依赖项的 `outputs` 函数的求值都是懒惰(lazy)的,也就是说,一个 flake 的 `outputs` 函数只有在被真正使用到的时候才会被求值,这样就能避免不必要的计算,从而提高效率。
|
||||
|
||||
上面的描述可能有点绕,我们还是结合本节中使用的 `flake.nix` 示例来看看这个过程。
|
||||
我们的 `flake.nix` 声明了 `inputs.nixpkgs` 这个依赖项,因此
|
||||
[nixpkgs/flake.nix] 会在我们执行 `sudo nixos-rebuild swtich` 这个命令时被求值。
|
||||
从 Nixpkgs 仓库的源码中能看到它的 flake outputs 定义中有返回 `lib` 这个属性,我们的例子中就使用了 `lib` 属性中的 `nixosSystem` 这个函数来配置我们的 NixOS 系统:
|
||||
|
||||
```nix{8-13}
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user