MicroVMs, Containers & Peer Meshes
Two common multi-host patterns stay concise and cycle-free in Yen:
- Guests inside hosts:
std.modules.allspawns everyguestunder its parent host (on = yen.host.box1), andstd.lib.microvmGuestforwards guestnixosmodules straight intomicrovm.vms.<guest>.config. - Peer meshes:
tree.selectreads every host’s typed address directly without touchingnixosConfigurations, avoiding cyclic imports.
{ yen, std, ...}:{ imports = [ std.modules.all ];
yen.host.box1.addr = "198.51.100.10"; yen.host.box2.addr = "198.51.100.20";
# Guests declare which host runs them (`on = yen.host.box1`); `std` spawns them underneath. yen.guest.api = { on = yen.host.box1; addr = "198.51.100.51"; };
yen.guest.pg = { on = yen.host.box1; addr = "198.51.100.52"; };
# Embed every guest's `nixos` modules into its parent host's `microvm.vms.<guest>`. yen.kinds.guest.sinks.nixos.intoParent.nixos = std.lib.microvmGuest { from = yen.kinds.guest.sinks.nixos; };}{ yen, ... }:let b = builtins; allHosts = { instances = yen.kinds.host; };in{ # Guest features look just like normal NixOS aspects. yen.aspects.api-guest = { at = yen.guest.api; nixos.services.nginx.enable = true; };
yen.aspects.pg-guest = { at = yen.guest.pg; nixos.services.postgresql.enable = true; };
# Full-mesh peers: every host reads every OTHER host's IP without `nixosConfigurations` cycles. yen.aspects.wg-mesh = { at = yen.kinds.host; cycle = { host, tree, ... }: { tree = [ allHosts ]; nixos.networking.wireguard.interfaces.wg0.peers = map (p: { endpoint = "${p.addr}:51820"; }) (b.filter (h: !(yen.lib.same h host)) (tree.select allHosts)); }; };}Notice box1 automatically embeds both MicroVMs AND pairs up with box2 on WireGuard while excluding itself using yen.lib.same:
{ box1 = { microvm = { vms = { api = { config = { services = { nginx = { enable = true; }; }; }; }; pg = { config = { services = { postgresql = { enable = true; }; }; }; }; }; }; networking = { wireguard = { interfaces = { wg0 = { peers = [ { endpoint = "198.51.100.20:51820"; } ]; }; }; }; }; }; box2 = { networking = { wireguard = { interfaces = { wg0 = { peers = [ { endpoint = "198.51.100.10:51820"; } ]; }; }; }; }; };}Next, generate flake.nix inputs, GitHub Actions CI matrices, and fleet docs straight from your live fleet in Self-Generating flake.nix, CI & Fleet Docs.