Skip to content

fix(core): a host's mainModule is its fleet delivery under an entity kind above hosts - #805

Open
sini wants to merge 3 commits into
mainfrom
fix/mainmodule-ancestor-kind
Open

sini wants to merge 3 commits into
mainfrom
fix/mainmodule-ancestor-kind

Conversation

@sini

@sini sini commented Nov 28, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Fixes #795. Builds on #804 for the fleet landing key.

When an entity kind sits above hosts (den.schema.host.parent), a host's configuration is built from the fleet walk. That's where the content this kind delivers lands: a { environment, host, ... } include, its provides.to-users, its excludes, and the values a sibling's pipe collects. host.mainModule was the host's own host-rooted resolve, so it lacked all of that. On these hosts it now imports the fleet's delivery for that host. It reads that delivery through the same getter the configuration uses, keyed by name, system and class.

Some cases are unchanged:

  • A host that the fleet walk doesn't resolve directly under the parent kind keeps its host-rooted imports. So does a host whose fleet delivery is empty. A host resolved directly under that kind can receive its fleet delivery even when den doesn't place an output for it (see below).
  • Without a kind above hosts, nothing changes, and mainModule stays independent of its siblings.

mainModule is always { imports = …; }. So binding a peer's mainModule, or asking ? imports, during the walk still forces nothing. Reading the modules themselves is a late-phase read.

That changes one pattern that used to evaluate. Under a parent kind, an include or policy might force a peer's mainModule modules while the fleet is being built. It used to get that peer's host-rooted modules, and now it gets an infinite recursion. No documented use or known configuration does this. The docs now say to read those modules from a class module, an output or the repl.

There are also three behaviour changes for a gated host's mainModule:

  • reading it costs the whole fleet walk, as building any configuration does;
  • an error in another host's include reaches it;
  • a host delivered from two scopes is refused by name, where its configuration warns and keeps the last.

Hosts resolved but not placed

A host can be resolved directly under the kind above hosts without den placing it (intoAttr = [ ]), for example when another framework builds it from mainModule. As reported in review, such a host now also gets its part of the fleet resolve as its mainModule, so it sees that kind's context ({ environment, host, ... }). The delivery is built from each walked host scope, through the same materializer and lost-content check as an instantiated host's. It feeds mainModule only: the landing index behind hasAspect, and every nixosConfigurations, are unchanged.

  • If the same host is also instantiated, the instantiated delivery wins.
  • One scope reached twice is one occurrence.
  • If the host is resolved in two distinct scopes and instantiated in neither, reading its mainModule is refused by name. That covers two environments, or one environment with different bindings. It also covers an environment resolved once per system whose host policy doesn't filter by system, a config that evaluated before.
  • A host the walk never resolves directly under that kind, such as one under an intermediate kind, keeps its host-rooted resolve.

Known limit, unchanged here and present on main: two hosts with the same name on different systems, resolved under one environment scope, share a walked scope. Walk each system's hosts in that system's environment.

Validation

These results are from local runs on the same tree (now rebased onto main as 9092aadf). The hosted test workflow hasn't run yet, because it skips draft PRs.

  • templates/ci/modules/deadbugs/mainmodule-ancestor-kind.nix has 26 cells and passes 26/26. The base fails 9 of the 14 cells for instantiated hosts. The first commit alone fails exactly the 8 cells for hosts that are resolved but not placed. Each cell compares mainModule's markers with nixosConfigurations for one ancestor delivery shape:

    • an environment include;
    • provides.to-users, read both in NixOS and in home-manager;
    • den.schema.environment.excludes;
    • a sibling's pipe collect;
    • same-named hosts on two systems.

    Further cells cover no-parent laziness, sibling poisons, the duplicate refusal, the in-walk ? imports shape read, and the fleet/rooted selection function.

  • Mutants turn the suite red:

    • the gate always on;
    • the gate always off;
    • choosing fleet vs rooted before returning the module;
    • falling back on a duplicate;
    • letting a resolve-only delivery win over an instantiated one, switching the resolve-only scan off, dropping the parent-kind check, always keying by nixos, and keeping only the last scope per parent and host.
  • nixosConfigurations is unchanged. Toplevel drvs are identical to the base for the minimal, default and example templates and a provides.to-users fixture. They're also identical for a real fleet host that uses host.parent. That host's mainModule failed to evaluate before, and now gives the identical drv.

  • Cost at the same demand, one host's toplevel drvPath:

    fleet mainModule nixosConfigurations
    10 hosts 10.8M thunks 10.9M thunks
    50 hosts 20.0M thunks 20.1M thunks

    Without a parent kind, the change is +0.003%. The resolve-only scan adds 1,515 thunks (+0.008%) at 50 hosts.

  • The full suite passes 2377/2377. Checkmate passes 15/15, including the abort-on-warn checks (2/2).

@github-actions github-actions Bot added the allow-ci allow all CI integration tests label Nov 28, 2026
@AlexLov

AlexLov commented Nov 29, 2026

Copy link
Copy Markdown

Hey @sini,
Could you consider adding a test for case when nixosConfigurations instantiated outside of den but it's still possible to use {envirinment, host, ... } in aspects/classes?
Something like this

test-externally-instantiated-host = denTest (
  { den, ... }:
  {
    imports = [
      common
      envTopology
    ];

    # This is how I currently add hosts to environments and this seems working
    den.policies.test-env-to-host-no-instantiate =
      { environment, ... }:
        lib.concatMap (
          host:
          [ (resolve.to "host" { inherit host; }) ]
          ++ lib.optionals (host.intoAttr != [ ]) [
            (instantiate host)
          ]
        ) (builtins.attrValues (den.hosts.x86_64-linux or { }));

    den.schema.environment.excludes = [ den.policies.test-env-to-host ];
    den.schema.environment.includes = [ den.policies.test-env-to-host-no-instantiate ];
    den.hosts.x86_64-linux.shelf.intoAttr = [ ];
    den.aspects.shelf.nixos =
      { environment, host, ... }:
      {
        environment.etc."mark-${host.name}".text = environment.name;
      };

    expr = marks (fromMain den.hosts.x86_64-linux.shelf).environment.etc;
    expected = [
      "mark-base"
      "mark-shelf"
    ];
  }
);

I'm currently in the middle of migration from other framework to den and I cannot use den's host instantiation (yet), so I just pass mainModule as imports to that framework's hosts instantiation. That way I can "sit on both chairs" and slowly move all my configurations under den.
But I still want to fully experience all den's features. And currently I stuck on this problem when I need to get both environment and host to be passed to nixos class and it doesn't work even with this PR.

But it's possible that I just don't understand how all this should work and probably I'm using it wrong. If so could you point me out how to achieve this correctly?

@sini

sini commented Nov 29, 2026 •

Copy link
Copy Markdown
Collaborator Author

Thanks, this is a real gap. Your policy resolves every host but instantiates only the placed ones, so shelf is walked under its environment without being instantiated. #805 builds a host's mainModule from the fleet's delivery only for hosts den instantiates, so shelf falls back to its host-only resolve, which has no environment. I'm extending the fleet delivery to hosts the walk resolves but doesn't instantiate, and I've added your case as a test.

(Edited: I first said this policy was the one in the ACL environments guide. It isn't; the guide skips unplaced hosts entirely.)

sini added a commit that referenced this pull request Nov 29, 2026
…ts fleet delivery

A fleet that resolves every host under an environment but instantiates only
those with an `intoAttr` leaves the rest unplaced, and a config that builds
such a host itself, from its mainModule, got the host-rooted resolve: no
environment context, so a `{ environment, host, ... }` class module was
dropped. The fleet delivery index was built from instantiate specs only.

Each host scope the walk resolved directly under the parent kind with no
instantiate spec now gets a delivery bounded at that walked scope, through the
same materializer and lost-content check as an instantiated host. It feeds
mainModule only; the landing index behind hasAspect and every
nixosConfigurations are unchanged. An instantiated occurrence wins its key and
one physical scope reached twice is one occurrence. Two distinct walked scopes
of a host, from two environments or one environment with different bindings,
are refused by name when that host's mainModule is read. A host the walk never
resolves directly under that kind keeps its host-rooted resolve.

Reported in GitHub #805
@sini
sini force-pushed the fix/mainmodule-ancestor-kind branch from 1cf47e9 to 6deaa7c Compare November 29, 2026 18:06
Base automatically changed from fix/landing-index-per-system to main November 30, 2026 07:41
sini added 3 commits November 29, 2026 23:41
…kind above hosts

With `den.schema.host.parent` set, a host's configuration is built from the
fleet walk, where content an ancestor kind delivers (a `{ environment, host,
... }` include, its `provides.to-users`, its excludes, a sibling's pipe
values) lands. mainModule was always the host's own host-rooted resolve, so it
lacked that content. A host's mainModule now imports the fleet's delivery for
that host when it has one, through the same reading nixosConfigurations uses,
keyed by den.lib.aspects.__landingKey. A host the flake doesn't instantiate,
or whose delivery is empty, keeps its host-rooted resolve, as its
configuration does. Without a kind above hosts nothing changes.

mainModule is always `{ imports = …; }` with the choice inside, so binding a
peer's mainModule or asking `? imports` during the walk forces nothing. Its
content, once forced, is a late-phase value: reading a gated peer's imports
from an include still building the fleet recurses. Den's own fallback uses a
private host-rooted accessor, never the public mainModule. Reading a gated
host's mainModule pays the fleet walk, as nixosConfigurations does, and an
error in another host's include reaches it.

Reported in GitHub #795
…ts fleet delivery

A fleet that resolves every host under an environment but instantiates only
those with an `intoAttr` leaves the rest unplaced, and a config that builds
such a host itself, from its mainModule, got the host-rooted resolve: no
environment context, so a `{ environment, host, ... }` class module was
dropped. The fleet delivery index was built from instantiate specs only.

Each host scope the walk resolved directly under the parent kind with no
instantiate spec now gets a delivery bounded at that walked scope, through the
same materializer and lost-content check as an instantiated host. It feeds
mainModule only; the landing index behind hasAspect and every
nixosConfigurations are unchanged. An instantiated occurrence wins its key and
one physical scope reached twice is one occurrence. Two distinct walked scopes
of a host, from two environments or one environment with different bindings,
are refused by name when that host's mainModule is read. A host the walk never
resolves directly under that kind keeps its host-rooted resolve.

Reported in GitHub #805
@sini
sini force-pushed the fix/mainmodule-ancestor-kind branch from 6deaa7c to 9092aad Compare November 30, 2026 07:41
@sini
sini marked this pull request as ready for review November 30, 2026 07:41

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

allow-ci allow all CI integration tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

host.mainModule omits content that needs an ancestor entity's context (host.parent)

2 participants