Repository navigation
libstore: compute the hash modulo via an intermediate derivation - #16191
Merged
Ericson2314 merged 6 commits intoAug 3, 2026
Merged
Conversation
Ericson2314
force-pushed
the
drv-inputs-shuffle-modulo
branch
4 times, most recently
from
July 23, 2026 00:51
6a5c3c4 to
517557f
Compare
Ericson2314
force-pushed
the
drv-inputs-shuffle-modulo
branch
2 times, most recently
from
July 30, 2026 20:34
60dda3f to
1be35d6
Compare
xokdvium
reviewed
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 | |||
Contributor
There was a problem hiding this comment.
Is there a reason this is compiled out currently?
Member
There was a problem hiding this comment.
I think this is a case that we made lenient at @Mic92's request to spread out the breakage with a warning instead
Member
There was a problem hiding this comment.
Now the test case is up and running
Ericson2314
force-pushed
the
drv-inputs-shuffle-modulo
branch
15 times, most recently
from
August 1, 2026 01:17
6b19310 to
1101f20
Compare
Mic92
approved these changes
Aug 3, 2026
Ericson2314
force-pushed
the
drv-inputs-shuffle-modulo
branch
from
August 3, 2026 20:10
4de8987 to
ce41459
Compare
Ericson2314
enabled auto-merge
August 3, 2026 20:13
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
force-pushed
the
drv-inputs-shuffle-modulo
branch
from
August 3, 2026 21:13
ce41459 to
a906dd5
Compare
|
This pull request has been mentioned on NixOS Discourse. There might be relevant details there: |
Draft
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.