Create VM with two interfaces

Hello,
I am following the guide in NixOS virtual machines — nix.dev documentation and I am wondering, is there a way to create the VM with two network interfaces? I know I can edit the file in ./results/bin, but is there a way to tell nix-build to create the file with two network interfaces right away?

Thanks in advance.

It depends on what exactly you want, however you can set virtualisation.vmVariant.virtualisation.qemu.networkingOptions (https://nixos.org/manual/nixos/stable/options#opt-virtualisation.qemu.networkingOptions) to set the networking interfaces.

Basically, I want to create a VM from the configuration.nix file I have (nix-build ‘<nixpkgs/nixos>’ -A vm -I nixpkgs=channel:nixos-26.05 -I nixos-config=./configuration.nix) that has two network interfaces. I am planning to use NixOS as gateway and I want to test my config as close to prod as possible.

It’s hard to give specifics as the default interface (user/slirp) only really makes sense for one interface. You could try looking at the QEMU documentation to see if any of the supported interfaces fits your use case.

Yes, there is, but what are you trying to achieve?

The NixOS test infrastructure allows to easily create multiple network segments, which are represented by an eth{n} interface on a VM, and connect together multiple machines through a vswitch.

However, as MaxHearnden says, the VM you created by nixos-rebuild build-vm (equivalent to your command) is simply set up to share your guest network by simulating a DHCP server and an IPv4 NAT.

So, multiple interfaces don’t really make sense… unless you change the network backend to something else and maybe share have multiple interfaces on the guest.