Skip to content

nixd/AST: drop lib.attrValues attribute keys from option paths#829

Open
ariofrio wants to merge 1 commit into
nix-community:mainfrom
ariofrio:attrvalues-transparency
Open

nixd/AST: drop lib.attrValues attribute keys from option paths#829
ariofrio wants to merge 1 commit into
nix-community:mainfrom
ariofrio:attrvalues-transparency

Conversation

@ariofrio

@ariofrio ariofrio commented May 22, 2026

Copy link
Copy Markdown
Contributor

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:

{ lib, ... }: lib.mkMerge (lib.attrValues {
  ssh = {
    services.openssh.enable = true;
  };
  caddy = {
    services.caddy.enable = true;
  };
})

Today option hover and completion don't work inside these sections — the LSP returns no hover for enable. getValueAttrPath walks up to the surrounding { ssh = ...; caddy = ...; } attrset and prepends the section name to the path, because it doesn't know lib.attrValues discards keys.

Fix

When the enclosing ExprAttrs is the sole argument of a <x>.attrValues call, recurse past the call without appending the current binding's key. Matches both lib.attrValues and builtins.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.md covers 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 append imports to the path. Needs a separate imports-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 in idioms::mkVarSelector would unblock this (and improve idiom resolution more broadly), as a separate PR.

`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 ]`).
@ariofrio ariofrio changed the title nixd/AST: option-path transparency through lib.attrValues nixd/AST: drop lib.attrValues attribute keys from option paths May 22, 2026

@inclyc inclyc left a comment

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.

Why this PR is marked as 'draft'?

@inclyc inclyc left a comment

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.

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?

@inclyc inclyc marked this pull request as ready for review May 23, 2026 08:43
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.

2 participants