Search code examples
deploymentvirtualizationnixnixos

NixOS within NixOS?


I'm starting to play around with NixOS deployments. To that end, I have a repo with some packages defined, and a configuration.nix for the server.

It seems like I should then be able to test this configuration locally (I'm also running NixOS). I imagine it's a bad idea to change my global configuration.nix to point to the deployment server's configuration.nix (who knows what that will break); but is there a safe and convenient way to "try out" the server locally - i.e. build it and either boot into it or, better, start it as a separate process?

I can see docker being one way, of course; maybe there's nothing else. But I have this vague sense Nix could be capable of doing it alone.


Solution

  • There is a fairly standard way of doing this that is built into the default system.

    Namely nixos-rebuild build-vm. This will take your current configuration file (by default /etc/nixos/configuration.nix, build it and create a script allowing you to boot the configuration into a virtualmachine.

    once the script has finished, it will leave a symlink in the current directory. You can then boot by running ./result/bin/run-$HOSTNAME-vm which will start a boot of your virtualmachine for you to play around with.

    TLDR;

    1. nixos-rebuild build-vm
    2. ./result/bin/run-$HOSTNAME-vm