Reactive Fleet Patterns & Middleware (cycle)
Most features only need plain aspects (nixos = ...; hm = ...;).
You reach for cycle when a feature needs one of two things plain modules cannot express cleanly:
- Inspecting the fleet or reduced sinks: for example, scanning every host with
form = yen.form.isoto export installer images automatically, or readingaiMcpto render tool configs. - Wrapping upstream modules without editing them: taking a regular
nixosmodule and redirecting it into a container/MicroVM, shifting its ports for staging, or filtering out unwanted emissions.
This pattern comes from Cycle.js: a cycle function reads inputs (tree and reduced sinks) and returns sink outputs, and Yen solves dependencies lazily in one pass.
{ yen, std, types, reduce, ...}:{ imports = [ std.modules.all ];
yen.kinds.form = { }; yen.form = { server = { }; iso = { }; };
yen.kinds.host = { fields = { form = types.opt yen.kinds.form yen.form.server; jumpVia = types.maybe yen.kinds.host; }; sinks.containers = { reduce = reduce.merge; into.out = { host, containers, ... }: if containers == { } then { } else { containers.${host.name} = containers; }; }; };
yen.host.nas.addr = "198.51.100.10"; yen.host.installer.form = yen.form.iso;
# A guest container running inside `nas`. yen.guest.gitea = { on = yen.host.nas; addr = "198.51.100.50"; };
# Standard host-style feature: writes straight to `nixos` and `firewall`. yen.aspects.gitea-app = { at = yen.guest.gitea; nixos.services.gitea.enable = true; firewall.tcp = [ 3000 ]; };}Notice two practical patterns in middleware.nix:
export-isosaskstreefor everyisohost and generatesout.releaseImagesautomatically — add a new ISO host anywhere and its release package appears with zero edits toflake.nix.yen.kinds.guest.intercepttakesgitea-app(written as a normalnixos+firewallfeature) and redirects itsnixosconfig intonas’scontainers.giteawhile shifting its TCP port3000to13000.
{ yen, cycle, ...}:let b = builtins; allHosts = { instances = yen.kinds.host; };in{ # 1. Whole-fleet discovery: automatically export an ISO package for every `form = iso` host. yen.aspects.export-isos = { at = yen.root; cycle = { tree, ... }: { tree = [ allHosts ]; out.releaseImages = b.listToAttrs ( map (h: { name = "${h.name}-iso"; value = "isoImage(${h.name})"; }) (b.filter (h: yen.lib.same h.form yen.form.iso) (tree.select allHosts)) ); }; };
# 2. Reuse a normal `nixos` aspect inside a guest container without editing `gitea-app`: # redirect `guest.nixos` -> `host.containers.<guest>` and shift container ports by +10000. yen.kinds.guest.intercept = cycle.compose [ (cycle.redirect yen.kinds.guest.sinks.nixos yen.kinds.host.sinks.containers ( { guest, ... }: cfg: { ${guest.name} = cfg; } )) (cycle.isolateSink yen.kinds.guest.sinks.firewall ( _: rule: rule // { tcp = map (p: p + 10000) (rule.tcp or [ ]); } )) ];}Inspect the generated release image, container config, and shifted port:
{ containers = { nas = { gitea = { services = { gitea = { enable = true; }; }; }; }; }; guestPorts = { gitea = [ 13000 ]; }; releaseImages = { installer-iso = "isoImage(installer)"; };}Built-in middleware helpers (yen.cycle.*)
Section titled “Built-in middleware helpers (yen.cycle.*)”Attach middleware to any kind with yen.kinds.<k>.intercept (runs on every placement of that kind) or to a specific aspect with yen.aspects.<a>.wrap.
| Helper | What it does |
|---|---|
cycle.redirect fromSink toSink fn |
Moves emissions from one sink to another (e.g. nixos into containers.<guest>) |
cycle.isolateSink sinkRef fn |
Transforms every value emitted into a sink (e.g. shifting ports or prefixing paths) |
cycle.filterSink sinkRef pred |
Drops emissions that fail a predicate |
cycle.isolateSource sinkRef fn |
Transforms an incoming sink value before a cycle component reads it |
cycle.wall [ sinkRefs ] |
Blocks upward emissions except the listed sinks |
cycle.compose [ m1 m2 ] |
Chains multiple middlewares outer-to-inner |
Next, enforce whole-fleet rules and export portable proof certificates in Whole-Fleet Rules & Lean 4 Proofs.