Skip to content

Guests inside hosts

A guest is a machine that runs inside a host: a NixOS container here, a microvm in the reference fleet. It is a scope under its host, with its own nixos sink. That sink forwards its modules up, into the host.

modules/kinds.nix
# A guest lives under its host. Its own `nixos` sink forwards up, into the host's containers.
{
yen,
lib,
inputs,
...
}:
{
yen.kinds.fleet = {
spawns.host = _: yen.host;
sinks.out.reduce = yen.lib.reduce.merge;
};
yen.kinds.host = {
spawns.guest = s: lib.filterAttrs (_: g: yen.lib.same g.on s) yen.guest;
sinks.nixos.out =
{ host, nixos, ... }:
{
nixosConfigurations.${host.name} = inputs.nixpkgs.lib.nixosSystem { modules = nixos; };
};
};
yen.kinds.guest = {
fields.on = yen.kinds.host;
sinks.nixos.nixos =
{ guest, nixos, ... }:
{
containers.${guest.name} = {
autoStart = true;
config.imports = nixos;
};
};
};
yen.fleet.main = { };
yen.flake = { out, ... }: out;
}
modules/machines.nix
{ yen, ... }:
{
yen.host.box = { };
yen.guest.web.on = yen.host.box;
yen.guest.db.on = yen.host.box;
yen.aspects.box = {
at = yen.host.box;
nixos = {
boot.loader.grub.device = "nodev";
fileSystems."/" = {
device = "/dev/sda1";
fsType = "ext4";
};
nixpkgs.hostPlatform = "x86_64-linux";
system.stateVersion = "25.05";
};
};
}
modules/services.nix
# Aspects at guests. None of this reaches the host's own system: an element goes to its nearest `nixos` sink.
{ yen, ... }:
{
yen.aspects.guest-base = {
at = yen.kinds.guest;
nixos =
{ guest, ... }:
{
networking.hostName = guest.name;
system.stateVersion = "25.05";
};
};
yen.aspects.nginx = {
at = yen.guest.web;
nixos.services.nginx.enable = true;
};
yen.aspects.postgres = {
at = yen.guest.db;
nixos.services.postgresql.enable = true;
};
}
flowchart BT
  n6c4c773b6d["fleet main"]
  ne5e52c6869["host box"]
  n6ce2a3fe4d["guest db"]
  n0e5c840457["guest web"]
  ne5e52c6869 -->|out| n6c4c773b6d
  n6ce2a3fe4d -->|nixos| ne5e52c6869
  n0e5c840457 -->|nixos| ne5e52c6869

nginx runs in the web container. The host box does not get it: the element went to web’s own sink.

Terminal window
sudo nixos-rebuild switch --flake .#box # builds box and both containers
Contribute Community Sponsor