Skip to content

Instantly share code, notes, and snippets.

@jhillyerd
Last active August 29, 2023 03:15
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jhillyerd/38dd7fe1492b713c64686055c910dfa7 to your computer and use it in GitHub Desktop.
Save jhillyerd/38dd7fe1492b713c64686055c910dfa7 to your computer and use it in GitHub Desktop.
Getting started with nixops and libvirtd

Enable libvirtd

Add:

virtualisation.libvirtd.enable = true;

and

users.users.<you>.extraGroups = [ "libvirtd" ];

to your /etc/nixos/configuration.nix.

Then run sudo nixos-rebuild boot and reboot your machine.

Starting state

A fresh install of libvirtd on NixOS will have a default storage pool for users in the libvirtd group:

$ virsh pool-dumpxml default

<pool type='dir'>
  <name>default</name>
  ...
  <target>
    <path>/var/lib/libvirt/images</path>
  </target>
</pool>

But no default pool for root:

$ sudo virsh pool-dumpxml default

error: failed to get pool 'default'
error: Storage pool not found: no storage pool with matching name 'default'

Additionally the images directory referenced above will not exist.

If your system is in a different state than this, you may have leftover configuration from a previous libvirtd install. You can run the following commands to reset it. This will destroy any machines you had defined!

sudo rm -rf /var/lib/libvirt
sudo systemctl libvirtd restart

Create the images directory

Create an images directory modifiable by the libvirtd group (bash syntax):

images=/var/lib/libvirt/images
sudo mkdir $images
sudo chown root:libvirtd $images
sudo chmod g+w $images

Setup the storage pool

Create a default pool for root:

sudo virsh pool-define-as default dir --target $images
sudo virsh pool-autostart default
sudo virsh pool-start default

Confirm you have an active default pool:

sudo virsh pool-list

Build the test network

cd nixops-libvirtd
nix-shell
poetry install
poetry shell
nixops create -d test tests/functional/single_machine_libvirtd_base.nix
nixops deploy -d test

Confirm you can connect to the VM

nixops ssh -d test machine
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment