Hello, I’m converting my NixOS configuration to a flake and refactoring it, and I discovered how useful nixos-rebuild build-vm can be to test out my config in a VM before committing it to my machine.
However, I need some settings to be different when I’m in a VM. What’s the best way to detect that the system is being built by build-vm?
PS: I discovered that you can build and run the VM in one step with nix run '.#nixosConfigurations.HOSTNAME.config.system.build.vm'. Might be obvious but maybe this can be useful for someone.
I define a second nixosConfiguration, call it vm, and import all the modules the normal config does, plus an extra module for my vm settings (defined inline in flake.nix because it’s so small). I also make liberal use of lib.mkOverride.
Then build my vm with nixos-rebuild build-vm .#vm.
I personally prefer the separate entrypoints because it makes very explicit where my test environment differs from the final deployed one, while having if conditions littered across modules makes it a bit harder to trace. On the other hand, the latter is very good at keeping bits of related configuration close together.
It’s a trade-off, but I think these are the two most reasonable options.