How to get networking to work with libvirtd

I just cannot get a working connection with libvirtd on NixOS.

I am using virt-manager to manage my VMs. When checking the hardware configuration in virt-manager, the network section says “IP address: Unknown”. When I boot the VM and check what the local IP address is, I get an address starting with 169.254 and I cannot reach the internet.

My following configuration section could be relevant:

  networking = {
    networkmanager.enable = true;
    firewall.enable = true;
    nftables.enable = true;
  };
  virtualisation.libvirtd.enable = true;

On my system, dnsmasq is installed as the wiki suggests. Also net.ipv4.ip_forward is set to 1. I even tried following the Arch wiki but no success. I’ve tried disabling the firewall altogether but also nothing.

Has anyone gotten it to work? If so, how? Any help would be much appreciated!

How did you install virt-manager?

programs.virt-manager.enable = true;

I hope.

How did you create / import your vms? If using virt-install, you would have a parameter like

--network network=default

which should result in a domain xml including:

    <interface type='network'>
      <mac address='52:54:00:b4:02:fc'/>
      <source network='default'/>
      <model type='virtio'/>
      <address type='pci' domain='0x0000' bus='0x01' slot='0x00' function='0x0'/>
    </interface>

Is dhcp enabled on your guest vm? Even if not, can you try setting an ip manually?

ip addr add dev enp1s0 192.168.122.33
ip route add default via 192.168.122.1

or the like – this is assuming your default libvirt network is on 192.168.122.0/24 and guest device is enp1s0.

Also, make sure the libvirt default network is started, sometimes it is not.

virsh --connect qemu:///system net-list
virsh --connect qemu:///system net-start default
virsh --connect qemu:///system net-autostart default

I installed virt-manager by adding it to my environment.systemPackages. However I tried your approach and sadly nothing changed.

I created the VM with the virt-manager GUI. Also the default network was created that way. I ended up with the following xml for the default network:

<network connections="1">
  <name>default</name>
  <uuid>bf3007be-b9fe-4bdc-8486-765553cf4fd1</uuid>
  <forward mode="nat">
    <nat>
      <port start="1024" end="65535"/>
    </nat>
  </forward>
  <bridge name="virbr0" stp="on" delay="0"/>
  <mac address="52:54:00:d2:a3:fb"/>
  <domain name="default"/>
  <ip address="192.168.122.1" netmask="255.255.255.0">
    <dhcp>
      <range start="192.168.122.2" end="192.168.122.254"/>
    </dhcp>
  </ip>
</network>

And for the network interface xml I have this:

<interface type="network">
  <mac address="52:54:00:ca:00:b7"/>
  <source network="default" portid="21572af7-d8f7-4710-8223-a11a824c023b" bridge="virbr0"/>
  <target dev="vnet6"/>
  <model type="virtio"/>
  <alias name="net0"/>
  <address type="pci" domain="0x0000" bus="0x01" slot="0x00" function="0x0"/>
</interface>

I tried setting an IP manually as you suggested, and it somehow works but not entirely.
ip addr add dev enp1s0 192.168.122.33 works, but ip route add default via 192.168.122.1 throws Error: Nexthop has invalid gateway.

For testing purposes, I set the default route to the same IP that I added previously and it doesn’t throw that error, but I still cannot reach the internet.