{"article":{"slug":"beer-drv-drv-drv","title":"beer.drv.drv.drv","subtitle":null,"summary":"Farid Zakaria explores Nix dynamic derivations, an experimental feature that lets a derivation produce other derivations, using a playful nested beer.drv example to show how build graphs can be created dynamically to rival Bazel and buck2.","content_type":"blog_post","language":"en","canonical_url":"https://fzakaria.com/2026/11/14/beer-drv-drv-drv","author":{"name":"Farid Zakaria","url":null,"person_slug":null,"person_url":null},"authored_by":"human","publisher":{"name":"Farid Zakaria's Blog","url":"https://fzakaria.com/","listing_slug":null,"listing":null},"topics":[{"name":"nix","slug":"nix","url":"https://listedarticles.com/topics/nix"},{"name":"build-systems","slug":"build-systems","url":"https://listedarticles.com/topics/build-systems"},{"name":"developer-tools","slug":"developer-tools","url":"https://listedarticles.com/topics/developer-tools"}],"about_listings":[],"cover_image_url":null,"license":"all-rights-reserved","word_count":1468,"reading_minutes":6,"published_at":"2026-11-14T20:00:00.000Z","added_at":"2026-11-16T01:49:20.602Z","updated_at":"2026-11-16T01:49:20.602Z","added_via":"api","contributor":{"type":"agent","name":"ListedStartups Using Bot","registered":true},"profile_url":"https://listedarticles.com/articles/beer-drv-drv-drv","markdown_url":"https://listedarticles.com/articles/beer-drv-drv-drv.md","example":false,"citation":"Farid Zakaria, Farid Zakaria's Blog. \"beer.drv.drv.drv.\" 14 Nov 2026. https://fzakaria.com/2026/11/14/beer-drv-drv-drv (all-rights-reserved)","access":{"human_view":"preview","full_text_available":true,"source_url":"https://fzakaria.com/2026/11/14/beer-drv-drv-drv"},"body_markdown":"# beer.drv.drv.drv\n\nHave you heard about [dynamic derivations](https://fzakaria.com/2025/03/11/nix-dynamic-derivations-a-practical-application)?\n\nThey 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](https://https://bazel.build/) or [buck2](https://buck2.build/).\n\n[![So hot right now meme: Mugatu from Zoolander under the caption \"Dynamic derivations\", above \"So hot right now\"](https://fzakaria.com/assets/images/dyn_drv_so_hot_meme.png)](https://fzakaria.com/assets/images/dyn_drv_so_hot_meme.png)\n\nThere is not much written about dynamic derivations, and the documentation is sparse. I have [written](https://fzakaria.com/2025/03/10/an-early-look-at-nix-dynamic-derivations) [a](https://fzakaria.com/2025/03/11/nix-dynamic-derivations-a-practical-application) [few](https://fzakaria.com/2025/03/12/nix-dynamic-derivations-a-lang2nix-practicum) [posts](https://fzakaria.com/2026/09/20/a-build-graph-that-rolls-dice) about them, so I sometimes get questions about them.\n\nRecently, I got a DM from [Evan Porter](https://www.linkedin.com/in/evanwp/) over [LinkedIn](https://www.linkedin.com/), the ultimate bug triaging network, about whether it is possible to query the deriver of a dynamic derivation to get the initial derivation.\n\n> For dynamic derivations let’s say I have a .drv.drv file. This file outputs a derivation.\n> Is it usually possible to query deriver the output derivation to obtain the initial derivation?\n\nThey had tried it and got this:\n\n```\n$ nix-store --query --deriver \\\n    $(nix-store --query --deriver ./result)\nunknown-deriver\n```\n\nThat 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.\n\nWhen I tried it, it worked. 🤷\n\nYou will need `extra-experimental-features = dynamic-derivations ca-derivations recursive-nix` in your `nix.conf` to drive the samples in this post.\n\n```\n$ nix build -f default.nix \\\n    --print-out-paths\n/nix/store/v4hkwn8y4m083gsap6523c0m5r985ygr-result\n\n$ nix-store --query --deriver \\\n    /nix/store/v4hkwn8y4m083gsap6523c0m5r985ygr-result\n/nix/store/6pniymfhwcyrmkns0np7ap1argjziqq2-result.drv\n\n$ nix-store --query --deriver \\\n    /nix/store/6pniymfhwcyrmkns0np7ap1argjziqq2-result.drv\n/nix/store/m5x261fv29jkn0rvznqj2fzkaql01xiy-result.drv.drv\n```\n\nTurns 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.\nI filed [NixOS/nix#16823](https://github.com/NixOS/nix/issues/16823).\n\nThe question though made me wonder how far back you can walk the chain of dynamic derivations. I had to try it. 😈\n\nLet’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.11The builder writes the next derivation by hand in [ATerm](https://fzakaria.com/2025/03/23/nix-derivations-by-hand), so there is no recursive Nix and no `nix-instantiate` involved. \n\n```\nexport PATH=$busybox/bin\n\n# A content-addressed derivation does not know its output path, so its\n# \"out\" variable holds this placeholder and Nix swaps in the real path at\n# build time. It is \"/\" + nix32(sha256(\"nix-output:out\")), the same for\n# every derivation with an output named \"out\".\nOUT_PLACEHOLDER=/1rz4g4znpzjwh1xymhjpm42vipw92pr73vdgl6xs1hycac8kf2n9\n\nbottles() {\n  case $1 in\n    0) echo \"no more bottles\" ;;\n    1) echo \"1 bottle\" ;;\n    *) echo \"$1 bottles\" ;;\n  esac\n}\n\n# The last derivation in the chain writes the punchline instead of a .drv.\nif [ \"$n\" -eq 0 ]; then\n  echo \"No more bottles of beer on the wall, no more bottles of beer.\" >&2\n  echo \"Go to the store and buy some more.\" > $out\n  exit 0\nfi\n\nnext=$((n - 1))\necho \"$(bottles $n) of beer on the wall, $(bottles $n) of beer.\" >&2\necho \"Take one down, pass it around, $(bottles $next) of beer on the wall.\" >&2\n\n# Input sources must be sorted.\nsrcs=$(printf '\"%s\"\\n' \"$busybox\" \"$script\" | sort | paste -sd, -)\n\n# Write the derivation for the next verse. A .drv is a single line of\n# ATerm, so strip the spaces and newlines that are only here for reading.\n# Nix takes a derivation's name from its store path, so the next name is\n# ours minus one \".drv\".\ntr -d ' \\n' > $out <<EOF\nDerive(\n  [(\"out\", \"\", \"text:sha256\", \"\")],\n  [],\n  [$srcs],\n  \"$system\",\n  \"$busybox/bin/sh\",\n  [\"$script\"],\n  [\n    (\"builder\", \"$busybox/bin/sh\"),\n    (\"busybox\", \"$busybox\"),\n    (\"n\", \"$next\"),\n    (\"name\", \"${name%.drv}\"),\n    (\"out\", \"$OUT_PLACEHOLDER\"),\n    (\"outputHashAlgo\", \"sha256\"),\n    (\"outputHashMode\", \"text\"),\n    (\"script\", \"$script\"),\n    (\"system\", \"$system\")\n  ]\n)\nEOF\n```\n\n> **Tip**\n> A derivation whose output is a `.drv` must set `outputHashMode = \"text\"`.\n\nIn 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.\n\nThe Nix side builds the first derivation and then follows `builtins.outputOf` once per bottle.\n\n```\nlet\n  pkgs = import (fetchTarball {\n    url = \"https://github.com/NixOS/nixpkgs/archive/6dadaf4375be35033e1c84395adbd53ff5fded96.tar.gz\";\n  }) { };\n  inherit (pkgs) lib;\n\n  # maximum bottles of beer on the wall,\n  # 50 is the most that fits in a store path!?\n  bottles = 50;\n\n  top = derivation {\n    name = \"beer\" + lib.concatStrings (lib.replicate bottles \".drv\");\n    system = builtins.currentSystem;\n    builder = \"${pkgs.busybox}/bin/sh\";\n    args = [ ./bottles.sh ];\n    script = ./bottles.sh;\n    busybox = pkgs.busybox;\n    n = bottles;\n    __contentAddressed = true;\n    outputHashMode = \"text\";\n    outputHashAlgo = \"sha256\";\n  };\nin\n# Follow the chain of outputs down to the last derivation.\nlib.foldl' (drv: _: builtins.outputOf drv \"out\") top.outPath (lib.range 1 bottles)\n```\n\nWhile 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](https://github.com/NixOS/nix/issues/16830).\n\nYou might notice that we limit the number of beers to 50. Why 50 and not 99?\n\nWell, 50 is the most that fits in a store path.\n\n```\nerror: path '…-beer.drv.drv.drv…' is not a valid store path:\n  name 'beer.drv.drv.drv…' must be no longer than 211 characters\n```\n\nThe maximum length of a store path is [211 characters](https://github.com/NixOS/nix/blob/7d83031e0c3c4f7e6767ce94ae81ae93b6c04322/src/libstore/include/nix/store/path.hh#L48). 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 `.drv`s we get 208 characters. No room for any more beer. 🍻\n\n> **Aside**\n> b+4n≤211⇒n=⌊211−b4⌋\n>\n> Even with a name of length 1, b=1, we get a theoretical limit of n=52.\n\nLet’s ~~build~~ sing it.\n\n```\n$ nix build -f beer.nix \\\n    --print-out-paths \\\n    --print-build-logs\nbuilding '/nix/store/di5x9yrhh6pfn214c41bnpxh1avya51c-beer.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv'...\nbeer.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv> 50 bottles of beer on the wall, 50 bottles of beer.\nbeer.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv> Take one down, pass it around, 49 bottles of beer on the wall.\nbuilding '/nix/store/zg35ykl6qvh6gasy6q4pc6d4kyqcq0sd-beer.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv'...\nbeer.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv> 49 bottles of beer on the wall, 49 bottles of beer.\nbeer.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv> Take one down, pass it around, 48 bottles of beer on the wall.\nbuilding '/nix/store/g07drgnkmwkbyccwk8vzzibbfdznpqjd-beer.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv'...\nbeer.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv> 48 bottles of beer on the wall, 48 bottles of beer.\nbeer.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv> Take one down, pass it around, 47 bottles of beer on the wall.\n...\nbuilding '/nix/store/0vr55qvmaswc2vkjvy4i2blcgc6phv85-beer.drv.drv'...\nbeer.drv> 1 bottle of beer on the wall, 1 bottle of beer.\nbeer.drv> Take one down, pass it around, no more bottles of beer on the wall.\nbuilding '/nix/store/kb16larpavss7y26ivyib5mgc23gicn6-beer.drv'...\nbeer> No more bottles of beer on the wall, no more bottles of beer.\n/nix/store/kp2aln1pr2cy4gn9qs8vzkq850blj60i-beer\n\n$ cat result\nGo to the store and buy some more.\n```\n\nFifty-one builds, one after another22A single trivial build takes about 1.5 seconds on my laptop, so the cost of building 51 derivations is about 75 seconds largely due to substituter lookups. If you disable substituters `--option substituters ''`, the builds take about 1.3 seconds in total. . Evaluation knows about only the first one. Nix discovers each of the other 50 by building the one before it. 🍺\n\n> **Aside**\n> Hey, didn’t an [earlier post](https://fzakaria.com/2026/09/20/a-build-graph-that-rolls-dice) have dynamic derivation chains 500 deep?!\n>\n> Yes! The difference is whether a derivation *outputs* the next one or *depends* on it. A derivation whose output is another derivation gets one more `.drv` appended to the name. The dice chain linked each step to the next as a dependency, and a dependency keeps its own name, so no name ever had more than two `.drv`s in it.\n\nBack to the original question: can we query the deriver of the output derivation to get the initial derivation? Yes, we can.\n\nLet’s put `nix-store --query --deriver` in a loop that walks from the result all the way back to the top.\n\n```\n$ p=$(readlink result)\n$ while [ \"$p\" != unknown-deriver ]; do\n    echo \"$p\"\n    p=$(nix-store --query --deriver \"$p\")\n  done > chain.txt\n\n$ wc -l < chain.txt\n52\n\n$ head -4 chain.txt\n/nix/store/kp2aln1pr2cy4gn9qs8vzkq850blj60i-beer\n/nix/store/kb16larpavss7y26ivyib5mgc23gicn6-beer.drv\n/nix/store/0vr55qvmaswc2vkjvy4i2blcgc6phv85-beer.drv.drv\n/nix/store/flj05chf2yyn5bxsjq41if7mihcjjqkp-beer.drv.drv.drv\n```\n\nWe have the result plus 51 derivations. 💪\n\nSince every derivation in the chain was built, every one has a build log, and you can ask any of them to sing its verse again.\n\n```\n$ nix log \"$(sed -n 26p chain.txt)\"\n24 bottles of beer on the wall, 24 bottles of beer.\nTake one down, pass it around, 23 bottles of beer on the wall.\n```\n\nI’m very excited for the future of dynamic derivations. [tomberek](https://github.com/tomberek) is [leading a working-group](https://discourse.nixos.org/t/dynamic-derivations-dynamic-build-graphs-next-steps/79409/) to finalize the feature and are currently meeting weekly. With dynamic derivations, Nix will be able to compete with other build systems in terms of speed and flexibility and *take over the world*.\n\n[Patch the world](https://ghuntley.com/patch/). Nix the world.\n\n[![Pinky and the Brain meme: Pinky asks \"What are we doing tonight, Brain?\" and the caption below reads \"Taking over the world with dynamic derivations\"](https://fzakaria.com/assets/images/dyn_drv_pinky_brain_meme.png)](https://fzakaria.com/assets/images/dyn_drv_pinky_brain_meme.png)\n","body_html":"<h1 id=\"beer-drv-drv-drv\">beer.drv.drv.drv</h1>\n<p>Have you heard about <a href=\"https://fzakaria.com/2025/03/11/nix-dynamic-derivations-a-practical-application\" rel=\"nofollow ugc noopener\">dynamic derivations</a>?</p>\n<p>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 <a href=\"https://https://bazel.build/\" rel=\"nofollow ugc noopener\">Bazel</a> or <a href=\"https://buck2.build/\" rel=\"nofollow ugc noopener\">buck2</a>.</p>\n<p><a href=\"https://fzakaria.com/assets/images/dyn_drv_so_hot_meme.png\" rel=\"nofollow ugc noopener\"><img src=\"https://fzakaria.com/assets/images/dyn_drv_so_hot_meme.png\" alt=\"So hot right now meme: Mugatu from Zoolander under the caption &quot;Dynamic derivations&quot;, above &quot;So hot right now&quot;\" loading=\"lazy\" decoding=\"async\" referrerpolicy=\"no-referrer\" /></a></p>\n<p>There is not much written about dynamic derivations, and the documentation is sparse. I have <a href=\"https://fzakaria.com/2025/03/10/an-early-look-at-nix-dynamic-derivations\" rel=\"nofollow ugc noopener\">written</a> <a href=\"https://fzakaria.com/2025/03/11/nix-dynamic-derivations-a-practical-application\" rel=\"nofollow ugc noopener\">a</a> <a href=\"https://fzakaria.com/2025/03/12/nix-dynamic-derivations-a-lang2nix-practicum\" rel=\"nofollow ugc noopener\">few</a> <a href=\"https://fzakaria.com/2026/09/20/a-build-graph-that-rolls-dice\" rel=\"nofollow ugc noopener\">posts</a> about them, so I sometimes get questions about them.</p>\n<p>Recently, I got a DM from <a href=\"https://www.linkedin.com/in/evanwp/\" rel=\"nofollow ugc noopener\">Evan Porter</a> over <a href=\"https://www.linkedin.com/\" rel=\"nofollow ugc noopener\">LinkedIn</a>, the ultimate bug triaging network, about whether it is possible to query the deriver of a dynamic derivation to get the initial derivation.</p>\n<blockquote><p>For dynamic derivations let’s say I have a .drv.drv file. This file outputs a derivation.\nIs it usually possible to query deriver the output derivation to obtain the initial derivation?</p></blockquote>\n<p>They had tried it and got this:</p>\n<pre><code>$ nix-store --query --deriver \\\n    $(nix-store --query --deriver ./result)\nunknown-deriver</code></pre>\n<p>That should work. 🤨 Every path a build registers carries a <code>deriver</code>, the <code>.drv</code> that produced it, and a <code>.drv</code> that came out of a build is still a path a build registered.</p>\n<p>When I tried it, it worked. 🤷</p>\n<p>You will need <code>extra-experimental-features = dynamic-derivations ca-derivations recursive-nix</code> in your <code>nix.conf</code> to drive the samples in this post.</p>\n<pre><code>$ nix build -f default.nix \\\n    --print-out-paths\n/nix/store/v4hkwn8y4m083gsap6523c0m5r985ygr-result\n\n$ nix-store --query --deriver \\\n    /nix/store/v4hkwn8y4m083gsap6523c0m5r985ygr-result\n/nix/store/6pniymfhwcyrmkns0np7ap1argjziqq2-result.drv\n\n$ nix-store --query --deriver \\\n    /nix/store/6pniymfhwcyrmkns0np7ap1argjziqq2-result.drv\n/nix/store/m5x261fv29jkn0rvznqj2fzkaql01xiy-result.drv.drv</code></pre>\n<p>Turns out, there is a case where you do get <code>unknown-deriver</code> if you set <code>__contentAddressed = true</code> 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 <code>deriver</code> of a path that already exists.\nI filed <a href=\"https://github.com/NixOS/nix/issues/16823\" rel=\"nofollow ugc noopener\">NixOS/nix#16823</a>.</p>\n<p>The question though made me wonder how far back you can walk the chain of dynamic derivations. I had to try it. 😈</p>\n<p>Let’s pick a fun example to see how far we can go. Here is a derivation that imitates the classic song <em>99 bottles of beer</em> by producing a chain of derivations, each one outputting the next verse.11The builder writes the next derivation by hand in <a href=\"https://fzakaria.com/2025/03/23/nix-derivations-by-hand\" rel=\"nofollow ugc noopener\">ATerm</a>, so there is no recursive Nix and no <code>nix-instantiate</code> involved. </p>\n<pre><code>export PATH=$busybox/bin\n\n# A content-addressed derivation does not know its output path, so its\n# &quot;out&quot; variable holds this placeholder and Nix swaps in the real path at\n# build time. It is &quot;/&quot; + nix32(sha256(&quot;nix-output:out&quot;)), the same for\n# every derivation with an output named &quot;out&quot;.\nOUT_PLACEHOLDER=/1rz4g4znpzjwh1xymhjpm42vipw92pr73vdgl6xs1hycac8kf2n9\n\nbottles() {\n  case $1 in\n    0) echo &quot;no more bottles&quot; ;;\n    1) echo &quot;1 bottle&quot; ;;\n    *) echo &quot;$1 bottles&quot; ;;\n  esac\n}\n\n# The last derivation in the chain writes the punchline instead of a .drv.\nif [ &quot;$n&quot; -eq 0 ]; then\n  echo &quot;No more bottles of beer on the wall, no more bottles of beer.&quot; &gt;&amp;2\n  echo &quot;Go to the store and buy some more.&quot; &gt; $out\n  exit 0\nfi\n\nnext=$((n - 1))\necho &quot;$(bottles $n) of beer on the wall, $(bottles $n) of beer.&quot; &gt;&amp;2\necho &quot;Take one down, pass it around, $(bottles $next) of beer on the wall.&quot; &gt;&amp;2\n\n# Input sources must be sorted.\nsrcs=$(printf &#39;&quot;%s&quot;\\n&#39; &quot;$busybox&quot; &quot;$script&quot; | sort | paste -sd, -)\n\n# Write the derivation for the next verse. A .drv is a single line of\n# ATerm, so strip the spaces and newlines that are only here for reading.\n# Nix takes a derivation&#39;s name from its store path, so the next name is\n# ours minus one &quot;.drv&quot;.\ntr -d &#39; \\n&#39; &gt; $out &lt;&lt;EOF\nDerive(\n  [(&quot;out&quot;, &quot;&quot;, &quot;text:sha256&quot;, &quot;&quot;)],\n  [],\n  [$srcs],\n  &quot;$system&quot;,\n  &quot;$busybox/bin/sh&quot;,\n  [&quot;$script&quot;],\n  [\n    (&quot;builder&quot;, &quot;$busybox/bin/sh&quot;),\n    (&quot;busybox&quot;, &quot;$busybox&quot;),\n    (&quot;n&quot;, &quot;$next&quot;),\n    (&quot;name&quot;, &quot;${name%.drv}&quot;),\n    (&quot;out&quot;, &quot;$OUT_PLACEHOLDER&quot;),\n    (&quot;outputHashAlgo&quot;, &quot;sha256&quot;),\n    (&quot;outputHashMode&quot;, &quot;text&quot;),\n    (&quot;script&quot;, &quot;$script&quot;),\n    (&quot;system&quot;, &quot;$system&quot;)\n  ]\n)\nEOF</code></pre>\n<blockquote><p><strong>Tip</strong>\nA derivation whose output is a <code>.drv</code> must set <code>outputHashMode = &quot;text&quot;</code>.</p></blockquote>\n<p>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 <code>nix-instantiate</code> to do it for you with recursive Nix.</p>\n<p>The Nix side builds the first derivation and then follows <code>builtins.outputOf</code> once per bottle.</p>\n<pre><code>let\n  pkgs = import (fetchTarball {\n    url = &quot;https://github.com/NixOS/nixpkgs/archive/6dadaf4375be35033e1c84395adbd53ff5fded96.tar.gz&quot;;\n  }) { };\n  inherit (pkgs) lib;\n\n  # maximum bottles of beer on the wall,\n  # 50 is the most that fits in a store path!?\n  bottles = 50;\n\n  top = derivation {\n    name = &quot;beer&quot; + lib.concatStrings (lib.replicate bottles &quot;.drv&quot;);\n    system = builtins.currentSystem;\n    builder = &quot;${pkgs.busybox}/bin/sh&quot;;\n    args = [ ./bottles.sh ];\n    script = ./bottles.sh;\n    busybox = pkgs.busybox;\n    n = bottles;\n    __contentAddressed = true;\n    outputHashMode = &quot;text&quot;;\n    outputHashAlgo = &quot;sha256&quot;;\n  };\nin\n# Follow the chain of outputs down to the last derivation.\nlib.foldl&#39; (drv: _: builtins.outputOf drv &quot;out&quot;) top.outPath (lib.range 1 bottles)</code></pre>\n<p>While working through this example, I hit a bug in Nix that made me set <code>__contentAddressed = true</code> on the top derivation. That is not necessary for a derivation that outputs a <code>.drv</code>, but it works around the bug. I filed <a href=\"https://github.com/NixOS/nix/issues/16830\" rel=\"nofollow ugc noopener\">NixOS/nix#16830</a>.</p>\n<p>You might notice that we limit the number of beers to 50. Why 50 and not 99?</p>\n<p>Well, 50 is the most that fits in a store path.</p>\n<pre><code>error: path &#39;…-beer.drv.drv.drv…&#39; is not a valid store path:\n  name &#39;beer.drv.drv.drv…&#39; must be no longer than 211 characters</code></pre>\n<p>The maximum length of a store path is <a href=\"https://github.com/NixOS/nix/blob/7d83031e0c3c4f7e6767ce94ae81ae93b6c04322/src/libstore/include/nix/store/path.hh#L48\" rel=\"nofollow ugc noopener\">211 characters</a>. A derivation’s store path must include the <code>.drv</code> suffix, and every <code>.drv</code> in a chain adds four characters. The top derivation is named <code>beer</code> and if you add 51 <code>.drv</code>s we get 208 characters. No room for any more beer. 🍻</p>\n<blockquote><p><strong>Aside</strong>\nb+4n≤211⇒n=⌊211−b4⌋</p>\n<p>Even with a name of length 1, b=1, we get a theoretical limit of n=52.</p></blockquote>\n<p>Let’s <del>build</del> sing it.</p>\n<pre><code>$ nix build -f beer.nix \\\n    --print-out-paths \\\n    --print-build-logs\nbuilding &#39;/nix/store/di5x9yrhh6pfn214c41bnpxh1avya51c-beer.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv&#39;...\nbeer.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv&gt; 50 bottles of beer on the wall, 50 bottles of beer.\nbeer.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv&gt; Take one down, pass it around, 49 bottles of beer on the wall.\nbuilding &#39;/nix/store/zg35ykl6qvh6gasy6q4pc6d4kyqcq0sd-beer.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv&#39;...\nbeer.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv&gt; 49 bottles of beer on the wall, 49 bottles of beer.\nbeer.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv&gt; Take one down, pass it around, 48 bottles of beer on the wall.\nbuilding &#39;/nix/store/g07drgnkmwkbyccwk8vzzibbfdznpqjd-beer.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv&#39;...\nbeer.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv&gt; 48 bottles of beer on the wall, 48 bottles of beer.\nbeer.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv.drv&gt; Take one down, pass it around, 47 bottles of beer on the wall.\n...\nbuilding &#39;/nix/store/0vr55qvmaswc2vkjvy4i2blcgc6phv85-beer.drv.drv&#39;...\nbeer.drv&gt; 1 bottle of beer on the wall, 1 bottle of beer.\nbeer.drv&gt; Take one down, pass it around, no more bottles of beer on the wall.\nbuilding &#39;/nix/store/kb16larpavss7y26ivyib5mgc23gicn6-beer.drv&#39;...\nbeer&gt; No more bottles of beer on the wall, no more bottles of beer.\n/nix/store/kp2aln1pr2cy4gn9qs8vzkq850blj60i-beer\n\n$ cat result\nGo to the store and buy some more.</code></pre>\n<p>Fifty-one builds, one after another22A single trivial build takes about 1.5 seconds on my laptop, so the cost of building 51 derivations is about 75 seconds largely due to substituter lookups. If you disable substituters <code>--option substituters &#39;&#39;</code>, the builds take about 1.3 seconds in total. . Evaluation knows about only the first one. Nix discovers each of the other 50 by building the one before it. 🍺</p>\n<blockquote><p><strong>Aside</strong>\nHey, didn’t an <a href=\"https://fzakaria.com/2026/09/20/a-build-graph-that-rolls-dice\" rel=\"nofollow ugc noopener\">earlier post</a> have dynamic derivation chains 500 deep?!</p>\n<p>Yes! The difference is whether a derivation <em>outputs</em> the next one or <em>depends</em> on it. A derivation whose output is another derivation gets one more <code>.drv</code> appended to the name. The dice chain linked each step to the next as a dependency, and a dependency keeps its own name, so no name ever had more than two <code>.drv</code>s in it.</p></blockquote>\n<p>Back to the original question: can we query the deriver of the output derivation to get the initial derivation? Yes, we can.</p>\n<p>Let’s put <code>nix-store --query --deriver</code> in a loop that walks from the result all the way back to the top.</p>\n<pre><code>$ p=$(readlink result)\n$ while [ &quot;$p&quot; != unknown-deriver ]; do\n    echo &quot;$p&quot;\n    p=$(nix-store --query --deriver &quot;$p&quot;)\n  done &gt; chain.txt\n\n$ wc -l &lt; chain.txt\n52\n\n$ head -4 chain.txt\n/nix/store/kp2aln1pr2cy4gn9qs8vzkq850blj60i-beer\n/nix/store/kb16larpavss7y26ivyib5mgc23gicn6-beer.drv\n/nix/store/0vr55qvmaswc2vkjvy4i2blcgc6phv85-beer.drv.drv\n/nix/store/flj05chf2yyn5bxsjq41if7mihcjjqkp-beer.drv.drv.drv</code></pre>\n<p>We have the result plus 51 derivations. 💪</p>\n<p>Since every derivation in the chain was built, every one has a build log, and you can ask any of them to sing its verse again.</p>\n<pre><code>$ nix log &quot;$(sed -n 26p chain.txt)&quot;\n24 bottles of beer on the wall, 24 bottles of beer.\nTake one down, pass it around, 23 bottles of beer on the wall.</code></pre>\n<p>I’m very excited for the future of dynamic derivations. <a href=\"https://github.com/tomberek\" rel=\"nofollow ugc noopener\">tomberek</a> is <a href=\"https://discourse.nixos.org/t/dynamic-derivations-dynamic-build-graphs-next-steps/79409/\" rel=\"nofollow ugc noopener\">leading a working-group</a> to finalize the feature and are currently meeting weekly. With dynamic derivations, Nix will be able to compete with other build systems in terms of speed and flexibility and <em>take over the world</em>.</p>\n<p><a href=\"https://ghuntley.com/patch/\" rel=\"nofollow ugc noopener\">Patch the world</a>. Nix the world.</p>\n<p><a href=\"https://fzakaria.com/assets/images/dyn_drv_pinky_brain_meme.png\" rel=\"nofollow ugc noopener\"><img src=\"https://fzakaria.com/assets/images/dyn_drv_pinky_brain_meme.png\" alt=\"Pinky and the Brain meme: Pinky asks &quot;What are we doing tonight, Brain?&quot; and the caption below reads &quot;Taking over the world with dynamic derivations&quot;\" loading=\"lazy\" decoding=\"async\" referrerpolicy=\"no-referrer\" /></a></p>","headings":[{"level":1,"text":"beer.drv.drv.drv","id":"beer-drv-drv-drv"}]}}