Skip to content

Commit

Permalink
feat: lazy evaluation
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan4yin committed Mar 16, 2024
1 parent 48a392d commit 79b45d6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
6 changes: 4 additions & 2 deletions docs/nixos-with-flakes/nixos-with-flakes-enabled.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,10 @@ 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.
By default, a flake will look for a `flake.nix` file in the root directory of each of its dependencies and **lazily** evaluates 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 within our outputs.

More precisely, the evaluation of the `outputs` function for each dependency is lazy, meaning that a flake's `outputs` function is only evaluated when it is actually used. This avoids unnecessary calculations and thereby improves efficiency.


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:
Expand Down
10 changes: 8 additions & 2 deletions docs/zh/nixos-with-flakes/nixos-with-flakes-enabled.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,15 @@ 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,从而使用它们提供的功能。
默认情况下,一个 flake 会在其每个依赖项(即 `inputs` 中的每一项)的根目录下寻找 `flake.nix` 文件并**懒惰求值**(lazy evaluation)它的 `outputs` 函数,并将该函数返回的 attribute set 作为参数传递给这个 flake 自身的 `outputs` 函数,这样我们就能在当前 flake 中使用它所依赖的其他 flakes 提供的功能了。

在我们这一节的例子中,[nixpkgs/flake.nix] 会在我们执行 `sudo nixos-rebuild swtich` 时被执行,我们能从其源码中看到它 outputs 的定义中有 `lib` 这个属性,我们的例子中就使用了 `lib` 属性中的 `nixosSystem` 这个函数:
更精确地说,对每个依赖项的 `outputs` 函数的求值都是懒惰(lazy)的,也就是说,一个 flake 的 `outputs` 函数只有在被真正使用到的时候才会被求值,这样就能避免不必要的计算,从而提高效率。

在我们这一节的例子中,
我们的 `flake.nix` 声明了 `inputs.nixpkgs` 这个依赖项(input),
[nixpkgs/flake.nix] 会在我们执行 `sudo nixos-rebuild swtich` 这个命令时被求值。
我们能从 Nixpkgs 仓库的源码中看到它 flake outputs 的定义中有 `lib` 这个属性,我们的例子中就使用了 `lib` 属性中的 `nixosSystem` 这个函数:

```nix{8-13}
{
Expand Down

0 comments on commit 79b45d6

Please sign in to comment.