Skip to content

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:

  1. Inspecting the fleet or reduced sinks: for example, scanning every host with form = yen.form.iso to export installer images automatically, or reading aiMcp to render tool configs.
  2. Wrapping upstream modules without editing them: taking a regular nixos module 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.

modules/fleet.nix
{
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-isos asks tree for every iso host and generates out.releaseImages automatically — add a new ISO host anywhere and its release package appears with zero edits to flake.nix.
  • yen.kinds.guest.intercept takes gitea-app (written as a normal nixos + firewall feature) and redirects its nixos config into nas’s containers.gitea while shifting its TCP port 3000 to 13000.
modules/middleware.nix
{
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:

nix eval .#releaseImages + .#containers + .#guestPorts
{
containers = {
nas = {
gitea = {
services = {
gitea = {
enable = true;
};
};
};
};
};
guestPorts = {
gitea = [ 13000 ];
};
releaseImages = {
installer-iso = "isoImage(installer)";
};
}

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.

ContributeCommunitySponsor