KVM / libvirt
Idealistically, the introduction declared a “No Sworkstations” rule (No Server-Workstations). Pragmatically, you can bend this rule a bit, by hosting some development servers inside of virtual machines (VM). Hosting VMs on your workstation is convenient for having a portable lab environment. By using virtual machines for all services, we get to maintain our core distinction between the roles of workstation and server.
This paradigm is considerably more adhoc than a proper hypervisor operating system like Proxmox. For pure server installs, Proxmox should be preferred. But if you want to have a mixed-mode native workstation, with extra server VMs, in the same portable platform, this setup works really well.
nixos-vm-template
nixos-vm-template is a tool for building and managing NixOS virtual
machines with an immutable (read-only) root filesystem design. If you
installed Home Manager earlier, the vm alias is already
configured for you.
Read these extra pages about nixos-vm-template:
- https://github.com/EnigmaCurry/nixos-vm-template
- https://blog.rymcg.tech/blog/linux/code-agent-vm/
- https://blog.rymcg.tech/blog/linux/nixos-proxmox-vm/
The vm alias
The vm alias is configured via Home Manager and points to the
nixos-vm-template Justfile. Configuration is stored in
~/.config/nixos-vm-template/env, which sets writable paths for VM
output and machine configs.
The vm alias has full tab completion for all commands, VM names,
profiles, and other arguments. Press Tab to see available options at
any point.
To see all available commands:
vm
Available VM profiles
List the available VM profiles (base images):
vm list-profiles
Creating a VM
Create a new VM with a given name and profile:
vm create myvm dev
This will:
- Build the profile’s base image (if not already built)
- Create machine identity files (hostname, SSH keys, etc.)
- Prompt for SSH public keys for admin and user accounts
- Create the boot and /var disks
- Define the VM in libvirt
Starting and stopping VMs
vm start myvm
vm stop myvm
vm force-stop myvm
Connecting to a VM
SSH into the VM (defaults to the user account):
vm ssh myvm
vm ssh admin@myvm
Open the VM console:
vm console myvm
Listing VMs
List all defined VMs:
vm list
List all machine configs:
vm list-machines
Managing VMs
Destroy a VM (removes disks but keeps machine config):
vm destroy myvm
Completely remove a VM including its machine config:
vm purge myvm
Recreate a VM from its existing machine config:
vm recreate myvm
Upgrade a VM to a new base image (preserves /var data):
vm upgrade myvm
Snapshots and backups
Create a snapshot:
vm snapshot myvm snapshot-name
List snapshots:
vm snapshots myvm
Restore a snapshot:
vm restore-snapshot myvm snapshot-name
Backup a VM:
vm backup myvm
List backups:
vm backups
Restore from backup:
vm restore-backup myvm