···1+# MicroVM in Den examples
2+3+There are two ways to run VMs:
4+5+## NixOS configuration MicroVM runner as package.
6+7+- Den example: [runnable-example.nix](./modules/runnable-example.nix)
8+- Den support: [microvm-runners.nix](./modules/microvm-runners.nix)
9+10+```console
11+nix run .#runnable-microvm
12+```
13+14+See https://microvm-nix.github.io/microvm.nix/packages.html
15+16+## MicroVM guests as part of a Host.
17+18+- Den example: [guests-example.nix](./modules/guests-example.nix)
19+- Den support: [microvm-integration.nix](./modules/microvm-integration.nix)
20+21+```console
22+nixos-rebuild build --flake .#server
23+```
24+25+See https://microvm-nix.github.io/microvm.nix/host.html\
26+https://microvm-nix.github.io/microvm.nix/declarative.html
···1+{ den, inputs, ... }:
2+{
3+ imports = [ inputs.den.flakeModule ];
4+5+ #
6+ # There are two ways to run VMS:
7+ #
8+ # - NixOS configuration MicroVM runner as package.
9+ # Den example: ./runnable-example.nix (by ./microvm-runners.nix)
10+ # See https://microvm-nix.github.io/microvm.nix/packages.html
11+ #
12+ #
13+ # - MicroVM guests as part of a Host.
14+ # Den example: ./guests-example.nix (by ./microvm-integration.nix)
15+ # See https://microvm-nix.github.io/microvm.nix/host.html
16+ # https://microvm-nix.github.io/microvm.nix/declarative.html
17+ #
18+19+ # automatically set hostname on all hosts.
20+ den.ctx.host.includes = [ den._.hostname ];
21+}
+38
templates/microvm/modules/guests-example.nix
···00000000000000000000000000000000000000
···1+{ den, lib, ... }:
2+{
3+4+ den.hosts.x86_64-linux.server.microvm.guests = [
5+ den.hosts.x86_64-linux.guest-microvm
6+ ];
7+8+ den.hosts.x86_64-linux.guest-microvm = {
9+ intoAttr = [ ]; # dont produce Guest nixosConfiguration at flake output
10+ };
11+12+ den.aspects.no-boot.nixos = {
13+ boot.loader.grub.enable = false;
14+ fileSystems."/".device = "/dev/null";
15+ };
16+17+ den.aspects.server = {
18+ # USER TODO: remove this on real bootable server
19+ includes = [ den.aspects.no-boot ];
20+21+ # NOTE: no microvm class exist for Host, only for Guests
22+ nixos.microvm.host.startupTimeout = 300;
23+ };
24+25+ den.aspects.guest-microvm = {
26+ # resolved with: `(den.ctx.host = { host = guest-microvm }).resolve { class = "nixos" }`
27+ # resulting nixos-module is set at server: microvm.vms.<name>.config
28+ nixos =
29+ { pkgs, ... }:
30+ {
31+ environment.systemPackages = [ pkgs.cowsay ];
32+ };
33+34+ # microvm class is for Guests!, forwarded into server: nixos.microvm.vms.<name>
35+ microvm.autostart = true;
36+ };
37+38+}
···1+# Copied from MicroVM flake template.
2+#
3+# This is just a normal NixOS configuration that happes to include microvm.nix module.
4+# No special Den classes nor context pipeline here. It's all just a single NixOS conf.
5+# Den support: ./microvm-runners.nix
6+#
7+{
8+ inputs,
9+ den,
10+ lib,
11+ ...
12+}:
13+{
14+15+ den.hosts.x86_64-linux.runnable-microvm = {
16+ intoAttr = [
17+ "microvms"
18+ "runnable-microvm"
19+ ]; # example not intended to be used from nixosConfigurations
20+ };
21+22+ den.aspects.runnable-microvm = {
23+ nixos = {
24+ imports = [ inputs.microvm.nixosModules.microvm ];
25+ users.users.root.password = "";
26+27+ # There's not much need to have a forwarding microvm class for runnable vms
28+ microvm = {
29+ volumes = [
30+ {
31+ mountPoint = "/var";
32+ image = "var.img";
33+ size = 256;
34+ }
35+ ];
36+ shares = [
37+ {
38+ # use proto = "virtiofs" for MicroVMs that are started by systemd
39+ proto = "9p";
40+ tag = "ro-store";
41+ # a host's /nix/store will be picked up so that no
42+ # squashfs/erofs will be built for it.
43+ source = "/nix/store";
44+ mountPoint = "/nix/.ro-store";
45+ }
46+ ];
47+48+ # "qemu" has 9p built-in!
49+ hypervisor = "qemu";
50+ socket = "control.socket";
51+ };
52+ };
53+ };
54+}