Skip to content

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.

modules/sinks.nix
{
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.

modules/features.nix
{ 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:

nix eval .#endpoints + .#nixos + .#overlayCount
{
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:

nix eval --json .#yen.errors (on duplicate endpoint)
[
{
code = "key-conflict";
error = "duplicate key `api` in reduce.keyed";
}
]
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
  • into.<target>: turns a reduced sink into another sink on the same machine or on root (out). For example, std.modules.machines forwards os into both nixos and darwin.
  • intoParent.<target>: forwards a child’s sink up to its parent host (for example, embedding a user’s hm sink into the host’s home-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.

ContributeCommunitySponsor