beer.drv.drv.drv
本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。
Have you heard about dynamic derivations? They are an experimental-feature in Nix that lets a derivation produce other derivations as its output. Why ? This lets you dynamically create your build graph, instead of having to know it all at evaluation time. It’s Nix’s answer to try and match the build speed of other build systems like Bazel or buck2.
你听说过动态派生(dynamic derivations)吗?这是 Nix 中的一项实验性功能,允许派生将其输出作为其他派生。为什么要这样做?这使你能够动态创建构建图,而不必在评估时就预知一切。这是 Nix 为了追赶 Bazel 或 buck2 等其他构建系统的构建速度而给出的答案。
There is not much written about dynamic derivations, and the documentation is sparse. I have written a few posts about them, so I sometimes get questions about them. Recently, I got a DM from Evan Porter over LinkedIn, the ultimate bug triaging network, about whether it is possible to query the deriver of a dynamic derivation to get the initial derivation.
关于动态派生的资料并不多,文档也很稀疏。我写过几篇关于它们的文章,所以有时会收到相关提问。最近,我在 LinkedIn(终极 Bug 分类网络)上收到了 Evan Porter 的私信,询问是否可以通过查询动态派生的派生器(deriver)来获取初始派生。
For dynamic derivations let’s say I have a .drv.drv file. This file outputs a derivation. Is it usually possible to query deriver the output derivation to obtain the initial derivation? They had tried it and got this: $ nix-store —query —deriver \ $(nix-store —query —deriver ./result) unknown-deriver That should work. 🤨 Every path a build registers carries a deriver, the .drv that produced it, and a .drv that came out of a build is still a path a build registered. When I tried it, it worked. 🤷
对于动态派生,假设我有一个 .drv.drv 文件。该文件输出一个派生。通常是否可以通过查询输出派生的派生器来获得初始派生?他们尝试后得到了这个结果:$ nix-store —query —deriver \ $(nix-store —query —deriver ./result) unknown-deriver。这本该奏效的。🤨 构建注册的每个路径都携带一个派生器(即生成它的 .drv),而从构建中产生的 .drv 仍然是构建注册的路径。当我尝试时,它成功了。🤷
You will need extra-experimental-features = dynamic-derivations ca-derivations recursive-nix in your nix.conf to drive the samples in this post.
你需要将 extra-experimental-features = dynamic-derivations ca-derivations recursive-nix 添加到你的 nix.conf 中,才能运行本文中的示例。
Turns out, there is a case where you do get unknown-deriver if you set __contentAddressed = true on the producer. If the producer is content-addressed, its output path is not known until after the build, and Nix does not fill in the deriver of a path that already exists. I filed NixOS/nix#16823. The question though made me wonder how far back you can walk the chain of dynamic derivations. I had to try it. 😈
事实证明,如果你在生产者上设置了 __contentAddressed = true,确实会出现 unknown-deriver 的情况。如果生产者是内容寻址的,其输出路径直到构建完成后才知道,而 Nix 不会填充已存在路径的派生器。我提交了 NixOS/nix#16823。不过这个问题让我好奇,动态派生链到底能回溯多远。我必须试一试。😈
Let’s pick a fun example to see how far we can go. Here is a derivation that imitates the classic song 99 bottles of beer by producing a chain of derivations, each one outputting the next verse.
让我们选一个有趣的例子来看看能走多远。这里有一个派生,它通过生成一系列派生来模仿经典歌曲《99 瓶啤酒》,每个派生输出下一句歌词。
Tip: A derivation whose output is a .drv must set outputHashMode = “text”. In our example we are using a content-addressed derivation, but the same idea works with an input-addressed one, but it’s more work to compute the output path in the shell, unless you are using nix-instantiate to do it for you with recursive Nix.
提示:输出为 .drv 的派生必须设置 outputHashMode = "text"。在我们的示例中,我们使用的是内容寻址派生,但同样的思路也适用于输入寻址派生,只是在 shell 中计算输出路径会更麻烦,除非你使用 nix-instantiate 配合递归 Nix 来为你完成。
The Nix side builds the first derivation and then follows builtins.outputOf once per bottle. While working through this example, I hit a bug in Nix that made me set __contentAddressed = true on the top derivation. That is not necessary for a derivation that outputs a .drv, but it works around the bug. I filed NixOS/nix#16830.
Nix 端构建第一个派生,然后每瓶啤酒调用一次 builtins.outputOf。在处理这个示例时,我遇到了 Nix 的一个 Bug,迫使我在顶层派生上设置了 __contentAddressed = true。对于输出 .drv 的派生来说,这并非必要,但它绕过了该 Bug。我提交了 NixOS/nix#16830。
You might notice that we limit the number of beers to 50. Why 50 and not 99? Well, 50 is the most that fits in a store path. error: path ’…-beer.drv.drv.drv…’ is not a valid store path: name ‘beer.drv.drv.drv…’ must be no longer than 211 characters. The maximum length of a store path is 211 characters. A derivation’s store path must include the .drv suffix, and every .drv in a chain adds four characters. The top derivation is named beer and if you add 51 .drvs we get 208 characters. No room for any more beer. 🍻
你可能注意到我们将啤酒数量限制为 50。为什么是 50 而不是 99?嗯,50 是存储路径所能容纳的最大数量。错误:路径 ’…-beer.drv.drv.drv…’ 不是有效的存储路径:名称 ‘beer.drv.drv.drv…’ 不能超过 211 个字符。存储路径的最大长度为 211 个字符。派生的存储路径必须包含 .drv 后缀,链中的每个 .drv 都会增加四个字符。顶层派生名为 beer,如果添加 51 个 .drv,我们得到 208 个字符。没有空间容纳更多的啤酒了。🍻