Skip to content

builtins.getFlake: Handle path:<p> where p has a discarded string context - #402

Merged
edolstra merged 1 commit into
mainfrom
getFlake-discarded-context
Mar 27, 2026
Merged

edolstra merged 1 commit into
mainfrom
getFlake-discarded-context

Conversation

@edolstra

@edolstra edolstra commented Mar 27, 2026 •

Copy link
Copy Markdown
Collaborator

Motivation

For instance, github:Mic92/sops-nix/8b89f44c2cc4581e402111d928869fe7ba9f7033 does this:

loadPrivateFlake =
  path:
  let
    flakeHash = builtins.readFile "${toString path}.narHash";
    flakePath = "path:${toString path}?narHash=${flakeHash}";
  in
  builtins.getFlake (builtins.unsafeDiscardStringContext flakePath);

This previously resulted in

error: path '/nix/store/2c8r30kz9zg8kz11ngp64ah5ya0hc011-source/dev/private/flake.nix' does not exist

when using lazy trees.

Fixes #345.

Context

Summary by CodeRabbit

  • Refactor
    • Optimized path-type flake reference handling for improved resolution when sources are already present in the store.

…text

For instance, github:Mic92/sops-nix/8b89f44c2cc4581e402111d928869fe7ba9f7033 does this:

  loadPrivateFlake =
    path:
    let
      flakeHash = builtins.readFile "${toString path}.narHash";
      flakePath = "path:${toString path}?narHash=${flakeHash}";
    in
    builtins.getFlake (builtins.unsafeDiscardStringContext flakePath);

This previously resulted in

  error: path '/nix/store/2c8r30kz9zg8kz11ngp64ah5ya0hc011-source/dev/private/flake.nix' does not exist

Fixes #345.
@edolstra edolstra added the flake-regression-test Run the flake regressions test suite on this PR label Mar 27, 2026
@edolstra edolstra closed this Mar 27, 2026
@edolstra edolstra reopened this Mar 27, 2026
@coderabbitai

coderabbitai Bot commented Mar 27, 2026 •

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 612d411c-fdb6-4aae-b482-1a5eb8260b33

📥 Commits

Reviewing files that changed from the base of the PR and between 6ab99ab and b3b623d.

📒 Files selected for processing (1)
  • src/libflake/flake-primops.cc

📝 Walkthrough

Walkthrough

The getFlake primitive now special-cases path-type flake references after devirtualization. When a path flake ref resolves to a store-present path, the code derives the store path, scans the realized string context for a matching path element, constructs a concrete path, and invokes lockFlake directly with that path before returning, bypassing the original flow.

Changes

Cohort / File(s) Summary
Path Flake Reference Handling
src/libflake/flake-primops.cc
Added special-case logic in getFlake primitive to detect path-type flake refs, derive store paths, match against realized NixStringContext elements, and construct concrete paths for direct lockFlake invocation.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested reviewers

  • cole-h

Poem

🐰 A path through the store, once twisted and lost,
Now springs straight and true—no context cost!
The flakes align where virtues meet real,
And sops-nix can show what it's meant to reveal! ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: handling path flake references with discarded string context in builtins.getFlake, which is the core fix for the reported issue.
Linked Issues check ✅ Passed The changes directly address issue #345 by implementing special handling for 'path:' flake refs with discarded context, allowing builtins.getFlake to resolve the flake instead of failing with path-not-exist errors.
Out of Scope Changes check ✅ Passed All changes in src/libflake/flake-primops.cc are focused solely on fixing the getFlake primitive to handle path refs with discarded context, remaining within the scope of issue #345.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch getFlake-discarded-context

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions

github-actions Bot commented Mar 27, 2026 •

Copy link
Copy Markdown

@github-actions
github-actions Bot temporarily deployed to pull request March 27, 2026 11:35 Inactive
@github-actions
github-actions Bot temporarily deployed to pull request March 27, 2026 11:36 Inactive
@edolstra
edolstra added this pull request to the merge queue Mar 27, 2026
Merged via the queue into main with commit d21cf80 Mar 27, 2026
94 checks passed
@edolstra
edolstra deleted the getFlake-discarded-context branch March 27, 2026 15:32
xokdvium added a commit to NixOS/nix that referenced this pull request Apr 19, 2026
This builds on top of #14050 to
actually make flakes get lazily copied to the store. This repurposes
a slightly less lazy (but also more deterministic) approach that
determinate nix has taken. We do still pay to cost of hashing an input
once to compute the store path and narHash daemon-client-side. This
could be improved in follow-ups in case we don't actually need to check
the narHash (like during local development).

We need certain backwards compatibility hacks for getFlake with a discarded string
context, those are similar to what detnix does.

See: DeterminateSystems#422
See: DeterminateSystems#402

Co-authored-by: Eelco Dolstra <edolstra@gmail.com>
xokdvium added a commit to NixOS/nix that referenced this pull request Apr 19, 2026
This builds on top of #14050 to
actually make flakes get lazily copied to the store. This repurposes
a slightly less lazy (but also more deterministic) approach than
determinate nix has taken. We do still pay to cost of hashing an input
once to compute the store path and narHash daemon-client-side. This
could be improved in follow-ups in case we don't actually need to check
the narHash (like during local development).

We need certain backwards compatibility hacks for getFlake with a discarded string
context, those are similar to what detnix does.

See: DeterminateSystems#422
See: DeterminateSystems#402

Co-authored-by: Eelco Dolstra <edolstra@gmail.com>
xokdvium added a commit to NixOS/nix that referenced this pull request Apr 25, 2026
This builds on top of #14050 to
actually make flakes get lazily copied to the store. This repurposes
a slightly less lazy (but also more deterministic) approach than
determinate nix has taken. We do still pay to cost of hashing an input
once to compute the store path and narHash daemon-client-side. This
could be improved in follow-ups in case we don't actually need to check
the narHash (like during local development).

We need certain backwards compatibility hacks for getFlake with a discarded string
context, those are similar to what detnix does.

See: DeterminateSystems#422
See: DeterminateSystems#402

Co-authored-by: Eelco Dolstra <edolstra@gmail.com>
xokdvium added a commit to NixOS/nix that referenced this pull request Apr 25, 2026
This builds on top of #14050 to
actually make flakes get lazily copied to the store. This repurposes
a slightly less lazy (but also more deterministic) approach than
determinate nix has taken. We do still pay to cost of hashing an input
once to compute the store path and narHash daemon-client-side. This
could be improved in follow-ups in case we don't actually need to check
the narHash (like during local development).

We need certain backwards compatibility hacks for getFlake with a discarded string
context, those are similar to what detnix does.

See: DeterminateSystems#422
See: DeterminateSystems#402

Co-authored-by: Eelco Dolstra <edolstra@gmail.com>
xokdvium added a commit to NixOS/nix that referenced this pull request Apr 25, 2026
This builds on top of #14050 to
actually make flakes get lazily copied to the store. This repurposes
a slightly less lazy (but also more deterministic) approach than
determinate nix has taken. We do still pay to cost of hashing an input
once to compute the store path and narHash daemon-client-side. This
could be improved in follow-ups in case we don't actually need to check
the narHash (like during local development).

We need certain backwards compatibility hacks for getFlake with a discarded string
context, those are similar to what detnix does.

See: DeterminateSystems#422
See: DeterminateSystems#402

Co-authored-by: Eelco Dolstra <edolstra@gmail.com>
Mic92 pushed a commit to Mic92/nix-1 that referenced this pull request Apr 26, 2026
This builds on top of NixOS#14050 to
actually make flakes get lazily copied to the store. This repurposes
a slightly less lazy (but also more deterministic) approach than
determinate nix has taken. We do still pay to cost of hashing an input
once to compute the store path and narHash daemon-client-side. This
could be improved in follow-ups in case we don't actually need to check
the narHash (like during local development).

We need certain backwards compatibility hacks for getFlake with a discarded string
context, those are similar to what detnix does.

See: DeterminateSystems#422
See: DeterminateSystems#402

Co-authored-by: Eelco Dolstra <edolstra@gmail.com>

This branch was previously deployed

1 inactive deployment
pull request — b3b623db Deployed Mar 27, 2026 by github-actions[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

flake-regression-test Run the flake regressions test suite on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

nix flake show for sops-nix fails

2 participants