Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,11 @@ assembled, and has these limits:
entries;
- in a `mainModule` build, `host.hasAspect` answers about what lands in the
configuration the flake builds (`nixosConfigurations`), not about that
`mainModule` build, and the two can differ for content an entity kind above
the host delivers ([#795](https://github.com/denful/den/issues/795)). A
`mainModule` class module that reads `host` also forces the flake's resolve;
`mainModule` build. With an entity kind above hosts the build carries the
same modules; without one it lacks values the host's pipes collect from
other hosts. A host den doesn't place has no such configuration, so its
`host.hasAspect` keeps the walk's answer. A `mainModule` class module that
reads `host` also forces the flake's resolve;
- a target query forces every instantiated host's walk, including its
includes, but no module bodies or option values: the first entity binding
a class module reads forces the head of every host's delivery (its scopes),
Expand Down
4 changes: 4 additions & 0 deletions docs/src/content/docs/guides/debug.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ nix-repl> cfg = (lib.nixosSystem { modules = [ module ]; }).config
nix-repl> cfg.networking.hostName
```

With an entity kind above hosts (`den.schema.host.parent`), the first read of
a host's `mainModule` resolves the whole fleet, as `nixosConfigurations` does.
See [Host instantiation](/reference/output/#host-instantiation).

## Common Issues

**Duplicate values in lists**: Den deduplicates owned and static configs
Expand Down
33 changes: 25 additions & 8 deletions docs/src/content/docs/reference/output.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,31 @@ host.instantiate {
}
```

`host.mainModule` is internally computed by resolving the host's aspect
with its context, collecting all class-specific config
and dispatched includes. For a host resolved from its own context (no entity
kind above the host, such as `den.schema.host.parent`), building it on its own
gives the same host-class content as the configuration den puts in
`flake.<intoAttr>`, including what the host delivers to its users and receives
back through them (`provides.to-users`, `provides.<user>`). Content that needs
an ancestor kind's context isn't in `mainModule`.
What a `mainModule` build carries depends on whether an entity kind sits above
hosts:

- **No entity kind above hosts** (the default): `mainModule` is the host's own
resolve, computed from the host alone. It has the host-class content of the
configuration den puts in `flake.<intoAttr>`, including what the host
delivers to its users and receives back through them (`provides.to-users`,
`provides.<user>`), but not values its pipes collect from other hosts.
- **An entity kind above hosts** (`den.schema.host.parent`, such as an
`environment`): `mainModule` is the host's part of the fleet-wide resolve,
the same modules its configuration is built from, so it carries what that
kind delivers too. Reading its modules pays for the whole fleet walk the
first time, as building any host's configuration already does, and an error
in another host's include reaches it, as it reaches every configuration den
builds. A host delivered from two scopes is refused by name, where its
configuration warns and keeps the last. A host den resolves directly under
that kind but doesn't place (`intoAttr = [ ]`, built by another framework
from its `mainModule`) gets its part of the resolve too, and an
instantiated occurrence of it wins over one that's only resolved. A host the
walk never resolves directly under that kind keeps its own resolve.

Under an entity kind above hosts, a host's `mainModule` is a late value: an
include or policy can bind it, or ask `mainModule ? imports`, but reading the
modules inside it while den is still resolving the fleet is a cycle. Read them
from a class module, a flake output or the repl, after the walk.

The result is placed at `flake.<intoAttr>` -- by default
`flake.nixosConfigurations.<name>` or `flake.darwinConfigurations.<name>`.
Expand Down
1 change: 1 addition & 0 deletions docs/src/content/docs/whats-changed.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ The same release lets a host choose what it receives (`admits`), lets a policy n

- **In-context `hasAspect` answers what lands in the entity.** In a class module taking `host` as its argument, `host.hasAspect X` is true when X's host-class content lands in the host's config, and `user.hasAspect X` when X's user-class content lands in that user's, the same from whichever scope asks, including content an entity kind above the host (an `environment`) delivers. It used to answer "is X in the asking scope", so a user's homeManager module could see `host.hasAspect` false for an aspect the host itself included, and true for one only that user included. Roles count through their includes, a provide is its own aspect, and forwards into the host count except per-user nests. The registry lookup `den.hosts.<s>.<h>.hasAspect` is unchanged, and policies, computed includes and guards keep their current answer; the two forms aren't promised to agree while den moves to a finalized query, whose public name is pending. The answer reaches an entity taken as the class module's own argument (`nixos = { host, ... }:`); a `host` the aspect function binds keeps the walk's answer. See [What "present" means](/explanation/structural-introspection/#what-present-means).
- **`mainModule` now matches what den builds.** A host's `mainModule` missed host content the host delivered through its users, from `provides.to-users`, `provides.<user>` and host policies that fire for each user. `nixosConfigurations` always had it. A host built from `host.mainModule` now gains that content; `nixosConfigurations` doesn't change.
- **Under an entity kind above hosts, `mainModule` is what the host is built from.** With `den.schema.host.parent` set (such as an `environment`), a host's `mainModule` is now its part of the fleet-wide resolve, so it gains what that kind delivers: its includes, its excludes, its `provides.to-users`, and values a pipe collects from sibling hosts. Reading it pays for the fleet walk, as building any host already does. An include or policy can still bind a peer's `mainModule` or ask `? imports`, but reading the modules inside it while den resolves the fleet is now a cycle; read them from a class module, an output or the repl. This includes a host den resolves directly under that kind but doesn't place (`intoAttr = [ ]`), whose `mainModule` another framework builds. Without a kind above hosts nothing changes, and `nixosConfigurations` doesn't change either. See [Host instantiation](/reference/output/#host-instantiation).
- **`den.aspects.<host>.<user>` reaches that user only.** A host aspect's child named after a user used to be included at host scope, so its `homeManager` and `user` content reached every user on the host, and a `{ user, ... }:` child ran once per user. It's now part of that user's own aspect on that host, next to `den.aspects."<user>@<host>"` and `den.aspects.<user>`, so it reaches that user, once. If a config relied on the old behaviour to give every user the same content, move that content to `den.aspects.<host>.provides.to-users` or `den.schema.user.includes`.

## Upgrading in one jump
Expand Down
1 change: 1 addition & 0 deletions nix/lib/aspects/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ in
mkProjectedHasAspect
__mkLateHasAspect
__landingKey
__mainModuleImports
;
mkAspectsType = typeCfg: lib.mapAttrs (_: v: v typeCfg) rawTypes;
# Predicates exported directly (not through types mapAttrs which applies { } to each value).
Expand Down
146 changes: 105 additions & 41 deletions nix/lib/aspects/fx/resolve.nix
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,12 @@ let
sid = spec.sourceScopeId or null;
idHash = spec.id_hash or null;
in
if sid != null && idHash != null then scopeByEntity."${sid}\n${idHash}" or null else null;
if spec ? walkedScopeId then
spec.walkedScopeId
else if sid != null && idHash != null then
scopeByEntity."${sid}\n${idHash}" or null
else
null;

# The per-host subtree extraction that produced the complete module set for a
# host (host-scope + user-scope + route-delivered modules, key-deduped) now
Expand Down Expand Up @@ -427,6 +432,25 @@ let
} { doFinalMerge = true; };
};

# Shared by the instantiation and the fleet delivery index so the two can't drift.
deliveredModulesOf =
spec: pm:
if pm == null then
null
else
let
inherit (pm) proj materialized;
inherit (proj) hostScopeId hostClass;
hostModules =
if proj.unbounded then
materialized.${hostClass} or [ ]
else
checkLost (spec.name or hostScopeId) hostClass (materialized.__lostAt hostClass) (
materialized.${hostClass} or [ ]
);
in
if hostModules == [ ] then null else hostModules;

# Build instantiateArgs for a spec without calling spec.instantiate.
# Factored out so both applyInstantiates and hostConfigs can reuse it.
mkInstantiateArgs =
Expand All @@ -437,25 +461,13 @@ let
spec:
let
pm = if materializedFor != null then materializedFor spec else perHostMaterialized argBundle spec;
preWalkedModules =
if pm != null then
let
inherit (pm) proj materialized;
inherit (proj) hostScopeId hostClass;
hostModules =
if proj.unbounded then
materialized.${hostClass} or [ ]
else
checkLost (spec.name or hostScopeId) hostClass (materialized.__lostAt hostClass) (
materialized.${hostClass} or [ ]
);
in
if hostModules == [ ] then null else hostModules
else
null;
preWalkedModules = deliveredModulesOf spec pm;
# Not the public mainModule: under an ancestor kind it reads this delivery.
modules =
if preWalkedModules != null then
preWalkedModules
else if spec ? __rootedModule then
[ spec.__rootedModule ]
else
lib.optional (spec ? mainModule) spec.mainModule;
in
Expand Down Expand Up @@ -1160,37 +1172,88 @@ let
) { } subtree;
};

# Each instantiated root entity's landing, by den.lib.aspects.__landingKey of its spec; a key two scopes deliver throws only when read.
fleetLanding =
builtins.mapAttrs
# Each instantiated root entity's deliveries, grouped by den.lib.aspects.__landingKey of its spec.
deliveriesByKey = builtins.groupBy (d: den.lib.aspects.__landingKey d.spec) (
lib.concatMap (
k:
let
pm = perHostByKey.${k};
spec = specByKey.${k};
in
lib.optional (pm != null && spec ? id_hash) { inherit pm spec; }
) (builtins.attrNames perHostByKey)
);
# A key two scopes deliver throws, naming both, only when it is read.
onlyDelivery =
ds:
let
d = builtins.head ds;
in
if builtins.length ds > 1 then
throw "den: two scopes deliver ${d.spec.name or "?"} (${d.spec.system or "?"}, ${d.spec.class or "?"}) to the fleet landing index: ${
lib.concatMapStringsSep " and " (x: x.pm.proj.hostScopeId) ds
}"
else
d;
fleetLanding = builtins.mapAttrs (
_: ds:
let
d = onlyDelivery ds;
in
landingOf {
rootScopeId = d.pm.proj.hostScopeId;
inherit (d.pm) materialized;
subtree = d.pm.proj.subtreeScopeIds;
}
) deliveriesByKey;
# Hosts walked under the parent kind but not placed: mainModule's only, not the landing index's.
hostParentKind = den.schema.host.parent or null;
resolveOnlyByKey = builtins.groupBy (d: den.lib.aspects.__landingKey d.spec) (
lib.concatMap
(
_: ds:
sid:
let
d = builtins.head ds;
parent = scopeParent.${sid} or null;
h = (scopeContexts.${sid} or { }).host or null;
spec = {
walkedScopeId = sid;
sourceScopeId = parent;
inherit (h) id_hash name system;
class = h.class or "nixos";
};
in
if builtins.length ds > 1 then
throw "den: two scopes deliver ${d.spec.name or "?"} (${d.spec.system or "?"}, ${d.spec.class or "?"}) to the fleet landing index: ${
lib.concatMapStringsSep " and " (x: x.pm.proj.hostScopeId) ds
}"
else
landingOf {
rootScopeId = d.pm.proj.hostScopeId;
inherit (d.pm) materialized;
subtree = d.pm.proj.subtreeScopeIds;
lib.optional
(
(scopeEntityKind.${sid} or null) == "host"
&& parent != null
&& (scopeEntityKind.${parent} or null) == hostParentKind
&& builtins.isAttrs h
&& h ? id_hash
)
{
inherit spec;
pm = perHostMaterialized instantiateBundle spec;
}
)
(
builtins.groupBy (d: den.lib.aspects.__landingKey d.spec) (
lib.concatMap (
k:
let
pm = perHostByKey.${k};
spec = specByKey.${k};
in
lib.optional (pm != null && spec ? id_hash) { inherit pm spec; }
) (builtins.attrNames perHostByKey)
# Every walked host scope; scopeByEntity keeps only the last per (parent, id_hash).
lib.optionals (hostParentKind != null) (
lib.concatMap builtins.attrNames (
builtins.attrValues (((result.state.boundEntityIndex or (_: { })) null).host or { })
)
)
);
)
);
# An instantiated entry wins its key; modules null where it fell back to the host-rooted resolve.
fleetDelivery = builtins.mapAttrs (
_: ds:
let
d = onlyDelivery ds;
in
{
modules = deliveredModulesOf d.spec d.pm;
}
) (resolveOnlyByKey // deliveriesByKey);

# TODO(#694): remove with the `isolated` alias. Every entity scope now
# bounds its ancestors' merge, so the flag only matters for a scope of an
Expand Down Expand Up @@ -1384,6 +1447,7 @@ let
# instantiates, with the include chains its locations map through.
fleet = {
landing = fleetLanding;
delivery = fleetDelivery;
chains = (result.state.scopedLocChains or (_: { })) null;
};
# The production edge object (see productionEdgeTrace above).
Expand Down
10 changes: 10 additions & 0 deletions nix/lib/aspects/has-aspect.nix
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ let
# id_hash is kind + name only, so same-named hosts on two systems need system and class too.
__landingKey = e: "${e.id_hash or ""}\n${e.system or ""}\n${e.class or ""}";

# Factored out so the empty-delivery fallback is testable without a fixture.
__mainModuleImports =
{
gated,
delivered,
rooted,
}:
if gated && delivered != null then delivered else rooted;

# The finalized (late) hasAspect over the fleet landing index: is ref's
# content, or that of an aspect it includes, in the entity's own evaluation
# in that class. `landing` is the entity's index entry: a root entity's
Expand Down Expand Up @@ -187,5 +196,6 @@ in
mkProjectedHasAspect
__mkLateHasAspect
__landingKey
__mainModuleImports
;
}
34 changes: 34 additions & 0 deletions nix/lib/entities/_types.nix
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,38 @@ let
default = den.lib.aspects.resolveWithPaths config.class config.resolved;
};

# Private, so den's instantiation fallback never reads the fleet through mainModule.
rootedModuleOption =
_den: config:
lib.mkOption {
internal = true;
visible = false;
readOnly = true;
type = lib.types.raw;
defaultText = "{ inherit (config.__resolveResult) imports; }";
default = { inherit (config.__resolveResult) imports; };
};

# Under an ancestor kind only the fleet walk sees what that kind delivers.
hostMainModuleOption =
den: config:
lib.mkOption {
internal = true;
visible = false;
readOnly = true;
type = lib.types.deferredModule;
defaultText = "the fleet's delivery for this host, or { inherit (config.__resolveResult) imports; }";
# The choice sits inside `imports` so binding or `? imports` forces nothing.
default = {
imports = den.lib.aspects.__mainModuleImports {
gated = (den.schema.host.parent or null) != null;
delivered =
(den.__fleet.fleet.delivery.${den.lib.aspects.__landingKey config} or { }).modules or null;
rooted = config.__resolveResult.imports;
};
};
};

# mainModule now derives imports from the shared result (no second run).
mainModuleOption =
_den: config:
Expand Down Expand Up @@ -299,6 +331,8 @@ in
lookupAspectBy
deepMergeAttrs
mainModuleOption
hostMainModuleOption
rootedModuleOption
resolveResultOption
pathSetByScopeOption
resolvedCtxModule
Expand Down
2 changes: 2 additions & 0 deletions nix/lib/entities/home.nix
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ let
lookupAspectBy
deepMergeAttrs
mainModuleOption
rootedModuleOption
resolveResultOption
pathSetByScopeOption
preprocessHosts
Expand Down Expand Up @@ -233,6 +234,7 @@ let
.${config.class};
};
mainModule = mainModuleOption den config;
__rootedModule = rootedModuleOption den config;
__resolveResult = resolveResultOption den config;
__pathSetByScope = pathSetByScopeOption den "home" config;
};
Expand Down
6 changes: 4 additions & 2 deletions nix/lib/entities/host.nix
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ let
lookupAspect
lookupAspectBy
deepMergeAttrs
mainModuleOption
hostMainModuleOption
rootedModuleOption
resolveResultOption
pathSetByScopeOption
preprocessHosts
Expand Down Expand Up @@ -132,7 +133,8 @@ let
}
.${config.class};
};
mainModule = mainModuleOption den config;
mainModule = hostMainModuleOption den config;
__rootedModule = rootedModuleOption den config;
__resolveResult = resolveResultOption den config;
__pathSetByScope = pathSetByScopeOption den "host" config;
};
Expand Down
Loading
Loading