Flakes/zh: Difference between revisions

Weijia (talk | contribs)
Created page with "=== 直接依赖项的快速迭代 ==="
Weijia (talk | contribs)
No edit summary
 
(29 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 221: Line 219:
=== 输出规范 ===
=== 输出规范 ===


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


一旦 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 379: Line 375:
=== 非 Flake 项目中的 Flake 支持 ===
=== 非 Flake 项目中的 Flake 支持 ===


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


<span id="Accessing_flakes_from_Nix_expressions"></span>
<span id="Accessing_flakes_from_Nix_expressions"></span>
Line 427: Line 419:
=== 直接依赖项的快速迭代 ===
=== 直接依赖项的快速迭代 ===


<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 443: 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 454: 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]]