Full commands to set up nixos on libvirt
Download the latest x86-64 image:
cd /path/to/isos
# Adjust URL to the latest version (see https://nixos.org/download/):
wget 'https://channels.nixos.org/nixos-25.05/latest-nixos-minimal-x86_64-linux.iso'
# Make sure libvirt has permission to read the iso
sudo chown libvirt-qemu:kvm latest-nixos-minimal-x86_64-linux.iso
File-based disk image (qcow2)
# Set the name of the VM:
VMNAME=nixos
# If you don't use ZFS, you can create a qcow2 (less performant) disk image file with:
sudo qemu-img create -f qcow2 /path/to/vms/$VMNAME.qcow2 20G
sudo chown libvirt-qemu:kvm /path/to/vms/$VMNAME.qcow2
# To create the VM using qcow2 as the disk:
virt-install \
--name $VMNAME \
--ram 2048 \
--vcpus 2 \
--boot=uefi \
--disk path=/path/to/vms/$VMNAME.qcow2,format=qcow2 \
--os-variant generic \
--network network=default,model=virtio \
--graphics none \
--console pty,target_type=virtio \
--cdrom /path/to/isos/latest-nixos-minimal-x86_64-linux.iso
Disk based on ZFS:
# Set the name of the VM:
VMNAME=nixos
# If using ZFS, you can create a thin-provisioned ZVOL with:
sudo zfs create -s -V 20G tank/vms/$VMNAME
# To create the VM using the ZVOL (when using ZFS) as the disk:
virt-install \
--name $VMNAME \
--ram 2048 \
--vcpus 2 \
--boot=uefi \
--disk path=/dev/zvol/tank/vms/$VMNAME,format=raw,bus=virtio \
--os-variant generic \
--network network=default,model=virtio \
--graphics none \
--console pty,target_type=virtio \
--cdrom /path/to/isos/latest-nixos-minimal-x86_64-linux.iso
Once inside NixOS, run the following to setup the partitions, swap and generate the main configuration file. You can copy/paste the whole thing.
DISK="/dev/vda"
# Wipe disk
sudo wipefs -a "$DISK"
sudo sgdisk --zap-all "$DISK"
sudo partprobe /dev/vda
# Create new GPT partition table
sudo parted $DISK --script mklabel gpt \
mkpart primary fat32 2048s 500MB \
set 1 boot on \
mkpart primary ext4 500MB 100% \
print \
quit
# Format partitions
sudo mkfs.fat -F 32 "${DISK}1"
sudo fatlabel "${DISK}1" NIXBOOT
sudo mkfs.ext4 -F -L NIXROOT "${DISK}2"
sudo partprobe /dev/vda
# Mount partitions
sudo mount /dev/disk/by-label/NIXROOT /mnt
sudo mkdir -p /mnt/boot
sudo mount /dev/disk/by-label/NIXBOOT /mnt/boot
sudo dd if=/dev/zero of=/mnt/.swapfile bs=1024 count=2097152 # 2GB size
sudo chmod 600 /mnt/.swapfile
sudo mkswap /mnt/.swapfile
sudo swapon /mnt/.swapfile
sudo nixos-generate-config --root /mnt
# Adjust the main configuration to your needs
sudo -e /mnt/etc/nixos/configuration.nix
Add the following to the configuration.nix file to create your user with sudo permission:
users.users.yourusername = {
isNormalUser = true;
home = "/home/yourusername";
description = "my user";
extraGroups = [ "wheel" "networkmanager" ];
openssh.authorizedKeys.keys = [ "ssh-ed25519 AAAAyoursshkey... yourusername@somehost" ];
};
Add the following to configuration.nix in order to enable the SSH server:
Finish the installation:
Exit the virsh console by hitting Ctrl+]
Then you should be able to SSH into the box using.