Skip to content

libstore: compute the hash modulo via an intermediate derivation - #16191

Merged
Ericson2314 merged 6 commits into
NixOS:masterfrom
obsidiansystems:drv-inputs-shuffle-modulo
Aug 3, 2026
Merged

Ericson2314 merged 6 commits into
NixOS:masterfrom
obsidiansystems:drv-inputs-shuffle-modulo

Conversation

@amaanq

@amaanq amaanq commented Jul 22, 2026 •

Copy link
Copy Markdown
Member

See each commit for details


Add 👍 to pull requests you find important.

The Nix maintainer team uses a GitHub project board to schedule and track reviews.

@github-actions github-actions Bot added new-cli Relating to the "nix" command with-tests Issues related to testing. PRs with tests have some priority store Issues and pull requests concerning the Nix store labels Jul 22, 2026
@Ericson2314
Ericson2314 force-pushed the drv-inputs-shuffle-modulo branch 4 times, most recently from 6a5c3c4 to 517557f Compare July 23, 2026 00:51
@Ericson2314
Ericson2314 force-pushed the drv-inputs-shuffle-modulo branch 2 times, most recently from 60dda3f to 1be35d6 Compare July 30, 2026 20:34
@github-actions github-actions Bot added the c api Nix as a C library with a stable interface label Jul 30, 2026
Comment on lines 163 to 181
@@ -176,7 +176,7 @@ TEST_F(FillInOutputPathsTest, throwsOnIncorrectEnvVar)
// Serialize before state
checkpointJson("bad-env-var", drv);

ASSERT_THROW(drv.fillInOutputPaths(*store), Error);
ASSERT_THROW(fillInOutputPaths(drv, *store), Error);
}
#endif

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason this is compiled out currently?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a case that we made lenient at @Mic92's request to spread out the breakage with a warning instead

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now the test case is up and running

@Ericson2314
Ericson2314 force-pushed the drv-inputs-shuffle-modulo branch 15 times, most recently from 6b19310 to 1101f20 Compare August 1, 2026 01:17
@Ericson2314
Ericson2314 force-pushed the drv-inputs-shuffle-modulo branch from 4de8987 to ce41459 Compare August 3, 2026 20:10
@Ericson2314
Ericson2314 enabled auto-merge August 3, 2026 20:13
Ericson2314 and others added 6 commits August 3, 2026 17:11
Commit 623f3d3 (turn 'derivation has
incorrect deferred output' into warning) added an `isDeferred`
parameter to the env-var validation in
`processDerivationOutputPaths`, with a warn-instead-of-throw branch
for compatibility with derivations produced by older versions of Nix
("this breaks nix develop when using a stable nix version"). But no
call site ever passed `true`, so the leniency was dead code and the
mismatch still threw.

Pass `isDeferred` when filling in a `Deferred` output, which is
exactly the compatibility case that commit was after: an older Nix
wrote the derivation with an env var that does not match the output
path a newer Nix computes.

Relatedly, replace the `#if 0`-ed `throwsOnIncorrectEnvVar` test ---
which asserted the strict behavior that could never be enabled --- with
a live test of the warn behavior: fill-in succeeds, the output is
still converted to `InputAddressed`, and the incorrect env var is
left alone. When the compat period ends this test should flip back to
asserting a throw.

Assisted-By: Claude Fable 5 <[email protected]>
Right now it is just noise, but it will become useful in the next
commit. Likewise add a `mapOutputs` counterpart to `mapInputs`,
also first used in the next commit.

For the members that only make sense for real `Output`s --- `type`,
`outputsAndOptPaths`, and `checkInvariants` --- rather than gating
them with `requires std::is_same_v<Out, Output>` clauses, we continue
in the direction of commit 9031746
(libstore: introduce `nix::derivation` namespace, use ADL free
functions) and pull them out as namespace-level free functions (found
by ADL): taking `const Derivation<Inputs, Output> &` pins the output
type by deduction / overload resolution, so no `requires` clauses are
needed at all.

Co-authored-by: Amaan Qureshi <[email protected]>
Assisted-by: Claude Fable 5
The overall goal here is to tighten up the very complicated hash
derivation modulo logic by making things more type-directed, removing
illegal states and boolean blindness.

To that end, we've done a number of refactors, and now things are
significantly clearer:

- `unparseDerivation` has no masking logic — it delegates to overloaded
  `unparseOutput` functions (one per `DerivationOutput` variant), so the
  rendering is purely determined by the output type.

- `derivationModulo` replaces `maskDerivation` and is similarly
  unopinionated, it *just* converts (by value) a derivation with input
  derivations specified by drv path into one with input derivations
  specified by hash. In other words, it just handles the inductive case
  for the "derivation hash modulo" algorithm, and is really just a
  slight special case of "map inputs".

The magic now all happens in the callers of `derivationModulo`, which
instead of a single function with a constexpr bool is split into two:

- `hashDerivationModulo` (private): This is for actually computing input
  addressed cases, and is only an inductive case (final step actually,
  not the base case, which is the first step).

  Returns `std::optional<Hash>` (nullopt if deferred). It works by
  converting outputs to `Deferred` (they must already be input-addressed
  or deferred), blanking output env vars, then calling
  `derivationModulo`, and finally hashing that derivation modulo. Used by
  `processDerivationOutputPaths` to compute a derivation's own output
  paths. The `Deferred` output type ensures the hash does not depend on
  output paths (avoiding circularity).

- `hashInputDerivationModulo` (public): This is the base case (fixed
  output derivation) or inductive case (hash of input-address with
  already-filled-in output paths).

  Returns `DrvHashModulo`. Handles fixed-CA (returns `CaOutputHashes`),
  extracts `InputAddressed` outputs (returns `DeferredDrv` if any output
  isn't `InputAddressed`), then calls `derivationModulo`. Used for
  computing a derivation's identity as an input to other derivations.
  The `InputAddressed` output type preserves output paths in the hash.

In both cases, the output type check and conversion happen in a single
`std::visit` over the output variants before calling `derivationModulo`,
making it impossible to reach it with an output variant that doesn't
match the target type.

`processDerivationOutputPaths` now works with `std::optional<Hash>`
instead of `DrvHashModulo`, since `CaOutputHashes` can never appear in
this context. We had an assert before but now we use the narrow return
type of `hashDerivationModulo` to prove that the case cannot happen.

`hashDerivationModuloImpl` is gone, so we don't have `Impl` in the
headers anymore, thankfully.

Relatedly, `HashModuloInputs` maps each input hash to a plain set of
output names, with no `DerivedPathMap` involved: the hash modulo is
only ever computed after dynamic inputs are resolved away, so the
nested `ChildNode` structure (and the versioned ATerm form it could
trigger) is statically impossible in that intermediate form.

Also, move the contents of `derivation/aterm.{cc,hh}` into `namespace
derivation`, and drop the now-redundant `Drv` / `Derivation` fragments
from the names inside it: `parse` (pairing with `unparse`), `read` /
`write` for the wire protocol, `hashModulo` / `hashInputModulo`,
`HashModulo` / `Hashes` / `hashes` / `HashFct`, and file-local
`parseOutput` / `readOutput` / `ATermVersion` / `pathInputModulo` /
`inputModulo`. Call sites are found by ADL where an argument is a
derivation type, and explicitly qualified otherwise
(`derivation::parse` and friends). The dead `resolveInputAddressed`
declaration (it lost its definition long ago) is removed rather than
moved.

Co-authored-by: Amaan Qureshi <[email protected]>
Assisted-by: Claude Fable 5
With the `builder-rpc-v0` experimental feature, outputs are
communicated to the builder over RPC (`SubmitOutput`) rather than via
environment variables, so `primops.cc` deliberately does not create
env vars named after outputs for such derivations.

But `processDerivationOutputPaths` did not know that: validation
(`checkInvariants`) would throw "derivation has missing environment
variable" for a fixed-output `builder-rpc-v0` derivation, and fill-in
(`fillInOutputPaths`) would invent the env var --- mutating the
derivation and thus changing its store path.

Detect the feature by parsing the full `derivation::Options` (no
hand-rolled `requiredSystemFeatures` reading), and skip output env
var filling and validation entirely for such derivations. Parsing
the options also has the nice side effect that `checkInvariants` now
catches malformed options. Note that when the derivation has input
derivations, they must be passed to the options parse so that output
placeholders in path-valued options (e.g. `exportReferencesGraph`)
are recognized rather than rejected as invalid store paths.

Relatedly, reject input-addressed (including deferred) derivations
that claim the feature: `builder-rpc-v0` may only be used with
content-addressing derivations, as the builder already enforces, and
now validation does too.

Assisted-By: Claude Fable 5 <[email protected]>
processDerivationOutputPaths only called type(drv) after the per-output
loop, so a mixed-output-type derivation reached hashModulo first, whose
maskOutputsAndEnv panics on non-input-addressed outputs. This turned a
catchable error into a process abort on checkInvariants and
fillInOutputPaths.
The isDeferred leniency in fillInOutputPaths only warned about a
mismatching env var but left it wrong, so the next checkInvariants on
the now-InputAddressed output threw anyway. Correct the env var during
fill-in so the derivation validates afterwards.
@Ericson2314
Ericson2314 force-pushed the drv-inputs-shuffle-modulo branch from ce41459 to a906dd5 Compare August 3, 2026 21:13
@Ericson2314
Ericson2314 added this pull request to the merge queue Aug 3, 2026
Merged via the queue into NixOS:master with commit 79f6e22 Aug 3, 2026
16 checks passed
@Ericson2314
Ericson2314 deleted the drv-inputs-shuffle-modulo branch August 3, 2026 22:25
@nixos-discourse

Copy link
Copy Markdown

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/nix-derivation-0-1/79491/2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c api Nix as a C library with a stable interface new-cli Relating to the "nix" command store Issues and pull requests concerning the Nix store with-tests Issues related to testing. PRs with tests have some priority

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants