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:

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:

[bash]: Run this on your workstation:
vm

Available VM profiles

List the available VM profiles (base images):

[bash]: Run this on your workstation:
vm list-profiles

Creating a VM

Create a new VM with a given name and profile:

[bash]: Run this on your workstation:
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

[bash]: Run this on your workstation:
vm start myvm
vm stop myvm
vm force-stop myvm

Connecting to a VM

SSH into the VM (defaults to the user account):

[bash]: Run this on your workstation:
vm ssh myvm
vm ssh admin@myvm

Open the VM console:

[bash]: Run this on your workstation:
vm console myvm

Listing VMs

List all defined VMs:

[bash]: Run this on your workstation:
vm list

List all machine configs:

[bash]: Run this on your workstation:
vm list-machines

Managing VMs

Destroy a VM (removes disks but keeps machine config):

[bash]: Run this on your workstation:
vm destroy myvm

Completely remove a VM including its machine config:

[bash]: Run this on your workstation:
vm purge myvm

Recreate a VM from its existing machine config:

[bash]: Run this on your workstation:
vm recreate myvm

Upgrade a VM to a new base image (preserves /var data):

[bash]: Run this on your workstation:
vm upgrade myvm

Snapshots and backups

Create a snapshot:

[bash]: Run this on your workstation:
vm snapshot myvm snapshot-name

List snapshots:

[bash]: Run this on your workstation:
vm snapshots myvm

Restore a snapshot:

[bash]: Run this on your workstation:
vm restore-snapshot myvm snapshot-name

Backup a VM:

[bash]: Run this on your workstation:
vm backup myvm

List backups:

[bash]: Run this on your workstation:
vm backups

Restore from backup:

[bash]: Run this on your workstation:
vm restore-backup myvm