Skip to content

Targeting Features Without mkIf

In Yen, each feature is an aspect that declares where it applies using at. Adding a database link (app1.db = yen.host.db1) lets both the server feature and client feature activate automatically from one shared fact.

modules/fleet.nix
{
yen,
std,
types,
...
}:
{
imports = [ std.modules.all ];
yen.kinds.cluster.fields.region = types.str;
yen.kinds.host.fields = {
cluster = types.maybe yen.kinds.cluster;
db = types.maybe yen.kinds.host;
};
yen.cluster.eu.region = "eu-central";
yen.host.db1 = {
cluster = yen.cluster.eu;
addr = "198.51.100.10";
};
yen.host.app1 = {
cluster = yen.cluster.eu;
addr = "198.51.100.11";
db = yen.host.db1;
};
# Opt `app1` out of automatic telemetry without editing the telemetry feature.
yen.host.app1.drop = [ yen.aspects.telemetry ];
}

Notice three things in aspects.nix:

  • postgres-server uses at.targetOf so pointing app1.db = yen.host.db1 turns on PostgreSQL and pulls in borg-backup via use automatically.
  • eu-db-client uses at.via to match any host whose db belongs to yen.cluster.eu.
  • app1 opts out of telemetry cleanly with drop = [ yen.aspects.telemetry ] — no boolean flag needed.
modules/aspects.nix
{
yen,
at,
...
}:
{
yen.aspects.borg-backup = {
nixos =
{ host, ... }:
{
services.borgbackup.jobs.${host.name}.enable = true;
};
};
# Runs on every host unless a host opts out via `drop`.
yen.aspects.telemetry = {
at = yen.kinds.host;
nixos.services.prometheus.exporters.node.enable = true;
};
# Reverse link: runs on any host referenced by another host's `db` field.
yen.aspects.postgres-server = {
at = at.targetOf yen.kinds.host.fields.db at.any;
nixos.services.postgresql.enable = true;
use = [ yen.aspects.borg-backup ];
};
# Forward link: runs on any host whose `db` lives in cluster `eu`.
yen.aspects.eu-db-client = {
at = at.via yen.kinds.host.fields.db (yen.kinds.host.where { cluster = yen.cluster.eu; });
nixos =
{ host, ... }:
{
environment.variables.DATABASE_HOST = host.db.addr;
};
};
}

Check what each host received:

nix eval .#nixos
{
app1 = {
environment = {
variables = {
DATABASE_HOST = "198.51.100.10";
};
};
};
db1 = {
services = {
borgbackup = {
jobs = {
db1 = {
enable = true;
};
};
};
postgresql = {
enable = true;
};
prometheus = {
exporters = {
node = {
enable = true;
};
};
};
};
};
}

Every at selector runs inside Yen’s Tupu-inspired Datalog solver, so relationships work in both directions and compose cleanly.

Selector When it matches
yen.kinds.host / yen.host.app1 Every host (including sub-kinds), or one specific host
yen.kinds.host.where { workstation = true; } Schema-checked field match (at.neq, at.gt, at.oneOf, at.contains, at.exists, at.nonEmpty, at.matches)
at.all [ ... ] / at.anyOf [ ... ] / at.not S Logical AND, OR, and NOT
at.parent S / at.under S / at.nearest K S Match based on the enclosing host or ancestor
at.has S / at.child S / at.sibling S Match based on users, guests, or sibling placements
at.via fieldRef S Follow a reference field (host.db) and match the target
at.targetOf fieldRef S Reverse link: match any host pointed to by fieldRef
at.rule ({ S, ... }: ...) Multi-hop Datalog rules (see Derived Topology & Multi-Hop Rules)
  • use = [ yen.aspects.foo ]: pull in another feature automatically (deduplicated even when two branches pull in the same feature).
  • on.user = { ... }: emit config onto child users from a host-level feature.
  • drop = [ yen.aspects.foo ] / drop.subtree = [ ... ]: opt a host (or an entire host + its users) out of a feature without editing upstream code.

Next, see how Yen collects and merges outputs without silent collisions in Custom Buckets & Merge Rules (Sinks).

ContributeCommunitySponsor