Thomas Koch: Minimal overhead VMs with Nix and MicroVM
Posted on March 17, 2024
Joachim Breitner wrote about a Convenient sandboxed development environment and thus reminded me to blog about MicroVM. I ve toyed around with it a little but not yet seriously used it as I m currently not coding.
MicroVM is a nix based project to configure and run minimal VMs. It can mount and thus reuse the hosts nix store inside the VM and thus has a very small disk footprint. I use MicroVM on a debian system using the nix package manager.
The MicroVM author uses the project to host production services. Otherwise I consider it also a nice way to learn about NixOS after having started with the nix package manager and before making the big step to NixOS as my main system.
The guests root filesystem is a tmpdir, so one must explicitly define folders that should be mounted from the host and thus be persistent across VM reboots.
I defined the VM as a nix flake since this is how I started from the MicroVM projects example:
description = "Haskell dev MicroVM";
inputs.impermanence.url = "github:nix-community/impermanence";
inputs.microvm.url = "github:astro/microvm.nix";
inputs.microvm.inputs.nixpkgs.follows = "nixpkgs";
outputs = self, impermanence, microvm, nixpkgs :
let
persistencePath = "/persistent";
system = "x86_64-linux";
user = "thk";
vmname = "haskell";
nixosConfiguration = nixpkgs.lib.nixosSystem
inherit system;
modules = [
microvm.nixosModules.microvm
impermanence.nixosModules.impermanence
( pkgs, ... :
environment.persistence.$ persistencePath =
hideMounts = true;
users.$ user =
directories = [
"git" ".stack"
];
;
;
environment.sessionVariables =
TERM = "screen-256color";
;
environment.systemPackages = with pkgs; [
ghc
git
(haskell-language-server.override supportedGhcVersions = [ "94" ]; )
htop
stack
tmux
tree
vcsh
zsh
];
fileSystems.$ persistencePath .neededForBoot = nixpkgs.lib.mkForce true;
microvm =
forwardPorts = [
from = "host"; host.port = 2222; guest.port = 22;
from = "guest"; host.port = 5432; guest.port = 5432; # postgresql
];
hypervisor = "qemu";
interfaces = [
type = "user"; id = "usernet"; mac = "00:00:00:00:00:02";
];
mem = 4096;
shares = [
# use "virtiofs" for MicroVMs that are started by systemd
proto = "9p";
tag = "ro-store";
# a host's /nix/store will be picked up so that no
# squashfs/erofs will be built for it.
source = "/nix/store";
mountPoint = "/nix/.ro-store";
proto = "virtiofs";
tag = "persistent";
source = "~/.local/share/microvm/vms/$ vmname /persistent";
mountPoint = persistencePath;
socket = "/run/user/1000/microvm-$ vmname -persistent";
];
socket = "/run/user/1000/microvm-control.socket";
vcpu = 3;
volumes = [];
writableStoreOverlay = "/nix/.rwstore";
;
networking.hostName = vmname;
nix.enable = true;
nix.nixPath = ["nixpkgs=$ builtins.storePath <nixpkgs> "];
nix.settings =
extra-experimental-features = ["nix-command" "flakes"];
trusted-users = [user];
;
security.sudo =
enable = true;
wheelNeedsPassword = false;
;
services.getty.autologinUser = user;
services.openssh =
enable = true;
;
system.stateVersion = "24.11";
systemd.services.loadnixdb =
description = "import hosts nix database";
path = [pkgs.nix];
wantedBy = ["multi-user.target"];
requires = ["nix-daemon.service"];
script = "cat $ persistencePath /nix-store-db-dump nix-store --load-db";
;
time.timeZone = nixpkgs.lib.mkDefault "Europe/Berlin";
users.users.$ user =
extraGroups = [ "wheel" "video" ];
group = "user";
isNormalUser = true;
openssh.authorizedKeys.keys = [
"ssh-rsa REDACTED"
];
password = "";
;
users.users.root.password = "";
users.groups.user = ;
)
];
;
in
packages.$ system .default = nixosConfiguration.config.microvm.declaredRunner;
;
I start the microVM with a templated systemd user service:
[Unit]
Description=MicroVM for Haskell development
Requires=microvm-virtiofsd-persistent@.service
After=microvm-virtiofsd-persistent@.service
AssertFileNotEmpty=%h/.local/share/microvm/vms/%i/flake/flake.nix
[Service]
Type=forking
ExecStartPre=/usr/bin/sh -c "[ /nix/var/nix/db/db.sqlite -ot %h/.local/share/microvm/nix-store-db-dump ] nix-store --dump-db >%h/.local/share/microvm/nix-store-db-dump"
ExecStartPre=ln -f -t %h/.local/share/microvm/vms/%i/persistent/ %h/.local/share/microvm/nix-store-db-dump
ExecStartPre=-%h/.local/state/nix/profile/bin/tmux new -s microvm -d
ExecStart=%h/.local/state/nix/profile/bin/tmux new-window -t microvm: -n "%i" "exec %h/.local/state/nix/profile/bin/nix run --impure %h/.local/share/microvm/vms/%i/flake"
The above service definition creates a dump of the hosts nix store db so that it can be imported in the guest. This is necessary so that the guest can actually use what is available in /nix/store. There is an effort for an overlayed nix store that would be preferable to this hack.
Finally the microvm is started inside a tmux session named microvm . This way I can use the VM with SSH or through the console and also access the qemu console.
And for completeness the virtiofsd service:
[Unit]
Description=serve host persistent folder for dev VM
AssertPathIsDirectory=%h/.local/share/microvm/vms/%i/persistent
[Service]
ExecStart=%h/.local/state/nix/profile/bin/virtiofsd \
--socket-path=$ XDG_RUNTIME_DIR /microvm-%i-persistent \
--shared-dir=%h/.local/share/microvm/vms/%i/persistent \
--gid-map :995:%G:1: \
--uid-map :1000:%U:1:
Debusine is a Free Software project developed by Freexian to manage
scheduling and distribution of Debian-related tasks to a network of worker
machines. It was started some time back, but its development pace has
recently increased significantly thanks to funding from the
Warning to the Planet Debian readers: the following post might shock you, if you're used to Debian's smooth upgrades using only the package manager.
Leapp?!
Contrary to distributions like
De 27 a 30 de abril de 2024 foi realizada a
Esta foi a quinta vez que uma MiniDebConf (como um evento presencial exclusivo
sobre Debian) aconteceu no Brasil. As edi es anteriores foram em Curitiba
(
N meros da edi o 2024
Durante os quatro dias de evento aconteceram diversas atividades para todos os
n veis de usu rios(as) e colaboradores(as) do projeto Debian. A
Os n meros finais da MiniDebConf Belo Horizonte 2024 mostram que tivemos um
recorde de participantes.
Atividades
A programa o da MiniDebConf foi intensa e diversificada. Nos dias 27, 29 e 30
(s bado, segunda e ter a-feira) tivemos palestras, debates, oficinas e muitas
atividades pr ticas.
J no dia 28 (domingo), ocorreu o Day Trip, um dia dedicado a passeios pela
cidade. Pela manh sa mos do hotel e fomos, em um nibus fretado, para o
Depois de 2 horas de passeio pelo Mercado, voltamos para o nibus e pegamos a
estrada para almo armos em um restaurante de comida t pica mineira.
Com todos bem alimentados, voltamos para Belo Horizonte para visitarmos o
principal ponto tur stico da cidade: a Lagoa da Pampulha e a Capela S o
Francisco de Assis, mais conhecida como
Voltamos para o hotel e o dia terminou no hacker space que montamos na sala
de eventos para o pessoal conversar, empacotar, e comer umas pizzas.
Financiamento coletivo
Pela terceira vez fizemos uma campanha de financiamento coletivo e foi incr vel
como as pessoas contribu ram! A meta inicial era arrecadar o valor equivalente
a uma cota ouro de R$ 3.000,00. Ao atingirmos essa meta, definimos uma nova,
equivalente a uma cota ouro + uma cota prata (R$ 5.000,00). E novamente
atingimos essa meta. Ent o propusermos como meta final o valor de uma cota
ouro + prata + bronze, que seria equivalente a R$ 6.000,00. O resultado foi que
arrecadamos R$ 7.239,65 com a ajuda de mais de 100 pessoas!
Muito obrigado as pessoas que contribu ram com qualquer valor. Como forma de
agradecimento,
Bolsas de alimenta o, hospedagem e/ou passagens para participantes
Cada edi o da MiniDebConf trouxe alguma inova o, ou algum benef cio diferente
para os(a) participantes. Na edi o deste ano em Belo Horizonte, assim como
acontece nas DebConfs,
Fotos e v deos
Voc pode assistir as grava es das palestras nos links abaixo:
Patrocinadores
Ouro:
A new bug fix release 0.1.3 of
Like each month, have a look at the work funded by
If you wander around a lot of open source repositories on the likes of GitHub, you ll invariably stumble over repos that have an issue (or more than one!) with a title like the above.
Sometimes sitting open and unloved, often with a comment or two from the maintainer and a bunch of I ll help out! followups that never seemed to pan out.
Very rarely, you ll find one that has been closed, with a happy ending.
These issues always fascinate me, because they say a lot about what it means to maintain an open source project, the nature of succession (particularly in a









