Flakes/zh: Difference between revisions

Ardenet (talk | contribs)
No edit summary
Ardenet (talk | contribs)
No edit summary
 
(12 intermediate revisions by 2 users not shown)
Line 3: Line 3:
{{Cleanup}}
{{Cleanup}}


'''Nix flakes''' 是 [[Nix]] 2.4 版本中首次引入的一项[[Experimental Nix features|实验性功能]]{{Cite manual|nix|development/experimental-features|number=13.8|title=Experimental Features|subsection=xp-feature-flakes|subtitle=flakes}}{{Cite manual|nix|release-notes/rl-2.4|number=14.27|title=Release 2.4 (2021-11-01)}},旨在解决 Nix 生态系统许多领域的改进问题:它们为 Nix 项目提供了一个统一结构、允许固定每个依赖项的特定版本并通过锁文件共享这些依赖项,同时总体上使编写可复现的 Nix 表达式变得更加方便。
'''Nix flakes''' 是 [[Nix]] 2.4 版本中首次引入的一项[https://nix.dev/manual/nix/stable/contributing/experimental-features 实验性功能]{{Cite manual|nix|development/experimental-features|number=13.8|title=Experimental Features|subsection=xp-feature-flakes|subtitle=flakes}}{{Cite manual|nix|release-notes/rl-2.4|number=14.27|title=Release 2.4 (2021-11-01)}},旨在解决 Nix 生态系统许多领域的改进问题:它们为 Nix 项目提供了一个统一结构、允许固定每个依赖项的特定版本并通过锁文件共享这些依赖项,同时总体上使编写可复现的 Nix 表达式变得更加方便。


Flake 是一个直接包含 <code>flake.nix</code> 文件的目录,该文件内容遵循一种特定结构。Flakes 引入了一种类似 URL 的语法{{Cite manual|nix|command-ref/new-cli/nix3-flake|number=8.5.17|title=nix flake|subsection=url-like-syntax|subtitle=URL-like syntax}} 来指定远程资源。为了简化这种 URL 语法,Flakes 使用符号标识符注册表{{Cite manual|nix|command-ref/new-cli/nix3-registry|number=8.5.62|title=nix registry}},这允许通过类似 <code>github:NixOS/nixpkgs</code> 的语法直接指定资源。
Flake 是一个直接包含 <code>flake.nix</code> 文件的目录,该文件内容遵循一种特定结构。Flakes 引入了一种类似 URL 的语法{{Cite manual|nix|command-ref/new-cli/nix3-flake|number=8.5.17|title=nix flake|subsection=url-like-syntax|subtitle=URL-like syntax}} 来指定远程资源。为了简化这种 URL 语法,Flakes 使用符号标识符注册表{{Cite manual|nix|command-ref/new-cli/nix3-registry|number=8.5.62|title=nix registry}},这允许通过类似 <code>github:NixOS/nixpkgs</code> 的语法直接指定资源。
Line 17: Line 17:
   description = "A very basic flake";
   description = "A very basic flake";


inputs = {
  inputs = {
     nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
     nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
   };
   };


outputs = { self, nixpkgs }: {
  outputs = { self, nixpkgs }: {
 
    packages.x86_64-linux = {
packages.x86_64-linux.hello = nixpkgs.legacyPackages.x86_64-linux.hello;
      default = self.packages.x86_64-linux.hello;
 
      hello = nixpkgs.legacyPackages.x86_64-linux.hello;
packages.x86_64-linux.default = self.packages.x86_64-linux.hello;
    };
 
   };
   };
}</nowiki>|name=flake.nix|lang=nix}}
}</nowiki>|name=flake.nix|lang=nix}}
Line 32: Line 31:
在上述示例中,您可以看到对该 flake 的'''描述'''、指定为某 Github 仓库特定分支的'''输入'''(此为 <code>nixos/nixpkgs</code> 仓库的 <code>nixos-unstable</code> 分支)以及一个使用该输入的'''输出'''。该输出简单地指定了该 flake 包含一个用于 x86_64 架构名为 <code>hello</code> 的包。即使您的 flake 输出不使用其输入(尽管这在实践中极不可能),其输出仍需要是一个 Nix 函数。
在上述示例中,您可以看到对该 flake 的'''描述'''、指定为某 Github 仓库特定分支的'''输入'''(此为 <code>nixos/nixpkgs</code> 仓库的 <code>nixos-unstable</code> 分支)以及一个使用该输入的'''输出'''。该输出简单地指定了该 flake 包含一个用于 x86_64 架构名为 <code>hello</code> 的包。即使您的 flake 输出不使用其输入(尽管这在实践中极不可能),其输出仍需要是一个 Nix 函数。


<div lang="en" dir="ltr" class="mw-content-ltr">
{{Note|Flakes require you to specify its outputs for each architecture separately. For more information, read the related section below.}}
{{Note|Flakes require you to specify its outputs for each architecture separately. For more information, read the related section below.}}
</div>


<span id="Nix_configuration"></span>
<span id="Nix_configuration"></span>
<div class="mw-translate-fuzzy">
=== Nix 配置 ===
=== Nix 配置 ===
为了推导 flake,您可以覆盖 <code>nix.conf</code> 文件中设置的全局 Nix 配置。例如,这可用于设置特定项目的二进制缓存源,同时保持全局配置不变。Flake 文件中可包含一个 nixConfig 属性,并在其中设置相关配置。例如,启用 nix-community 二进制缓存可以通过以下方式实现:
为了推导 flake,您可以覆盖 <code>nix.conf</code> 文件中设置的全局 Nix 配置。例如,这可用于设置特定项目的二进制缓存源,同时保持全局配置不变。Flake 文件中可包含一个 nixConfig 属性,并在其中设置相关配置。例如,启用 nix-community 二进制缓存可以通过以下方式实现:
{{File|3=<nowiki>{
  ...
  nixConfig = {
    extra-substituters = [
      "https://nix-community.cachix.org"
    ];
    extra-trusted-public-keys = [
      "nix-community.cachix.org-1:...="
    ];
  }
}</nowiki>|name=flake.nix|lang=nix}}{{Note|如果您习惯通过 NixOS 配置来设置 Nix 配置,则这些选项位于 <code>nix.settings</code> 下,而不是 <code>nix</code> 下。例如,您无法在 <code>nix.optimization.enable</code> 下指定自动存储优化。}}
</div>
<div lang="en" dir="ltr" class="mw-content-ltr">
It is possible to override the global Nix configuration set in your <code>nix.conf</code> file for the purposes of evaluating a flake. This can be useful, for example, for setting up binary caches specific to certain projects, while keeping the global configuration untouched. The flake file can contain a nixConfig attribute with any relevant configuration settings supplied. For example, enabling the nix-community binary cache would be achieved by:
</div>


{{File|3=<nowiki>{
{{File|3=<nowiki>{
Line 69: Line 50:
}</nowiki>|name=flake.nix|lang=nix}}
}</nowiki>|name=flake.nix|lang=nix}}


<div lang="en" dir="ltr" class="mw-content-ltr">
{{Note|如果您习惯通过 NixOS 配置来设置 Nix 配置,则这些选项位于 <code>nix.settings</code> 下,而不是 <code>nix</code> 下。例如,您无法在 <code>nix.optimization.enable</code> 下指定自动存储优化。}}
{{Note|If you are used to configuring your Nix settings via the NixOS configuration, these options are under <code>nix.settings</code> and not <code>nix</code>. For example, you cannot specify the automatic storage optimisation under <code>nix.optimisation.enable</code>.}}
</div>


<span id="Setup"></span>
<span id="Setup"></span>
Line 107: Line 86:
====Nix 独立程序====
====Nix 独立程序====


{{Note |[https://github.com/DeterminateSystems/nix-installer Determinate Nix 安装程序] 默认启用 Flakes 功能。}}
{{Note |[https://github.com/DeterminateSystems/nix-installer Determinate Nix 安装程序] 默认启用 Flakes 功能,但安装的是专有的 Determinate Nix。}}


添加如下内容至 <code>~/.config/nix/nix.conf</code> 或 <code>/etc/nix/nix.conf</code>:
添加如下内容至 <code>~/.config/nix/nix.conf</code> 或 <code>/etc/nix/nix.conf</code>:
Line 125: Line 104:


<span id="The_nix_flakes_command"></span>
<span id="The_nix_flakes_command"></span>
<div class="mw-translate-fuzzy">
=== Nix Flakes 命令 ===
=== Nix Flakes 命令 ===
{{Main|Nix (command)}}
</div>


{{Main|Nix (command)}}
{{Main|Nix (command)}}
Line 153: Line 129:
   description = "Example flake with a devShell";
   description = "Example flake with a devShell";


inputs.nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
  inputs.nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";


outputs = { self, nixpkgs}:
  outputs = { self, nixpkgs }:
     let
     let
       system = "x86_64-linux";
       system = "x86_64-linux";
Line 184: Line 160:
==== 在 flake 仓库中构建特定属性 ====
==== 在 flake 仓库中构建特定属性 ====


运行 <code>nix build</code> 将在 <code>legacyPackages</code> 和 <code>packages</code> 输出属性中查找相应的 [[derivation|derivation]],然后基于您的系统架构构建默认输出项。如果您想在 flake 仓库中指定构建属性,可以运行 <code>nix build .#<attr></code>。在上面的示例中,如果您想构建 <code>packages.x86_64-linux.hello</code> 属性,请运行:
运行 <code>nix build</code> 将在 <code>legacyPackages</code> 和 <code>packages</code> 输出属性中查找相应的 [[derivations|derivation]],然后基于您的系统架构构建默认输出项。如果您想在 flake 仓库中指定构建属性,可以运行 <code>nix build .#<attr></code>。在上面的示例中,如果您想构建 <code>packages.x86_64-linux.hello</code> 属性,请运行:


<syntaxHighlight lang=console>
<syntaxHighlight lang=console>
Line 220: Line 196:
<code>inputs.nixpkgs.url = "github:NixOS/nixpkgs/<branch name>";</code>
<code>inputs.nixpkgs.url = "github:NixOS/nixpkgs/<branch name>";</code>


<div lang="en" dir="ltr" class="mw-content-ltr">
Nixpkgs 也可以指向一个由 NixOS 组织缓存的 URL:
Nixpkgs can alternatively also point to an url cached by the NixOS organization:
</div>


<code>inputs.nixpkgs.url = "<nowiki>https://nixos.org/channels/nixpkgs-unstable/nixexprs.tar.xz</nowiki>";</code>
<code>inputs.nixpkgs.url = "<nowiki>https://nixos.org/channels/nixpkgs-unstable/nixexprs.tar.xz</nowiki>";</code>


<div lang="en" dir="ltr" class="mw-content-ltr">
在此示例中,输入将指向 <code>nixpkgs-unstable</code> 频道(channel)。
In this example the input would point to the `nixpkgs-unstable` channel.
</div>


对于任何包含 flake.nix 文件的仓库,其所属网站也必须被定义。Nix 知道 nixpkgs 仓库的位置,因此没有必要声明它在 GitHub 上。
对于任何包含 flake.nix 文件的仓库,其所属网站也必须被定义。Nix 知道 nixpkgs 仓库的位置,因此没有必要声明它在 GitHub 上。
Line 261: Line 233:
=== 输出规范 ===
=== 输出规范 ===


Nix 包管理器仓库的 [https://github.com/NixOS/nix/blob/master/src/nix/flake-check.md src/nix/flake-check.md] 中对进行了描述。
[https://nix.dev/manual/nix/2.33/command-ref/new-cli/nix3-flake-check.html#evaluation-checks Nix flake check 手册页]中对输出规范进行了描述。


一旦 Inputs 被解析,它们就会与 <code>self</code> 一起传递给函数 <code>outputs</code>,<code>self</code> 是此 flake 在 Store 中的目录。<code>outputs</code> 根据以下规范返回 flake 的输出。
一旦 Inputs 被解析,它们就会与 <code>self</code> 一起传递给函数 <code>outputs</code>,<code>self</code> 是此 flake 在 Store 中的目录。<code>outputs</code> 根据以下规范返回 flake 的输出。
Line 336: Line 308:
* {{Nixpkgs Manual|name=fetchurl|anchor=#sec-pkgs-fetchers-fetchurl-inputs}} 和 {{Nixpkgs Manual|name=fetchzip|anchor=#sec-pkgs-fetchers-fetchzip-inputs}} 需要传入 <code>sha256</code> 参数才会被视为纯函数。
* {{Nixpkgs Manual|name=fetchurl|anchor=#sec-pkgs-fetchers-fetchurl-inputs}} 和 {{Nixpkgs Manual|name=fetchzip|anchor=#sec-pkgs-fetchers-fetchzip-inputs}} 需要传入 <code>sha256</code> 参数才会被视为纯函数。


* <code>builtins.currentSystem</code> 函数是非确定且不纯的,因为它反映了执行推导的主机系统。通常可以通过将系统类型(例如 x86_64-linux)显式传递给需要它的 Derivations 来避免这种情况
* <code>builtins.currentSystem</code> 函数是非确定且不纯的,因为它返回的是执行推导的主机系统。通常可以通过将系统类型(例如 x86_64-linux)显式传递给需要它的 Derivations 来避免使用


*  <code>builtins.getEnv</code> 函数也是不纯的。请避免从环境变量中读取数据,同样,也不要引用 flake 目录之外的文件。
*  <code>builtins.getEnv</code> 函数也是不纯的。请避免从环境变量中读取数据,同样,也不要引用 flake 目录之外的文件。
Line 532: Line 504:
{{references}}
{{references}}


<div class="mw-translate-fuzzy">
[[Category:Software|软件]]
{{references}}
[[Category:Nix|Nix]]
</div>
[[Category:Nix Language|Nix 语言]]
[[Category:Flakes|Flakes]]