nixd/AST: drop lib.attrValues attribute keys from option paths#829
Open
ariofrio wants to merge 1 commit into
Open
nixd/AST: drop lib.attrValues attribute keys from option paths#829ariofrio wants to merge 1 commit into
ariofrio wants to merge 1 commit into
Conversation
`getValueAttrPath` walks the AST parent chain to build the option-path
prefix for a cursor position inside a NixOS module. It terminates at
the first non-`ExprAttrs` ancestor, which means option hover and
completion are unavailable inside the idiomatic
`lib.mkMerge (lib.attrValues { name = MODULE; ... })` pattern: the
walker bails at the surrounding `ExprCall`, after incorrectly
prepending the section attribute name (e.g. "ssh") to the path.
Add a syntactic transparency rule to the walker: when the enclosing
`ExprAttrs` is itself the sole argument of a `<x>.attrValues` call,
the call discards attribute names. Recurse past the call without
appending the current binding's key, so the inner module's option
paths resolve unprefixed.
False-positive risk is bounded by the options-tree lookup downstream:
a misrecognized call only causes the analyzer to construct a path that
doesn't match any real option, producing no hover (same as today's
fail-closed behavior).
The check is purely syntactic and matches `<x>.attrValues` directly,
without resolving identifier aliasing (e.g.
`let av = lib.attrValues; in av {...}` or
`with builtins; attrValues {...}` aren't recognized). A follow-up to
route the check through `VariableLookupAnalysis` would make
recognition robust against aliasing.
Test fixture exercises the standalone attrValues case. All 188
existing tests still pass.
Two related extensions are deferred to separate changes: an `imports`
binding-key passthrough (would unlock
`{ imports = lib.attrValues {...}; }`) and let-bound section
identifiers (would unlock `let ssh = MODULE; in mkMerge [ ssh ]`).
inclyc
reviewed
May 23, 2026
inclyc
left a comment
Member
There was a problem hiding this comment.
Why this PR is marked as 'draft'?
inclyc
reviewed
May 23, 2026
inclyc
left a comment
Member
There was a problem hiding this comment.
The code itself LGTM, what I'm most concerned about right now is whether this idiom is actually widely adopted — meaning, have we perhaps found a pattern match that isn't all that complex but may not be entirely correct, and might not be very broadly used? Is this something officially recommended, or widely suggested by some kind of styling guide?
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.
Motivation
A neat little idiom for organizing larger NixOS modules in a single file is to give each chunk of related configuration a name and merge them via
lib.mkMerge (lib.attrValues { ... }). Each section attribute (ssh,caddy, etc.) is decorative — the merge collapses them at the option level — but the names document what each block does and let readers jump to them by attribute key:Today option hover and completion don't work inside these sections — the LSP returns no hover for
enable.getValueAttrPathwalks up to the surrounding{ ssh = ...; caddy = ...; }attrset and prepends the section name to the path, because it doesn't knowlib.attrValuesdiscards keys.Fix
When the enclosing
ExprAttrsis the sole argument of a<x>.attrValuescall, recurse past the call without appending the current binding's key. Matches bothlib.attrValuesandbuiltins.attrValues(any ExprSelect ending in.attrValues). False positives are bounded by the options-tree lookup — a misrecognized call only causes the analyzer to build a path that doesn't match any real option, producing no hover (same fail-closed behavior as today).nixd/tools/nixd/test/hover/attrvalues-transparency.mdcovers this end-to-end. All 188 existing tests still pass.Not yet handled
Neither of these is a regression — both already produce no hover on master:
{ imports = lib.attrValues {...}; }— the walker now lands in the surrounding{ imports = …; }attrset and would appendimportsto the path. Needs a separateimports-key passthrough rule.Let-bound aliases like
let av = lib.attrValues; in lib.mkMerge (av {...})— the check matches the function expression syntactically, not through identifier rebinding. Following let/rec bindings inidioms::mkVarSelectorwould unblock this (and improve idiom resolution more broadly), as a separate PR.