Custom Buckets & Merge Rules (Sinks)
Every output bucket in Yen — called a sink — declares an explicit combine rule (reduce).
reduce.modules deduplicates diamond imports cleanly via provenance keys, reduce.keyed catches duplicate names immediately, and std.modules.all ships nixos, darwin, hm, firewall, unfree, persist, secrets, and services ready to use.
{ yen, std, reduce, ...}:let b = builtins;in{ imports = [ std.modules.all ];
# Add two custom typed sinks onto standard `host`. yen.kinds.host.sinks = { # Keyed registry: two services claiming the same name fail closed (`key-conflict`). endpoints = { reduce = reduce.keyed (e: e.name); into.out = { host, endpoints, ... }: if endpoints == { } then { } else { endpoints.${host.name} = endpoints; }; }; # Raw sink: functions are stored untouched (just like Nixpkgs overlays). overlays = { raw = true; reduce = reduce.list; into.out = { host, overlays, ... }: if overlays == [ ] then { } else { overlayCount.${host.name} = b.length overlays; }; }; };
yen.host.web.addr = "198.51.100.20";}Here api and metrics both register distinct endpoints and both pull in shared-search (a diamond dependency).
reduce.modules tags each module with its provenance key so evalModules only includes corp.internal once, and raw = true keeps the overlay function untouched.
{ yen, ... }:{ yen.aspects.api = { at = yen.host.web; endpoints.api.port = 8080; # Diamond test: both `api` and `metrics` pull in `shared-search`. use = [ yen.aspects.shared-search ]; };
yen.aspects.metrics = { at = yen.host.web; endpoints.metrics.port = 9100; use = [ yen.aspects.shared-search ]; };
yen.aspects.shared-search = { nixos.networking.search = [ "corp.internal" ]; overlays = _final: _prev: { customTool = true; }; };}Inspect the collected sinks:
{ endpoints = { web = { api = { port = 8080; }; metrics = { port = 9100; }; }; }; nixos = { web = { networking = { search = [ "corp.internal" ]; }; }; }; overlayCount = { web = 1; };}If a second aspect also tries to register endpoints.api, reduce.keyed refuses the fleet immediately with key-conflict:
[ { code = "key-conflict"; error = "duplicate key `api` in reduce.keyed"; }]Choosing the right reducer
Section titled “Choosing the right reducer”| Reducer | Behavior | Great for |
|---|---|---|
reduce.modules |
Wraps each module with a provenance key so diamond imports deduplicate cleanly |
nixos, darwin, hm |
reduce.keyed keyFn |
Merges entries by key; errors (key-conflict) on collisions |
Services, secrets, ports, CI runners |
reduce.layers |
Merges { rank; set; } records; higher rank wins, equal rank errors |
Explicit layered defaults and overrides |
reduce.merge |
Deep-merges attrsets; errors (merge-conflict) on leaf clashes |
Structured JSON/YAML outputs |
reduce.set / reduce.list / reduce.flat |
Deduplicated list, ordered list, or concatenated lists | Firewall ports, unfree names, paths |
reduce.one / reduce.maybe / reduce.last |
Exactly 1 item, 0..1 items, or last item in deterministic order | Single owner, osConfig, primary value |
reduce.elements |
Preserves full { from; aspect; origin; value; } provenance records |
Audit tables and generated docs |
Forwarding sinks (into and intoParent)
Section titled “Forwarding sinks (into and intoParent)”into.<target>: turns a reduced sink into another sink on the same machine or onroot(out). For example,std.modules.machinesforwardsosinto bothnixosanddarwin.intoParent.<target>: forwards a child’s sink up to its parent host (for example, embedding a user’shmsink into the host’shome-manager.users.<name>).yen.lib.many [ a b ]: emit multiple items from one aspect;yen.lib.lit fn: emit a raw function without Yen pre-filling its arguments.
Next, see how users request host services upward while standalone homes stay isolated in Host ↔ User Wiring & Walled Homes.