Skip to content

MicroVMs, Containers & Peer Meshes

Two common multi-host patterns stay concise and cycle-free in Yen:

  1. Guests inside hosts: std.modules.all spawns every guest under its parent host (on = yen.host.box1), and std.lib.microvmGuest forwards guest nixos modules straight into microvm.vms.<guest>.config.
  2. Peer meshes: tree.select reads every host’s typed address directly without touching nixosConfigurations, avoiding cyclic imports.
modules/fleet.nix
{
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;
};
}
modules/mesh.nix
{ 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:

nix eval .#nixos
{
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.

ContributeCommunitySponsor