|
|
(108 intermediate revisions by the same user 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 74: |
Line 74: |
| ====Nix 独立程序==== | | ====Nix 独立程序==== |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | {{Note |[https://github.com/DeterminateSystems/nix-installer Determinate Nix 安装程序] 默认启用 Flakes 功能。}} |
| {{Note | The [https://github.com/DeterminateSystems/nix-installer Determinate Nix Installer] enables flakes by default.}} | |
| </div>
| |
|
| |
|
| 添加如下内容至 <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 88: |
Line 86: |
| {{Warning | 由于 flake 文件的内容会被复制到全局可读的 [[Nix_package_manager#Nix_store|Nix Store]] 目录下,所以请不要在 flake 文件中写入任何未加密的秘密信息。您应该改用 [[Comparison of secret managing schemes|秘密管理方案]]。}} | | {{Warning | 由于 flake 文件的内容会被复制到全局可读的 [[Nix_package_manager#Nix_store|Nix Store]] 目录下,所以请不要在 flake 文件中写入任何未加密的秘密信息。您应该改用 [[Comparison of secret managing schemes|秘密管理方案]]。}} |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | {{Note | 对于 [[Git]] 仓库中的 flakes,只有工作区中的文件才会被复制到 Store 中。 |
| {{Note | For flakes in [[git]] repositories, only files in the working tree will be copied to the store. | |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | 因此,如果您使用 <code>git</code> 管理您的 flake,请确保在首次创建之后使用 <code>git add</code>添加所有项目文件。}} |
| Therefore, if you use <code>git</code> for your flake, ensure to <code>git add</code> any project files after you first create them.}}
| |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | === Nix Flakes 命令 === |
| === The nix flakes command ===
| |
| {{Main|Nix (command)}} | | {{Main|Nix (command)}} |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | {{ic|nix flake}} 的子命令在 {{Nix Manual|name=Nix 手册命令参考页面|anchor=command-ref/new-cli/nix3-flake}} 中被描述。 |
| The {{ic|nix flake}} subcommand is described in {{Nix Manual|name=command reference page of the Nix manual|anchor=command-ref/new-cli/nix3-flake}}.
| |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | 此 flake 生成一个单 Flake 输出 <code>packages</code>。其中,<code>x86_64-linux</code> 是系统特定的属性集。其中包含两个软件包的 [[derivations|Derivations(派生/定义)]]:<code>default</code> 和 <code>hello</code>。您可以使用 {{Nix Manual|name=show 命令|anchor=command-ref/new-cli/nix3-flake-show}} 给出某 flake 的输出,如下所示: |
| This flake produces a single flake output <code>packages</code>. And within that, <code>x86_64-linux</code> is a system-specifc attribute set. And within that, two package [[derivations]] <code>default</code> and <code>hello</code>. You can find outputs with the {{Nix Manual|name=show command|anchor=command-ref/new-cli/nix3-flake-show}} of a flake as shown below:
| |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| |
| <syntaxhighlight lang="console"> | | <syntaxhighlight lang="console"> |
| $ nix flake show | | $ nix flake show |
Line 117: |
Line 104: |
| └───hello: package 'hello-2.12.2' | | └───hello: package 'hello-2.12.2' |
| </syntaxhighlight> | | </syntaxhighlight> |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr"> | | <span id="Development_shells"></span> |
| ==== Development shells ==== | | ==== 开发环境 Shell ==== |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | <code>devShell</code> 是定义在 flake 中由 Nix 提供的[[Development_environment_with_nix-shell#nix develop|开发环境]]。它允许您声明一个可复用的 Shell 环境,其中将包含开发特定项目所需的工具、库和环境变量。这相当于在 flake 中定义一个 <code>nix-shell</code>。 |
| A <code>devShell</code> is a Nix-provided [[Development_environment_with_nix-shell#nix develop|development environment]] defined within a flake. It lets you declare a reproducible shell environment with the tools, libraries, and environment variables you need for the development of a specific project. This is flake equivalent to defining a <code>nix-shell</code>.
| |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| |
| <syntaxhighlight lang="nix"> | | <syntaxhighlight lang="nix"> |
| { | | { |
| description = "Example flake with a devShell"; | | description = "带有 devShell 的示例 flake"; |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr"> | | inputs.nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable"; |
| inputs.nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable"; | |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr"> | | outputs = { self, nixpkgs}: |
| outputs = { self, nixpkgs}: | |
| let | | let |
| system = "x86_64-linux"; | | system = "x86_64-linux"; |
Line 148: |
Line 126: |
| ]; | | ]; |
| shellHook = '' | | shellHook = '' |
| echo "Welcome to the devShell!" | | echo "欢迎进入 devShell!" |
| ''; | | ''; |
| }; | | }; |
Line 154: |
Line 132: |
| } | | } |
| </syntaxhighlight> | | </syntaxhighlight> |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | 进入开发环境 Shell: |
| To enter the development shell environment:
| |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| |
| <syntaxhighlight lang="console"> | | <syntaxhighlight lang="console"> |
| $ nix develop | | $ nix develop |
| </syntaxhighlight> | | </syntaxhighlight> |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | {{note|使用 nix develop 命令进入开发 shell 无需定义 devShell。 |
| {{note|You don’t need to define a devShell to enter a development shell using nix develop. | | 如果未定义 devShell,nix develop 命令会将您带入一个包含 flake 默认构建依赖项(如果有)的环境。}} |
| If no devShell is defined, nix develop will drop you into an environment containing the default build dependencies of the flake (if any).}}
| |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr"> | | <span id="Build_specific_attributes_in_a_flake_repository"></span> |
| ==== Build specific attributes in a flake repository ==== | | ==== 在 flake 仓库中构建特定属性 ==== |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | 运行 <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> 属性,请运行: |
| Running <code>nix build</code> will look in the <code>legacyPackages</code> and <code>packages</code> output attributes for the corresponding [[derivation]] and then your system architecture and build the default output. If you want to specify a build attribute in a flake repository, you can run <code>nix build .#<attr></code>. In the example above, if you wanted to build the <code>packages.x86_64-linux.hello</code> attribute, run:
| |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| |
| <syntaxHighlight lang=console> | | <syntaxHighlight lang=console> |
| $ nix build .#hello | | $ nix build .#hello |
| </syntaxHighlight> | | </syntaxHighlight> |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr"> | | 同样,您可以给 <code>run</code> 命令:<code>nix run .#hello</code> 和 <code>develop</code>命令:<code>nix develop .#hello</code>指定属性。 |
| Likewise, you can specify an attribute with the run command: <code>nix run .#hello</code> and the develop command: <code>nix develop .#hello</code>.
| |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr"> | | <span id="Flake_schema"></span> |
| == Flake schema == | | == Flake 规范 == |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr"> | | <code>flake.nix</code> 文件是一个具有特殊限制的 Nix 文件(稍后会详细介绍)。 |
| The flake.nix file is a Nix file but that has special restrictions (more on that later).
| |
| </div> | |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | 它有 4 个顶级属性: |
| It has 4 top-level attributes:
| |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | * <code>description</code>:描述此 flake 的字符串。 |
| * <code>description</code> is a string describing the flake. | |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | * <code>inputs</code>:一个包含此 flake 所有依赖项的属性集。相关规范见下述内容。 |
| * <code>inputs</code> is an attribute set of all the dependencies of the flake. The schema is described below. | |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | * <code>outputs</code>: 一个接收参数的函数,其参数为所有所需输入的属性集,并输出另一个属性集,其规范如下所述。 |
| * <code>outputs</code> is a function of one argument that takes an attribute set of all the realized inputs, and outputs another attribute set whose schema is described below. | |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | * <code>nixConfig</code>:一个属性集,包含了 [https://nixos.org/manual/nix/stable/command-ref/conf-file.html 赋予 nix.conf 的值]。这可以通过添加特定于 flake 的配置(例如 [[Binary Cache|二进制缓存源]])来扩展用户 nix 操作的正常行为。 |
| * <code>nixConfig</code> is an attribute set of values which reflect the [https://nixos.org/manual/nix/stable/command-ref/conf-file.html values given to nix.conf]. This can extend the normal behavior of a user's nix experience by adding flake-specific configuration, such as a [[Binary Cache|binary cache]]. | |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr"> | | <span id="Input_schema"></span> |
| === Input schema === | | === 输入规范 === |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | [https://nixos.org/manual/nix/stable/command-ref/new-cli/nix3-flake.html#flake-inputs Nix flake inputs 手册]. |
| [https://nixos.org/manual/nix/stable/command-ref/new-cli/nix3-flake.html#flake-inputs The nix flake inputs manual]. | |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | [https://nixos.org/manual/nix/stable/command-ref/new-cli/nix3-flake.html#flake-references Nix flake 引用手册]. |
| [https://nixos.org/manual/nix/stable/command-ref/new-cli/nix3-flake.html#flake-references The nix flake references manual]. | |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr"> | | <code>inputs</code> 属性定义了 flake 的依赖项。例如,为了让系统能够正确构建,nixpkgs 必须被定义为系统 flake 的依赖项。 |
| The inputs attribute defines the dependencies of the flake. For example, nixpkgs has to be defined as a dependency for a system flake in order for the system to build properly.
| |
| </div> | |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | [[Nixpkgs]] 可使用以下代码进行定义: |
| [[Nixpkgs]] can be defined using the following code: | |
| </div>
| |
|
| |
|
| <code>inputs.nixpkgs.url = "github:NixOS/nixpkgs/<branch name>";</code> | | <code>inputs.nixpkgs.url = "github:NixOS/nixpkgs/<branch name>";</code> |
Line 246: |
Line 188: |
|
| |
|
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | 对于任何包含 flake.nix 文件的仓库,其所属网站也必须被定义。Nix 知道 nixpkgs 仓库的位置,因此没有必要声明它在 GitHub 上。 |
| For any repository with its own flake.nix file, the website must also be defined. Nix knows where the nixpkgs repository is, so stating that it's on GitHub is unnecessary.
| |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | 例如,将 [[Hyprland]] 添加为输入看起来像这样: |
| For example, adding [[Hyprland]] as an input would look something like this:
| |
| </div>
| |
|
| |
|
| <code>inputs.hyprland.url = "github:hyprwm/Hyprland";</code> | | <code>inputs.hyprland.url = "github:hyprwm/Hyprland";</code> |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | 如果您想让 Hyprland 的 nixpkgs 依赖跟随 nixpkgs 输入以避免出现多个版本的 nixpkgs,可以使用以下代码来完成: |
| If you want to make Hyprland follow the nixpkgs input to avoid having multiple versions of nixpkgs, this can be done using the following code:
| |
| </div>
| |
|
| |
|
| <code>inputs.hyprland.inputs.nixpkgs.follows = "nixpkgs";</code> | | <code>inputs.hyprland.inputs.nixpkgs.follows = "nixpkgs";</code> |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | 使用大括号 ({}),我们可以缩短这些内容并将其放在一个表中。代码如下所示: |
| Using curly brackets({}), we can shorten all of this and put it in a table. The code will look something like this:
| |
| </div>
| |
|
| |
|
| <syntaxhighlight lang="nix"> | | <syntaxhighlight lang="nix"> |
Line 276: |
Line 210: |
| </syntaxhighlight> | | </syntaxhighlight> |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | 默认情况下,包 <code>src</code> 中的 Git 子模块不会被复制到 Nix Store,这可能会导致构建失败。Git 仓库中的 Flakes 可以声明它们需要启用 Git 子模块。从 Nix 版本 [https://discourse.nixos.org/t/nix-2-27-0-released/62003 2.27] 开始,您可以通过以下方式启用子模块: |
| By default, Git submodules in package <code>src</code>'s won't get copied to the nix store, this may cause the build to fail. Flakes in Git repositories can declare that they need Git submodules to be enabled. Since Nix version [https://discourse.nixos.org/t/nix-2-27-0-released/62003 2.27], you can enable submodules by:
| |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| |
| <syntaxhighlight lang="nix"> | | <syntaxhighlight lang="nix"> |
| inputs.self.submodules = true; | | inputs.self.submodules = true; |
| </syntaxhighlight> | | </syntaxhighlight> |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr"> | | <span id="Output_schema"></span> |
| === Output schema === | | === 输出规范 === |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | Nix 包管理器仓库的 [https://github.com/NixOS/nix/blob/master/src/nix/flake-check.md src/nix/flake-check.md] 中对此进行了描述。 |
| This is described in the nix package manager [https://github.com/NixOS/nix/blob/master/src/nix/flake-check.md src/nix/flake-check.md].
| |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr"> | | 一旦 Inputs 被解析,它们就会与 <code>self</code> 一起传递给函数 <code>outputs</code>,<code>self</code> 是此 flake 在 Store 中的目录。<code>outputs</code> 根据以下规范返回 flake 的输出。 |
| Once the inputs are resolved, they're passed to the function `outputs` along with with `self`, which is the directory of this flake in the store. `outputs` returns the outputs of the flake, according to the following schema.
| |
| </div> | |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | 其中: |
| Where:
| |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | * <code><system></code> 为类似“x86_64-linux”、“aarch64-linux”、“i686-linux”、“x86_64-darwin”的值 |
| * <code><system></code> is something like "x86_64-linux", "aarch64-linux", "i686-linux", "x86_64-darwin" | |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | * <code><name></code> 是一个属性名称,如“hello”。 |
| * <code><name></code> is an attribute name like "hello". | |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | * <code><flake></code> 是一个 flake 名称, 如“nixpkgs”。 |
| * <code><flake></code> is a flake name like "nixpkgs". | |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | * <code><store-path></code> 是 <code>/nix/store..</code> 的路径。 |
| * <code><store-path></code> is a <code>/nix/store..</code> path | |
| </div>
| |
|
| |
|
| <syntaxHighlight lang=nix> | | <syntaxHighlight lang=nix> |
Line 366: |
Line 281: |
| </syntaxHighlight> | | </syntaxHighlight> |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | 您还可以定义其他任意属性,但以上这些是 Nix 已知的输出。 |
| You can also define additional arbitrary attributes, but these are the outputs that Nix knows about.
| |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr"> | | <span id="Core_usage_patterns"></span> |
| == Core usage patterns == | | == 核心使用模式 == |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr"> | | <span id="Making_your_evaluations_pure"></span> |
| === Making your evaluations pure === | | === 使您的推导更纯 === |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | Nix Flakes 在纯粹推导模式下进行,这意味着对于外部环境的访问被限制以确保可复现性。要保持使用 Flakes 时的纯粹性(Purity),请考虑以下方式: |
| Nix flakes are evaluated in a pure evaluation mode, meaning that access to the external environment is restricted to ensure reproducibility. To maintain purity when working with flakes, consider the following: | |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | * {{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}} and {{Nixpkgs Manual|name=fetchzip|anchor=#sec-pkgs-fetchers-fetchzip-inputs}} require a <code>sha256</code> argument to be considered pure. | |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | * <code>builtins.currentSystem</code> 函数是非确定且不纯的,因为它反映了执行推导的主机系统。通常可以通过将系统类型(例如 x86_64-linux)显式传递给需要它的 Derivations 来避免这种情况。 |
| * <code>builtins.currentSystem</code> is non-hermetic and impure as it reflects the host system performing the evauluation. This can usually be avoided by passing the system (i.e., x86_64-linux) explicitly to derivations requiring it. | |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | * <code>builtins.getEnv</code> 函数也是不纯的。请避免从环境变量中读取数据,同样,也不要引用 flake 目录之外的文件。 |
| * <code>builtins.getEnv</code> is also impure. Avoid reading from environment variables and likewise, do not reference files outside of the flake's directory. | |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr"> | | <span id="Defining_a_flake_for_multiple_architectures"></span> |
| === Defining a flake for multiple architectures === | | === 为多架构定义 Flake === |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | Flakes 强制要求您为每种支持的架构指定一个程序。以下示例展示了如何编写一个针对多种架构的 flake。 |
| Flakes force you to specify a program for each supported architecture. An example below shows how to write a flake that targets multiple architectures. | |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| |
| <syntaxhighlight lang="nix"> | | <syntaxhighlight lang="nix"> |
| { | | { |
| description = "A flake targeting multiple architectures"; | | description = "针对多种架构的 flake"; |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr"> | | inputs = { |
| inputs = { | |
| nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable"; | | nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable"; |
| }; | | }; |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr"> | | 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 431: |
Line 326: |
| } | | } |
| </syntaxhighlight> | | </syntaxhighlight> |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | 您还可以使用如 [[Flake Utils|flake-utils]] 或 [[Flake Parts|flake-parts]] 的第三方项目来编写,它们会提供代码来避免此类样板代码。为了避免多次重新定义程序,请参阅 [[Flake Utils#Defining a flake for multiple architectures]] |
| You can also use third-parties projects like [[Flake Utils|flake-utils]] or [[Flake Parts|flake-parts]] that automatically provide code to avoid this boilerplate. To avoid re-defining the program multiple times, refer to [[Flake Utils#Defining a flake for multiple architectures]]
| |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr"> | | <span id="Using_overlays"></span> |
| === Using overlays === | | === 使用 overlays === |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | 要将 [[Overlays]] 与 Flakes 一起使用,请参阅 [[Overlays#In a Nix flake]] 页面。 |
| To use [[Overlays]] with flakes, refer to [[Overlays#In a Nix flake]] page.
| |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr"> | | <span id="Enable_unfree_software"></span> |
| === Enable unfree software === | | === 启用非自由软件 === |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | 为了在 flake 项目中允许使用 [[Unfree software|非自由软件]],您需要在导入 Nixpkgs 时通过设置 <code>config.allowUnfree = true;</code> 来明确允许它。 |
| To allow for [[Unfree software|unfree software]] in a flake project, you need to explicitly allow it by setting <code>config.allowUnree = true;</code> when importing Nixpkgs.
| |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| |
| <syntaxhighlight lang="nix"> | | <syntaxhighlight lang="nix"> |
| { | | { |
Line 465: |
Line 350: |
| }; | | }; |
| } | | } |
| </syntaxhighlight> | | </syntaxhighlight> |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr"> | | <span id="NixOS_configuration_with_flakes"></span> |
| == NixOS configuration with flakes == | | == 启用 Flake 的 NixOS 配置 == |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | 可使用 Flakes 管理 [[NixOS]] 系统配置,以获得可复现、声明式输入和简化更新的优点。 |
| It is possible to manage a [[NixOS]] system configuration using flakes, gaining the benefits of reproducible, declarative inputs and streamlined updates.
| |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | 关于详情与示例,请参阅 [[NixOS system configuration#Defining NixOS as a flake]]。 |
| For details and examples, see [[NixOS system configuration#Defining NixOS as a flake]].
| |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr"> | | <span id="Development_tricks"></span> |
| == Development tricks == | | == 开发技巧 == |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr"> | | <span id="Automatically_switch_nix_shells_with_direnv"></span> |
| === Automatically switch nix shells with direnv === | | === 使用 direnv 自动切换 Nix shell === |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | 在项目目录之间导航时,使用 [[Direnv]] 可以自动激活不同的 Nix shell。Nix 与 Direnv 的额外集成参阅 [https://github.com/nix-community/nix-direnv nix-direnv]。 |
| It is possible to automatically activate different Nix shells when navigating between project directories by using [[Direnv]]. Additional Nix integration with Direnv can be achieved with [https://github.com/nix-community/nix-direnv nix-direnv].
| |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr"> | | <span id="Pushing_Flakes_to_Cachix"></span> |
| === Pushing Flakes to Cachix === | | === 推送 Flakes 至 Cachix === |
| </div>
| |
|
| |
|
| https://docs.cachix.org/pushing#flakes | | https://docs.cachix.org/pushing#flakes |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr"> | | <span id="Flake_support_in_projects_without_flakes"></span> |
| === Flake support in projects without flakes === | | === 非 Flake 项目中的 Flake 支持 === |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | [https://github.com/edolstra/flake-compat flake-compat] 库提供了一个兼容层,允许使用传统 <code>default.nix</code> 和 <code>shell.nix</code> 文件的项目与 Flakes 兼容。更多详情和使用示例,请参阅 [[Flake Compat]] 页面。 |
| The [https://github.com/edolstra/flake-compat flake-compat] library provides a compatibility layer that allows projects using traditional <code>default.nix</code> and <code>shell.nix</code> files to operate with flakes. For more details and usage examples, see the [[Flake Compat]] page.
| |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | 另一个允许在非 flake 项目中使用 Flakes 的项目是 [https://github.com/fricklerhandwerk/flake-inputs flake-inputs]。 |
| Another project that allows consuming flakes from non-flake projects is [https://github.com/fricklerhandwerk/flake-inputs flake-inputs].
| |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr"> | | <span id="Accessing_flakes_from_Nix_expressions"></span> |
| === Accessing flakes from Nix expressions === | | === 从 Nix 表达式访问 Flakes === |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | 如果您想在启用了 Flakes 功能的系统上从常规 Nix 表达式中访问 flake,可以使用类似 <code>(builtins.getFlake "/path/to/directory").packages.x86_64-linux.default</code> 的代码,其中“directory”是包含 <code>flake.nix</code> 的目录。 |
| If you want to access a flake from within a regular Nix expression on a system that has flakes enabled, you can use something like <code>(builtins.getFlake "/path/to/directory").packages.x86_64-linux.default</code>, where 'directory' is the directory that contains your <code>flake.nix</code>.
| |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr"> | | <span id="Efficiently_build_multiple_flake_outputs"></span> |
| === Efficiently build multiple flake outputs === | | === 高效构建多个 Flake 输出 === |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | 要自动推送''所有'' flake 输出,请查看 [https://github.com/srid/devour-flake#usage devour-flake]。 |
| To push ''all'' flake outputs automatically, checkout [https://github.com/srid/devour-flake#usage devour-flake].
| |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr"> | | <span id="Build_a_package_added_in_a_PR"></span> |
| === Build a package added in a PR === | | === 构建一个添加至 PR 中的包 === |
| </div>
| |
|
| |
|
| <syntaxHighlight> | | <syntaxHighlight> |
Line 534: |
Line 396: |
| </syntaxHighlight> | | </syntaxHighlight> |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | 这允许构建尚未添加到 nixpkgs 的包。 |
| this allows building a package that has not yet been added to nixpkgs.
| |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | 请注意,这将下载 nixpkgs 的完整 tarball 压缩档。如果您已有本地克隆,由于增量压缩机制,使用它可能会更快: |
| note that this will download a full source tarball of nixpkgs. if you already have a local clone, using that may be faster due to delta compression:
| |
| </div>
| |
|
| |
|
| <syntaxHighlight> | | <syntaxHighlight> |
Line 546: |
Line 404: |
| </syntaxHighlight> | | </syntaxHighlight> |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | 这允许构建尚未添加到 nixpkgs 的包。 |
| this allows building a package that has not yet been added to nixpkgs.
| |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr"> | | <span id="How_to_add_a_file_locally_in_git_but_not_include_it_in_commits"></span> |
| === How to add a file locally in git but not include it in commits === | | === 如何在 git 中添加一个本地文件但不将其包含在提交中 === |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | 当 [[git]] 文件夹存在时,flake 将仅复制在 git 中添加的文件,以最大限度地提高可复现性(因此,如果您忘记在代码库中添加本地文件,则在尝试编译时会直接出错)。但是,有时出于开发目的您可能需要创建一个备用的 flake 文件,例如包含您首选编辑器的配置,如[https://discourse.nixos.org/t/local-personal-development-tools-with-flakes/22714/8 此处所述],这种情况下当然无需提交此文件,因为它只包含您自己首选的工具。在上述情况下,您可以执行以下操作(例如,创建了一个名为 <code>extra/flake.nix</code> 的文件): |
| When a [[git]] folder exists, flake will only copy files added in git to maximize reproducibility (this way if you forgot to add a local file in your repo, you will directly get an error when you try to compile it). However, for development purpose you may want to create an alternative flake file, for instance containing configuration for your preferred editors as described [https://discourse.nixos.org/t/local-personal-development-tools-with-flakes/22714/8 here]… of course without committing this file since it contains only your own preferred tools. You can do so by doing something like that (say for a file called <code>extra/flake.nix</code>):
| |
| </div>
| |
|
| |
|
| <syntaxHighlight> | | <syntaxHighlight> |
Line 563: |
Line 416: |
| </syntaxHighlight> | | </syntaxHighlight> |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr"> | | <span id="Rapid_iteration_of_a_direct_dependency"></span> |
| === Rapid iteration of a direct dependency === | | === 直接依赖项的快速迭代 === |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | 使用 Nix 作为开发环境的一个常见痛点是,每次更新依赖项时都需要完全重构并重新进入开发 shell。<code>nix develop --redirect <flake> <directory></code> 命令允许您向 shell 提供可变的依赖项,就像它是由 Nix 构建的一样。 |
| One common pain point with using Nix as a development environment is the need to completely rebuild dependencies and re-enter the dev shell every time they are updated. The <code>nix develop --redirect <flake> <directory></code> command allows you to provide a mutable dependency to your shell as if it were built by Nix.
| |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | 考虑这样一个场景:您的可执行程序 <code>consumexe</code> 依赖于一个库 <code>libdep</code>。你希望同时开发这两个项目,并且对 <code>libdep</code> 的修改能够实时反映到 <code>consumexe</code> 中。这种工作流程可以通过以下方式实现: |
| Consider a situation where your executable, <code>consumexe</code>, depends on a library, <code>libdep</code>. You're trying to work on both at the same time, where changes to <code>libdep</code> are reflected in real time for <code>consumexe</code>. This workflow can be achieved like so:
| |
| </div>
| |
|
| |
|
| <syntaxHighlight lang=bash> | | <syntaxHighlight lang=bash> |
Line 583: |
Line 431: |
| </syntaxHighlight> | | </syntaxHighlight> |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | 现在您已经构建了依赖项,<code>consumexe</code> 可以将其作为输入。'''在另一个终端中''': |
| Now that you've built the dependency, <code>consumexe</code> can take it as an input. '''In another terminal''':
| |
| </div>
| |
|
| |
|
| <syntaxHighlight lang=bash> | | <syntaxHighlight lang=bash> |
Line 594: |
Line 440: |
| </syntaxHighlight> | | </syntaxHighlight> |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | 如果 Nix 警告您重定向的 flake 实际上并未用作已推导 flake 的输入,请尝试使用 <code>--inputs-from .</code> 标志。如果一切顺利,您应该能够在依赖项更改时执行 <code>buildPhase && installPhase</code> 操作,并使用新版本依赖重建您的程序,而''无需''退出开发 shell。 |
| If Nix warns you that your redirected flake isn't actually used as an input to the evaluated flake, try using the <code>--inputs-from .</code> flag. If all worked well you should be able to <code>buildPhase && installPhase</code> when the dependency changes and rebuild your consumer with the new version ''without'' exiting the development shell.
| |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr"> | | <span id="See_also"></span> |
| == See also == | | === 另见 === |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr"> | | <span id="Official_sources"></span> |
| === Official sources === | | === 官方来源 === |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| |
| * [https://nix.dev/concepts/flakes Flakes] - nix.dev | | * [https://nix.dev/concepts/flakes Flakes] - nix.dev |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | * [https://nixos.org/manual/nix/unstable/command-ref/new-cli/nix3-flake.html Nix flake 命令参考手册] - 关于 Flakes 及其各部分的更多附加细节。 |
| * [https://nixos.org/manual/nix/unstable/command-ref/new-cli/nix3-flake.html Nix flake command reference manual] - Many additional details about flakes, and their parts. | |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | * [https://github.com/NixOS/nix/blob/master/src/nix/flake.md 更详细地描述 Flakes Inputs 的规范] |
| * [https://github.com/NixOS/nix/blob/master/src/nix/flake.md spec describing flake inputs in more detail] | |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | * [https://github.com/NixOS/rfcs/pull/49 RFC 49] (2019) - 原始 Flakes 规范 |
| * [https://github.com/NixOS/rfcs/pull/49 RFC 49] (2019) - Original flakes specification | |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr"> | | <span id="Guides"></span> |
| === Guides === | | === 指南 === |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | * [https://jade.fyi/blog/flakes-arent-real/ Flakes 幻象,亦非洪水猛兽] (Jade Lovelace, 2024) |
| * [https://jade.fyi/blog/flakes-arent-real/ Flakes aren't real and can't hurt you] (Jade Lovelace, 2024) | |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | * [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) - 🛠️ ❤️ An unofficial NixOS & Flakes book for beginners. | |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | * [https://xeiaso.net/blog/nix-flakes-1-2022-02-21 Nix Flakes:一个简要介绍] (Xe Iaso, 2022) |
| * [https://xeiaso.net/blog/nix-flakes-1-2022-02-21 Nix Flakes: an Introduction] (Xe Iaso, 2022) | |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | * [https://serokell.io/blog/practical-nix-flakes Practical Nix Flakes] (Alexander Bantyev, 2021) - 关于使用 Nix 和 Flakes 的介绍文章。 |
| * [https://serokell.io/blog/practical-nix-flakes Practical Nix Flakes] (Alexander Bantyev, 2021) - Intro article on working with Nix and Flakes | |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | * [https://www.tweag.io/blog/2020-05-25-flakes/ Nix Flakes, 第一节:介绍和教程] (Eelco Dolstra, 2020) |
| * [https://www.tweag.io/blog/2020-05-25-flakes/ Nix Flakes, Part 1: An introduction and tutorial] (Eelco Dolstra, 2020) | |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | * [https://www.tweag.io/blog/2020-06-25-eval-cache/ Nix Flakes, 第二节:推导缓存] (Eelco Dolstra, 2020) |
| * [https://www.tweag.io/blog/2020-06-25-eval-cache/ Nix Flakes, Part 2: Evaluation caching] (Eelco Dolstra, 2020) | |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | * [https://www.tweag.io/blog/2020-07-31-nixos-flakes/ Nix Flakes, 第三节:管理 NixOS 系统] (Eelco Dolstra, 2020) |
| * [https://www.tweag.io/blog/2020-07-31-nixos-flakes/ Nix Flakes, Part 3: Managing NixOS systems] (Eelco Dolstra, 2020) | |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | * [https://www.youtube.com/watch?v=QXUlhnhuRX4&list=PLgknCdxP89RcGPTjngfNR9WmBgvD_xW0l Nix flakes 101: Introduction to nix flakes] (Jörg Thalheim, 2020) YouTube 视频 |
| * [https://www.youtube.com/watch?v=QXUlhnhuRX4&list=PLgknCdxP89RcGPTjngfNR9WmBgvD_xW0l Nix flakes 101: Introduction to nix flakes] (Jörg Thalheim, 2020) YouTube video | |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr"> | | <span id="Useful_flake_modules"></span> |
| === Useful flake modules === | | === Flake 实用模块 === |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | * [[Flake Utils|flake-utils]]:一个用于简化 Flakes 编写、避免样板代码的库 |
| * [[Flake Utils|flake-utils]]: Library to avoid some boiler-code when writing flakes | |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | * [[Flake Parts|flake-parts]]:帮助编写模块化、结构化 Flakes 的库 |
| * [[Flake Parts|flake-parts]]: Library to help write modular and organized flakes | |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | * [[Flake Compat|flake-compat]]:Flakes 兼容层 |
| * [[Flake Compat|flake-compat]]: A compatibility layer for flakes | |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| | * [https://github.com/nix-community/todomvc-nix 构建 Rust 和 Haskell 的 flakes] |
| * [https://github.com/nix-community/todomvc-nix building Rust and Haskell flakes] | |
| </div>
| |
|
| |
|
| <div lang="en" dir="ltr" class="mw-content-ltr">
| |
| {{references}} | | {{references}} |
| </div>
| |
|
| |
|
| [[Category:Software]] | | [[Category:Software]] |