Skip to content

fix: nested includes scoping and content wrapper callability - #527

Merged
sini merged 8 commits into
denful:mainfrom
sini:fix/nested-includes-and-content-functor
May 13, 2026
Merged

sini merged 8 commits into
denful:mainfrom
sini:fix/nested-includes-and-content-functor

Conversation

@sini

@sini sini commented May 13, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Three fixes for users migrating from provides/_ to direct nesting:

1. Nested sub-aspects no longer auto-walk. They must be explicitly included to emit class modules. This matches provides behavior and makes activation explicit. den.aspects.root = { includes = [ den.aspects.root.a ]; a.nixos...; b.nixos...; } now correctly includes only a.

2. Multi-def merge fixed at the source. aspectContentType.merge now produces per-key merged values instead of lossy shallow-merge (foldl' //). When multiple modules define den.aspects.system.base.nixos, all definitions contribute. This eliminates mergeContentValuesPerKey from the pipeline — every consumer sees properly merged keys regardless of code path.

3. Single-function content wrappers are callable. den.aspects.X.Y = fn: now gets __functor so (den.aspects.wm.gnome-autologin "benjamin") works without provides.

Changes

  • nix/lib/aspects/types.nix — per-key merge in aspectContentType.merge, __functor for single-function wrappers
  • nix/lib/aspects/fx/handlers/compile-static.nix — remove auto-walk (nestedToWalk, emitNestedAspect), net -80 lines
  • nix/lib/aspects/fx/content-util.nix — remove now-unused mergeContentValuesPerKey
  • 6 test files — update expectations, add explicit includes

Known limitation

nested-aspects.test-direct-nesting-basic fails — entity-linked nested keys (igloo.tux where tux is a declared user) previously relied on auto-walk for activation. Policy-driven activation for these is a separate design topic.

Test plan

  • 784/785 CI tests pass (1 known: entity-linked activation)
  • Multi-def merge: deadbugs.nested-aspect-merge, deadbugs.content-wrapper-functor
  • No auto-walk: deadbugs.nested-includes-scoping
  • Explicit includes: nested-aspects, structural-detection
  • Formatted with just fmt

🤖 Generated with Claude Code

When an aspect's includes list references its own nested sub-keys,
suppress auto-walk so only explicitly included sub-aspects are active.
This aligns direct nesting behavior with provides, where forwarded
keys are excluded from classification via __providesForwarded.

Also make single-function content wrappers callable by adding __functor.
Without this, den.aspects.X.Y where Y is a function (parametric aspect)
can't be invoked as (den.aspects.X.Y arg) unless using provides (_.Y).
sini added 2 commits May 13, 2026 00:55
Addresses code review: compare full provider path instead of just
leaf name to prevent theoretical false positives when unrelated
aspects share names. Also adds cross-aspect name collision test.
@sini

sini commented May 13, 2026

Copy link
Copy Markdown
Collaborator Author

Fixing another regression I found during implementation.

Nested sub-aspects are no longer auto-walked. They must be explicitly
included to emit class modules, matching provides behavior and making
activation explicit.

Multi-def merge is fixed at the source: aspectContentType.merge now
produces per-key merged values instead of lossy shallow-merge. This
eliminates the need for mergeContentValuesPerKey in the pipeline —
every consumer sees properly merged keys regardless of code path.

Also removes emitNestedAspect (dead code with nestedToWalk always [])
and mergeContentValuesPerKey from content-util (no longer needed).

Single-function content wrappers get __functor for callability without
provides (den.aspects.X.Y = fn: {...} is now callable).
@osatomic

Copy link
Copy Markdown

Found an issue with deeply nested sub-aspects that are defined in separate files.

test-something = denTest (
  { den, igloo, ... }:
  {
    # fails
    imports = [
      { den.aspects.root.sub1.sub2.a.nixos.environment.variables.FROM_A = "yes"; }
    ];
    # passes
    # den.aspects.root.sub1.sub2.a.nixos.environment.variables.FROM_A = "yes";

    den.aspects.root.sub1.sub2.b.nixos.environment.variables.FROM_B = "yes";

    den.hosts.x86_64-linux.igloo.users.tux = { };

    den.aspects.igloo.includes = [
      den.aspects.root.sub1.sub2.a
      den.aspects.root.sub1.sub2.b
    ];

    expr = {
      hasA = igloo.environment.variables ? FROM_A;
      hasB = igloo.environment.variables ? FROM_B;
    };
    expected = {
      hasA = true;
      hasB = true;
    };
  }
);

sini added 3 commits May 13, 2026 10:13
When a host aspect has a nested key matching a declared user name
(e.g., den.aspects.igloo.tux where tux is a user), the host-to-users
policy now emits a policy.include for it. This activates entity-named
sub-aspects without auto-walk — the policy layer handles it.

Keys already in __providesForwarded are excluded to prevent double
activation (provides path already handles those via mkCrossPolicy).
Remove unwrapContentValuesRaw (no longer imported after
emitNestedAspect removal) and unreachable else branch in
aspectContentType per-key merge.
aspectContentType.merge: skip per-key merge when there's only one
attrset definition (the common case). Avoids eager key enumeration
via lib.unique/concatMap on the hot path.

host-to-users policy: hoist forwarded set above concatMap so it's
computed once per host instead of per user. Inline entityAspectChild
as lib.optional for directness.
@sini

sini commented May 13, 2026

Copy link
Copy Markdown
Collaborator Author

Found an issue with deeply nested sub-aspects that are defined in separate files.

image

Roger roger, on it.

When a per-key merge produces a __contentValues wrapper (multi-def
case), shallow-forward the attrset sub-keys onto it so deeper nested
access works (e.g., den.aspects.root.sub1.sub2.a where sub2 is
defined across separate module imports).
@sini

sini commented May 13, 2026

Copy link
Copy Markdown
Collaborator Author

The above commit is a band-aid that fixes this edge case, but the right solution is going to be a refactor of the wrapping mechanism. At the expense of some scope creep, I'm going to try and tackle the refactor.

@sini

sini commented May 13, 2026

Copy link
Copy Markdown
Collaborator Author

Okay, I've confirmed that the scope for this refactor is going to actually scope creep to nearly the size of the already designed flake-aspect 2.0 library extraction effort scheduled for next week/after we do this point release so I'm going to leave the band-aid for now. We know what to replace, and we now have these tests to make sure we don't reintroduce the regression. Going to merge this.

@sini
sini merged commit df9f1c4 into denful:main May 13, 2026
10 checks passed
charlesfire pushed a commit to charlesfire/den that referenced this pull request May 16, 2026
)

## Summary

Three fixes for users migrating from `provides`/`_` to direct nesting:

**1. Nested sub-aspects no longer auto-walk.** They must be explicitly
included to emit class modules. This matches provides behavior and makes
activation explicit. `den.aspects.root = { includes = [
den.aspects.root.a ]; a.nixos...; b.nixos...; }` now correctly includes
only `a`.

**2. Multi-def merge fixed at the source.** `aspectContentType.merge`
now produces per-key merged values instead of lossy shallow-merge
(`foldl' //`). When multiple modules define
`den.aspects.system.base.nixos`, all definitions contribute. This
eliminates `mergeContentValuesPerKey` from the pipeline — every consumer
sees properly merged keys regardless of code path.

**3. Single-function content wrappers are callable.** `den.aspects.X.Y =
fn:` now gets `__functor` so `(den.aspects.wm.gnome-autologin
"benjamin")` works without `provides`.

### Changes

- `nix/lib/aspects/types.nix` — per-key merge in
`aspectContentType.merge`, `__functor` for single-function wrappers
- `nix/lib/aspects/fx/handlers/compile-static.nix` — remove auto-walk
(`nestedToWalk`, `emitNestedAspect`), net -80 lines
- `nix/lib/aspects/fx/content-util.nix` — remove now-unused
`mergeContentValuesPerKey`
- 6 test files — update expectations, add explicit includes

### Known limitation

`nested-aspects.test-direct-nesting-basic` fails — entity-linked nested
keys (`igloo.tux` where `tux` is a declared user) previously relied on
auto-walk for activation. Policy-driven activation for these is a
separate design topic.

## Test plan

- [x] 784/785 CI tests pass (1 known: entity-linked activation)
- [x] Multi-def merge: `deadbugs.nested-aspect-merge`,
`deadbugs.content-wrapper-functor`
- [x] No auto-walk: `deadbugs.nested-includes-scoping`
- [x] Explicit includes: `nested-aspects`, `structural-detection`
- [x] Formatted with `just fmt`

🤖 Generated with [Claude Code](https://claude.com/claude-code)
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.

3 participants