Flakes/zh: Difference between revisions

Weijia (talk | contribs)
Created page with "另一个允许在非 flake 项目中使用 Flakes 的项目是 [https://github.com/fricklerhandwerk/flake-inputs flake-inputs]。"
Tags: Mobile edit Mobile web edit
FuzzyBot (talk | contribs)
Updating to match new version of source page
 
(15 intermediate revisions by 3 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 版本中首次引入的一项[[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 表达式变得更加方便。


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 9: Line 9:
Flakes 还允许锁定引用和版本,然后通过 inputs {{cite manual|nix|command-ref/new-cli/nix3-flake-lock|number=7.5.19|title=nix flake lock}}{{cite manual|nix|command-ref/new-cli/nix3-flake-info|number=7.5.17|title=nix flake info}} 以可编程方式进行查询和更新。此外,一个实验性的 CLI 实用程序接受 flake 引用作为参数,该引用指向用于构建、运行和部署软件包的表达式。{{Cite manual|nix|command-ref/new-cli/nix|number=8.5.1|title=nix}}
Flakes 还允许锁定引用和版本,然后通过 inputs {{cite manual|nix|command-ref/new-cli/nix3-flake-lock|number=7.5.19|title=nix flake lock}}{{cite manual|nix|command-ref/new-cli/nix3-flake-info|number=7.5.17|title=nix flake info}} 以可编程方式进行查询和更新。此外,一个实验性的 CLI 实用程序接受 flake 引用作为参数,该引用指向用于构建、运行和部署软件包的表达式。{{Cite manual|nix|command-ref/new-cli/nix|number=8.5.1|title=nix}}


<span id="Flake_file_structure"></span>
== Flake 文件结构 ==
== Flake 文件结构 ==
一个最小化的 flake 文件包含该 flake 的描述(description),一组输入依赖项(inputs)和一个输出(outputs)。您可以随时使用 <code>nix flake init</code> 命令来生成一个非常基础的 flake 文件。这将在当前目录下创建一个名为 <code>flake.nix</code> 的文件,其内容类似于:
一个最小化的 flake 文件包含该 flake 的描述(description),一组输入依赖项(inputs)和一个输出(outputs)。您可以随时使用 <code>nix flake init</code> 命令来生成一个非常基础的 flake 文件。这将在当前目录下创建一个名为 <code>flake.nix</code> 的文件,其内容类似于:
{{File|3=<nowiki>{
{{File|3=<nowiki>{
   description = "一个非常基础的 flake";
   description = "A very basic flake";


   inputs = {
   inputs = {
Line 19: Line 22:


   outputs = { self, nixpkgs }: {
   outputs = { self, nixpkgs }: {
    packages.x86_64-linux = {
      default = self.packages.x86_64-linux.hello;
      hello = nixpkgs.legacyPackages.x86_64-linux.hello;
    };
  };
}</nowiki>|name=flake.nix|lang=nix}}


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


    packages.x86_64-linux.default = self.packages.x86_64-linux.hello;
{{Note|Flakes require you to specify its outputs for each architecture separately. For more information, read the related section below.}}


  };
<span id="Nix_configuration"></span>
}</nowiki>|name=flake.nix|lang=nix}}
=== Nix 配置 ===
在上述示例中,您可以看到对该 flake 的'''描述'''、指定为某 Github 仓库特定分支的'''输入'''(此为 <code>nixos/nixpkgs</code> 仓库的 <code>nixos-unstable</code> 分支)以及一个使用该输入的'''输出'''。该输出简单地指定了该 flake 包含一个用于 x86_64 架构名为 <code>hello</code> 的包。即使您的 flake 输出不使用其输入(尽管这在实践中极不可能),其输出仍需要是一个 Nix 函数。{{Note|Flakes require you to specify its outputs for each architecture separately. For more information, read the related section below.}}


=== Nix 配置 ===
为了推导 flake,您可以覆盖 <code>nix.conf</code> 文件中设置的全局 Nix 配置。例如,这可用于设置特定项目的二进制缓存源,同时保持全局配置不变。Flake 文件中可包含一个 nixConfig 属性,并在其中设置相关配置。例如,启用 nix-community 二进制缓存可以通过以下方式实现:
为了推导 flake,您可以覆盖 <code>nix.conf</code> 文件中设置的全局 Nix 配置。例如,这可用于设置特定项目的二进制缓存源,同时保持全局配置不变。Flake 文件中可包含一个 nixConfig 属性,并在其中设置相关配置。例如,启用 nix-community 二进制缓存可以通过以下方式实现:
{{File|3=<nowiki>{
{{File|3=<nowiki>{
   ...
   ...
Line 40: Line 48:
     ];
     ];
   }
   }
}</nowiki>|name=flake.nix|lang=nix}}{{Note|如果您习惯通过 NixOS 配置来设置 Nix 配置,则这些选项位于 <code>nix.settings</code> 下,而不是 <code>nix</code> 下。例如,您无法在 <code>nix.optimization.enable</code> 下指定自动存储优化。}}
}</nowiki>|name=flake.nix|lang=nix}}
 
{{Note|如果您习惯通过 NixOS 配置来设置 Nix 配置,则这些选项位于 <code>nix.settings</code> 下,而不是 <code>nix</code> 下。例如,您无法在 <code>nix.optimization.enable</code> 下指定自动存储优化。}}


<span id="Setup"></span>
<span id="Setup"></span>
Line 49: Line 59:


当使用任意 [[Nix command|<code>nix</code> 命令]]时,添加如下命令行参数:
当使用任意 [[Nix command|<code>nix</code> 命令]]时,添加如下命令行参数:
<syntaxhighlight lang="shell">
<syntaxhighlight lang="shell">
  --experimental-features 'nix-command flakes'
  --experimental-features 'nix-command flakes'
</syntaxhighlight>
</syntaxhighlight>
<span id="Enabling_flakes_permanently"></span>
<span id="Enabling_flakes_permanently"></span>
=== 永久启用 Flakes ===
=== 永久启用 Flakes ===
Line 81: Line 93:
experimental-features = nix-command flakes
experimental-features = nix-command flakes
</syntaxHighlight>
</syntaxHighlight>
<span id="Usage"></span>
<span id="Usage"></span>
== 用法 ==
== 用法 ==
Line 90: Line 103:
因此,如果您使用 <code>git</code> 管理您的 flake,请确保在首次创建之后使用 <code>git add</code>添加所有项目文件。}}
因此,如果您使用 <code>git</code> 管理您的 flake,请确保在首次创建之后使用 <code>git add</code>添加所有项目文件。}}


<span id="The_nix_flakes_command"></span>
=== Nix Flakes 命令 ===
=== Nix Flakes 命令 ===
{{Main|Nix (command)}}
{{Main|Nix (command)}}


Line 112: Line 127:
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
{
{
   description = "带有 devShell 的示例 flake";
   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 126: Line 141:
         ];
         ];
         shellHook = ''
         shellHook = ''
           echo "欢迎进入 devShell!"
           echo "Welcome to the devShell!"
         '';
         '';
       };
       };
Line 181: Line 196:
<code>inputs.nixpkgs.url = "github:NixOS/nixpkgs/<branch name>";</code>
<code>inputs.nixpkgs.url = "github:NixOS/nixpkgs/<branch name>";</code>


Nixpkgs can alternatively also point to an url cached by the NixOS organization:
Nixpkgs 也可以指向一个由 NixOS 组织缓存的 URL:


<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>


In this example the input would point to the `nixpkgs-unstable` channel.
在此示例中,输入将指向 <code>nixpkgs-unstable</code> 频道(channel)。
 


对于任何包含 flake.nix 文件的仓库,其所属网站也必须被定义。Nix 知道 nixpkgs 仓库的位置,因此没有必要声明它在 GitHub 上。
对于任何包含 flake.nix 文件的仓库,其所属网站也必须被定义。Nix 知道 nixpkgs 仓库的位置,因此没有必要声明它在 GitHub 上。
Line 198: Line 212:
<code>inputs.hyprland.inputs.nixpkgs.follows = "nixpkgs";</code>
<code>inputs.hyprland.inputs.nixpkgs.follows = "nixpkgs";</code>


使用大括号 ({}),我们可以缩短这些内容并将其放在一个表中。代码如下所示:
使用大括号 (<code>{}</code>),我们可以缩短这些内容并将其放在一个表中。代码如下所示:


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
Line 233: Line 247:
* <code><store-path></code> 是 <code>/nix/store..</code> 的路径。
* <code><store-path></code> 是 <code>/nix/store..</code> 的路径。


<syntaxHighlight lang=nix>
<syntaxhighlight lang="nix">
{ self, ... }@inputs:
{ self, ... }@inputs:
{
{
Line 246: Line 260:
     type = "app";
     type = "app";
     program = "<store-path>";
     program = "<store-path>";
    meta = {description = "..."; inherit otherMetaAttrs; };
   };
   };
   # Executed by `nix run . -- <args?>`
   # Executed by `nix run . -- <args?>`
   apps."<system>".default = { type = "app"; program = "..."; };
   apps."<system>".default = { type = "app"; program = "..."; meta = {description = "..."; inherit otherMetaAttrs; }; };


   # Formatter (alejandra, nixfmt or nixpkgs-fmt)
   # Formatter (alejandra, nixfmt, treefmt-nix or nixpkgs-fmt)
   formatter."<system>" = derivation;
   formatter."<system>" = derivation;
   # Used for nixpkgs packages, also accessible via `nix build .#<name>`
   # Used for nixpkgs packages, also accessible via `nix build .#<name>`
Line 279: Line 294:
   templates.default = { path = "<store-path>"; description = ""; };
   templates.default = { path = "<store-path>"; description = ""; };
}
}
</syntaxHighlight>
</syntaxhighlight>


您还可以定义其他任意属性,但以上这些是 Nix 已知的输出。
您还可以定义其他任意属性,但以上这些是 Nix 已知的输出。
Line 304: Line 319:
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
{
{
   description = "针对多种架构的 flake";
   description = "A flake targeting multiple architectures";


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


  outputs = { self, nixpkgs }: let
outputs = { self, nixpkgs }: let
     systems = [ "x86_64-linux" "aarch64-linux" ];
     systems = [ "x86_64-linux" "aarch64-linux" ];
     forAllSystems = f: builtins.listToAttrs (map (system: {
     forAllSystems = f: builtins.listToAttrs (map (system: {
Line 439: Line 454:
# Output should show ~/libdep-src-checkout/ so you know it worked
# Output should show ~/libdep-src-checkout/ so you know it worked
</syntaxHighlight>
</syntaxHighlight>


如果 Nix 警告您重定向的 flake 实际上并未用作已推导 flake 的输入,请尝试使用 <code>--inputs-from .</code> 标志。如果一切顺利,您应该能够在依赖项更改时执行 <code>buildPhase && installPhase</code> 操作,并使用新版本依赖重建您的程序,而''无需''退出开发 shell。
如果 Nix 警告您重定向的 flake 实际上并未用作已推导 flake 的输入,请尝试使用 <code>--inputs-from .</code> 标志。如果一切顺利,您应该能够在依赖项更改时执行 <code>buildPhase && installPhase</code> 操作,并使用新版本依赖重建您的程序,而''无需''退出开发 shell。
Line 459: Line 475:
=== 指南 ===
=== 指南 ===


* [https://jade.fyi/blog/flakes-arent-real/ Flakes 幻象,亦非洪水猛兽] (Jade Lovelace, 2024)
* [https://jade.fyi/blog/flakes-arent-real/ Flakes 并非幻象,亦非洪水猛兽] (Jade Lovelace, 2024)


* [https://github.com/ryan4yin/nixos-and-flakes-book NixOS & Flakes Book](Ryan4yin, 2023) - 🛠️ ❤️ 一本非官方的 NixOS & Flakes 新手入门书籍。
* [https://github.com/ryan4yin/nixos-and-flakes-book NixOS & Flakes Book](Ryan4yin, 2023) - 🛠️ ❤️ 一本非官方的 NixOS & Flakes 新手入门书籍。
Line 488: Line 504:
{{references}}
{{references}}


[[Category:Software]]
[[Category:Software|软件]]
[[Category:Nix]]
[[Category:Nix|Nix]]
[[Category:Nix Language]]
[[Category:Nix Language|Nix 语言]]
[[Category:Flakes]]
[[Category:Flakes|Flakes]]