Organizing Hosts, Users & Sub-Resources
When multiple servers share operators or carry per-host disks, declaring membership and sub-resources directly on your data keeps every host concise.
Yen models your fleet as a tree of active placements (called scopes), such as host:web/operator:alice or host:web/disk:data.
Parent kinds attach children with spawns, and any child feature automatically emits into its parent host’s nixos config.
{ yen, std, at, types, ...}:{ imports = [ std.modules.all ];
yen.kinds.env = { }; yen.env.prod = { }; yen.env.dev = { };
# Operators declare which hosts they belong to instead of editing every host file. yen.kinds.operator.fields = { hosts = types.opt types.selector at.none; groups = types.opt (types.listOf types.str) [ "wheel" ]; };
yen.kinds.host = { fields.env = types.opt yen.kinds.env yen.env.dev; # Attach every operator whose `hosts` selector matches this host. spawns.operator = yen.lib.matching yen.kinds.operator.fields.hosts yen.operator; # Nested kind: each `disk.<name>` declared on a host lives under that host. kinds.disk.fields = { device = types.str; fsType = types.opt types.str "ext4"; }; };}Now declare two hosts and let Alice target every production host with a single selector.
Add a nested disk.data right inside yen.host.web.
{ yen, ... }:{ yen.host.web = { env = yen.env.prod; disk.data = { device = "/dev/disk/by-label/data"; fsType = "xfs"; }; }; yen.host.lab.env = yen.env.dev;
# Alice gets an account on every production host automatically. yen.operator.alice.hosts = yen.kinds.host.where { env = yen.env.prod; };
yen.aspects.mount-disk = { at = yen.kinds.host.disk; nixos = { disk, ... }: { fileSystems."/mnt/${disk.name}" = { inherit (disk) device fsType; }; }; };
yen.aspects.define-operator = { at = yen.kinds.operator; nixos = { operator, ... }: { users.users.${operator.name} = { isNormalUser = true; extraGroups = operator.groups; }; }; };}Inspect the generated NixOS config for web:
nix eval .#yen.oknix eval .#nixos.web{ fileSystems = { "/mnt/data" = { device = "/dev/disk/by-label/data"; fsType = "xfs"; }; }; users = { users = { alice = { extraGroups = [ "wheel" ]; isNormalUser = true; }; }; };}Three ways to attach children (spawns)
Section titled “Three ways to attach children (spawns)”- List on the parent (default in
std.modules.machines):yen.host.laptop.users = [ yen.user.alice ];. - Selector on the child (
yen.lib.matching): shown above —alicejoins everyprodhost automatically when you add new servers. - Back-pointer on the child (
yen.lib.pointing): used byguestand standalonehome(yen.guest.api.on = yen.host.box;) so each guest or home names the host it runs on. - Nested kinds (
kinds.disk): scoped strictly to their parent host (yen.host.web.disk.data), ideal for disks, network interfaces, or host-local services.
Next, see how Yen handles defaults and layered overrides cleanly in Explicit Defaults & Layered Overrides.