installing NixOS on KVM - QEMU

https://wiki.archlinux.org/title/QEMU

There were some issue when installing NixOS on VirtualBox:

  • small resolution
  • cannot start hyprland

One thread suggested to use KVM instead.

So here I am…

CLI

Tip

It’s better to use virt-manager as it’s more user-friendly and will add the corresponding flags.

Installation

# install in arch
yay -S qemu-full

Tip

When I tried to launch the installation, I had a “Kernel panic” error with the message “is deadlock on memory”. By default only 128 MiB of memory is assigned to the machine. The amount of memory can be adjusted with the -m switch, for example -m 512M or -m 2G.

Issue encountered

Cannot start SSH daemon ✅

I installed NixOS using the following commands:

# create hard disk image
qemu-img create -f raw nix 50G
 
# start NixOS installation from ISO image
qemu-system-x86_64 \
  -cdrom ~/Downloads/nixos-gnome-24.05.675.805a384895c6-x86_64-linux.iso \
  -boot order=d \
  -drive file=nix,format=raw \
  -m 2048

I had the following error:

[ERROR] Cannot start ssh daemon

I changed the command from -boot order=d to -boot menu=on.

However, now, it’s a black screen…

Success

I tried another guide: https://www.baeldung.com/linux/qemu-from-terminal

# first create qcow2 hard disk image
qemu-img create -f qcow2 nix.qcow2 50G
 
# execute image
qemu-system-x86_64 \
  -enable-kvm \
  -m 4G \
  -smp 2 \
  -hda nix.qcow2 \
  -boot d \
  -cdrom ~/Downloads/nixos-gnome-24.05.675.805a384895c6-x86_64-linux.iso \
  -netdev user,id=net0,net=192.168.0.0/24,dhcpstart=192.168.0.9 \
  -device virtio-net-pci,netdev=net0 \
  -vga qxl

Stuck on loading screen ✅

I used another command to support UEFI. It started good, i.e. the resolution was greater than the previous one (3/4 of my screen), however, the screen is stuck to the loading screen with the TianoCore and Nix logos…

Success

Used the guide from above https://www.baeldung.com/linux/qemu-from-terminal, and it worked.

Small resolution ✅

Success

See https://superuser.com/a/169243. I needed to add the flag -vga std when executing the qemu-system-x86_64 command.

Clipboard not shared ❌

Tried to add SPICE to have shared clipboard from host to guest:

$ # not working as I do not have SPICE client
$ qemu-system-x86_64 -enable-kvm \
  -m 4G \
  -smp 2 \
  -hda nix.qcow2 \
  -netdev user,id=net0,net=192.168.0.0/24,dhcpstart=192.168.0.9 \
  -device virtio-net-pci,netdev=net0 \
  -vga std \
  -device virtio-serial-pci \
  -spice port=5930,disable-ticketing=on \
  -device virtserialport,chardev=spicechannel0,name=com.redhat.spice.0 \
  -chardev spicevmc,id=spicechannel0,name=vdagent \
  -display spice-app
qemu-system-x86_64: info: Launching display with URI: spice+unix:///tmp/.EECBP2/spice.sock
qemu-system-x86_64: Failed to launch spice+unix:///tmp/.EECBP2/spice.sock URI: The specified location is not supported
qemu-system-x86_64: You need a capable Spice client, such as virt-viewer 8.0
 
$ # Installing spice client, and now it's working!
$ yay -S virt-viewer

Fail

I gave up. It seems it’s not possible to share the clipboard on Wayland.

virt-manager

Installation

Installation tutorial: https://fr.linux-console.net/?p=22252

Issues encountered

No GUI for Hyprland ✅

Configure the VM with 3D acceleration on by updating its configuration:

  • Video Model: Virtio
  • Display Spice
    • Listen Type: None
    • Check OpenGL
    • Select 0000:00:02:0 Intel

Network “default” is not active ✅

I installed NixOS at bioserenity office using virt-manager.

Now, I tried to start the VM at home, but I got the error:

Error starting domain: Requested operation is not valid: network 'default' is not active

Traceback (most recent call last):
  File "/usr/share/virt-manager/virtManager
...

Success

I had to enable it:

$ # check network
$ sudo virsh net-list --all
 Name      State      Autostart   Persistent
----------------------------------------------
 default   inactive   no          yes
 
$ # enable default network
$ sudo virsh net-start default
Network default started
 
$ sudo virsh net-list --all
 Name      State    Autostart   Persistent
--------------------------------------------
 default   active   no          yes
 
$ # enable auto start so it's enable with boot
$ sudo virsh net-autostart default
Network default marked as autostarted
 

Source: https://askubuntu.com/a/1120132

Alacritty not launched on Hyprland startup ✅

When I re-started the VM at home, using Hyprland, alacritty is not executing automatically. Moreover, when I tried to SUPER+Return, no console is spawn…

Success

It was my alacritty/default.nix configuration that was not working… I had the following:

{ pkgs, ... }: {
  home.packages = with pkgs; [ alacritty ];
 
  # Symlink ~/.config/alacritty
  xdg.configFile.alacritty = {
    source = ./config;
    recursive = true;
  };
}

And the symlink creation made some issues…

Shared clipboard ❌

The clipboard are not shared between the host and the VM.

Tried adding the following configuration at NixOS level:

{ ... }: {
  services = {
    qemuGuest.enable = true;
    spice-vdagentd.enable = true;
  };
}

then, in virt-manager, add channels:

  • qemu-vdagent
    • device type: qemu-vdagent
    • target type: virtio
    • target name: org.qemu.guest_agent.0
    • clipboard: on
<channel type="qemu-vdagent">
  <source>
    <clipboard copypaste="yes"/>
  </source>
  <target type="virtio" name="org.qemu.guest_agent.0"/>
  <alias name="channel0"/>
  <address type="virtio-serial" controller="0" bus="0" port="2"/>
</channel>
  • spice
    • device type: spicevmc
    • target type: virtio
    • target name: com.redhat.spice.0
<channel type="spicevmc">
  <target type="virtio" name="com.redhat.spice.0"/>
  <address type="virtio-serial" controller="0" bus="0" port="2"/>
</channel>

There was another channel that was present before I added my channels (its name was conflicting with the channel I wanted to add):

<channel type="unix">
  <target type="virtio" name="org.qemu.guest_agent.0"/>
  <address type="virtio-serial" controller="0" bus="0" port="1"/>
</channel>

I followed the tips here: https://unix.stackexchange.com/questions/642562/make-shared-clipboard-work-for-qemu. However, it seems it’s only working for X11.

Fail

Could not find a way to share clipboard. Instead, I enable ssh so it’s easier to copy paste.

Cannot ssh from host to VM ✅

$ ssh l-lin@192.168.122.159
l-lin@192.168.122.159: Permission denied (publickey,keyboard-interactive).

I enabled sshd at NixOS level:

{ ... }: {
  services.openssh = {
    enable = true;
    settings = {
      AllowUsers = [ "l-lin" ];
      PasswordAuthentication = false;
      PermitRootLogin = "yes";
    };
  };
 
  # Start the OpenSSH agent when you log in.
  # The OpenSSH agent remembers private keys for you so that you don’t have to type in passphrases
  # every time you make an SSH connection.
  # Use ssh-add to add a key to the agent.
  programs.ssh.startAgent = true;
}

However, it’s not connecting. Maybe I need to add my public key?

Success

I only needed to change the value of PasswordAuthentication to true. Source: https://stackoverflow.com/a/4398006