Build and test NixOS before deployment

I’m trying to build sd card images for raspberry pi but the build itself is a bit finicky and not very reliable. I was trying to find a good way to build NixOS locally on a system without NixOS to then try it out either in VM or docker but both of these have some issues of their own and seem generally a bit advanced or not very used.

What do people usually do to test out new services/configuration changes? Do you just do nixos-rebuild switch and if there are any issues just roll back?

Is there any reliable way to test a nix configurutation out locally/in VM/in docker before deploying it?

There are many more options than just the switch. If you take a look at the man page of nixos-rebuild you can see that there is the build option for example. Or maybe more fitting there is the buid-vm option where you could test the system before deploying it on the rpi. There are also the build and test options that could apply to your situation but just take a look at the man-page.
Hope that was helpful!

I use nixos-rebuild build-vm, as @lovirent mentions, but that indeed does not work on non-NixOS systems.

If I wanted to support non-NixOS systems, I’d add an app to my flake that builds a generic qemu image using nixos-generators and then runs it with a simple script. Which is basically what nixos-rebuild build-vm does, except it doesn’t rely on the host having a nix store.

Or just switch to NixOS for development, build-vm is incredibly useful. You can still write a custom script for convenient access via a nix flake, in this case I make some small changes (e.g. not including hardware-configuration.nix) to the VM target so that I can test the software without having underlying hardware/networking quirks get in the way.

If you want to run tests, that’s an option too. There’s even a convenient helper library in the nixpkgs repo, though it’s not really designed to help test a system configuration. Still, I’ve written an integration test or two with that before.

You might also find deploy-rs with its auto-rollback and pre-deployment test features interesting.

1 Like

I did not think about it not working on non nixos,
thanks for clarifying that