Skip to content

Explicit Defaults & Layered Overrides

As a multi-host configuration grows, it helps to distinguish between fixed fleet facts and layered defaults. Yen handles both explicitly:

  • You can spread one host’s fields across several files freely, as long as each file sets distinct fields.
  • If two files give different values for the same field, Yen reports a two-answers error naming both files instead of guessing.
  • When you want fallback defaults or layered overrides, you state that intent with yen.lib.cascade, typed aspect settings, or reduce.layers.
modules/fleet.nix
{
yen,
std,
types,
reduce,
...
}:
{
imports = [ std.modules.all ];
yen.kinds.env.fields.tz = types.maybe types.str;
yen.kinds.host = {
fields = {
env = types.opt yen.kinds.env yen.env.prod;
tz = types.maybe types.str;
};
# Explicit ranked layers instead of `mkDefault` / `mkForce` battles.
sinks.sysctl = {
reduce = reduce.layers;
into.nixos = { sysctl, ... }: { boot.kernel.sysctl = sysctl; };
};
};
yen.env.prod.tz = "UTC";
yen.host.api.env = yen.env.prod;
yen.host.eu = {
env = yen.env.prod;
tz = "Europe/Berlin";
};
# Shared helper available to any module via `yen.helpers`.
yen.helpers.hostTz =
host:
yen.lib.cascade yen.kinds.host.fields.tz [
host
host.env
];
}
modules/aspects.nix
{
yen,
types,
...
}:
{
yen.aspects.acme = {
at = yen.kinds.host;
settings = {
email = types.str;
staging = types.opt types.bool false;
};
nixos =
{ host, settings, ... }:
{
time.timeZone = yen.helpers.hostTz host;
security.acme.defaults = {
inherit (settings) email;
server = if settings.staging then "staging" else "prod";
};
};
};
# Fleet default (rank 10 sysctl baseline + aspect-level ACME email).
yen.aspects.prod-defaults = {
at = yen.kinds.host;
set = [ (yen.aspects.acme { email = "ops@example.com"; }) ];
sysctl = {
rank = 10;
set."vm.swappiness" = 60;
};
};
# Host-specific override wins cleanly over fleet defaults.
yen.host.eu.use = [
(yen.aspects.acme {
email = "eu@example.com";
staging = true;
})
];
yen.aspects.eu-tuning = {
at = yen.host.eu;
sysctl = {
rank = 50;
set."vm.swappiness" = 10;
};
};
}

Here api inherits "UTC" from yen.env.prod, "ops@example.com" from set, and vm.swappiness = 60 from rank 10. eu overrides all three cleanly — its own tz, host-level use settings, and a rank 50 sysctl layer:

nix eval .#nixos
{
api = {
boot = {
kernel = {
sysctl = {
"vm.swappiness" = 60;
};
};
};
security = {
acme = {
defaults = {
email = "ops@example.com";
server = "prod";
};
};
};
time = {
timeZone = "UTC";
};
};
eu = {
boot = {
kernel = {
sysctl = {
"vm.swappiness" = 10;
};
};
};
security = {
acme = {
defaults = {
email = "eu@example.com";
server = "staging";
};
};
};
time = {
timeZone = "Europe/Berlin";
};
};
}

Surfacing accidental conflicts immediately

Section titled “Surfacing accidental conflicts immediately”

If another file also sets yen.host.eu.tz = "America/Chicago";, nix eval --json .#yen.errors names both conflicting files:

nix eval --json .#yen.errors
[
{
code = "two-answers";
error = "conflicting declarations at `yen.host.eu.tz`";
}
]
  • Aspect settings: narrower bindings win cleanly (schema default < set < aspect use < host/user use), and equal-rank conflicts report settings-conflict.
  • Layered sinks (reduce.layers): higher numeric rank wins (50 beats 10), while two differing values at the same rank report layer-conflict.
  • Provenance: run nix eval --json .#yen.declaredIn at any time to see which file declared any field or aspect.

Next, target features declaratively across hosts in Targeting Features Without mkIf.

ContributeCommunitySponsor