Skip to content

Comments

builtins.getFlake fixes#337

Merged
edolstra merged 4 commits intomainfrom
fix-302
Feb 6, 2026
Merged

builtins.getFlake fixes#337
edolstra merged 4 commits intomainfrom
fix-302

Conversation

@edolstra
Copy link
Collaborator

@edolstra edolstra commented Feb 6, 2026

Motivation

Fixes #302:

  • Devirtualize strings in builtins.getFlake.
  • Propagate string context in builtins.flakeRefToString.
  • Force attributes in builtins.flakeRefToString to fix unevaluated thunk errors.
  • Make fetchTree return a lazily computed NAR hash for backward compatibility.

Note: allowing builtins.getFlake to fetch from store paths is probably a bad idea, since it's ambiguous when using chroot stores. So it prints a warning now.

Context

Summary by CodeRabbit

  • New Features

    • Added an internal NAR-hash computation and a lazy fallback for missing narHash values.
  • Bug Fixes

    • Improved string/context handling for flake references and added a deprecation warning emitted during evaluation.
  • Tests

    • Added a functional test covering flake reference evaluation and the new deprecation warning.

@coderabbitai
Copy link

coderabbitai bot commented Feb 6, 2026

📝 Walkthrough

Walkthrough

Adds an internal __narHash primop to compute NAR SHA-256 (SRI format) for a path, uses it as a lazy fallback when narHash is absent during tree emission, introduces contextual string handling in flake reference evaluation, and adds a functional regression test for parent-flake access from a subflake.

Changes

Cohort / File(s) Summary
Core NAR hash computation
src/libexpr/primops.cc
Added internal __narHash primop (__narHash(p)) that realises a path, performs a dry-run fetch-to-store to compute the NAR SHA-256, returns the SRI-formatted hash as a Nix string.
Lazy hash fallback in tree emission
src/libexpr/primops/fetchTree.cc
When input.getNarHash() is missing, populate narHash by invoking the internal __narHash primop on the store path alias, enabling lazy computation for backwards compatibility.
Flake reference context handling
src/libflake/flake-primops.cc
Introduced NixStringContext usage and switched to forceString with context in getFlake, prim_flakeRefToString, and flakeRefToString; forces attribute values before string extraction and emits warnings if context is present.
Tests - flakes suite
tests/functional/flakes/get-flake.sh, tests/functional/flakes/meson.build
Added a functional test that stages a subflake referencing its parent via builtins.getFlake/builtins.flakeRefToString, checks for a deprecation warning and correct evaluated value; added test to meson build list.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

Suggested labels

flake-regression-test

Suggested reviewers

  • cole-h
  • grahamc

Poem

🐰 Through lazy trees I bound and dash,
I found a missing NAR and made a hash.
A tiny primop hummed its tune,
Parent flakes now greet the moon,
Hooray — no phantom store-path crash!

🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'builtins.getFlake fixes' directly relates to the main objective of the PR, which is to fix bugs in builtins.getFlake and related flake functionality.
Linked Issues check ✅ Passed The PR implements all required fixes: devirtualizes strings in builtins.getFlake, propagates string context in builtins.flakeRefToString, forces attributes to prevent thunk errors, and adds lazy NAR hash computation for backward compatibility.
Out of Scope Changes check ✅ Passed All changes directly address issue #302: the primop addition, context handling, attribute forcing, and test cases are all in scope and necessary to fix the lazy-trees parent flake reference error.

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

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix-302

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

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@src/libexpr/primops/fetchTree.cc`:
- Around line 33-42: The lookup key for the internal primop is wrong: replace
the call that uses get(state.internalPrimOps, "narHash") in fetchTree.cc so it
requests the registered name "__narHash" instead; ensure the code uses the same
string used when the primop was registered (internalPrimOps.emplace(primOp.name,
...)) so get(state.internalPrimOps, "__narHash") returns the correct primop for
the mkApp call that builds the lazy NAR-hash.

@github-actions
Copy link

github-actions bot commented Feb 6, 2026

@github-actions github-actions bot temporarily deployed to pull request February 6, 2026 14:16 Inactive
Allowing strings with context here is probably a mistake since in a
chroot evaluation, it's ambiguous what filesystem store paths refer
to. Hence the warning.

Fixes #302.
Fixes "attribute 'x' is a thunk".
@github-actions github-actions bot temporarily deployed to pull request February 6, 2026 15:07 Inactive
@edolstra edolstra added this pull request to the merge queue Feb 6, 2026
Merged via the queue into main with commit d69e6ae Feb 6, 2026
28 checks passed
@edolstra edolstra deleted the fix-302 branch February 6, 2026 15:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

lazy-trees: error when referencing parent flake from subflake

2 participants