Overriding network configuration for when a virtual machine test image is built


I’m trying to configure my NixOS servers with a flake that contains all the configurations.

I have taken a good deal of inspiration from My New Network (and deploying it with deploy-rs) - sam@samleathers.com ~ $ (although I am not using deploy-rs).

One thing I really think is cool/worthwhile is being able to build a VM to test what my server will look like before I take it to any metal. As I’m migrating from an old, non-NixOS server, this is really helping me get things prepared.

By using these commands, I can have a VM built and running quickly:

nix build .#nixosConfigurations.mymachine.config.system.build.vm

However the VM waits around for 90 seconds ‘waiting for enp3s0’, presumably because I have the following bit of configuration related to my physical server’s Ethernet port:

  networking.interfaces.enp3s0.useDHCP = false;
  networking.interfaces.enp3s0.ipv4.addresses = [
    { address = ""; prefixLength = 16; }
  networking.defaultGateway = "";
  networking.nameservers = [ "" ];

Can I, within my NixOS config, detect that a VM image is being built and change which options I enable? Alternatively, what do other people do for this kind of situation? Have a ‘myservervm’ target which imports many of the same files in common?