`nixos-rebuild switch` crashes my machine

Hi friends, I have a NixOS 23.11 VM. Any time I run nixos-rebuild switch, my graphical session goes black and the VM becomes unresponsive. A grub entry is added for a new generation, but the new generations fail to boot into the GUI and instead drop back to a tty login.

My understanding is that nixos-rebuild switch should never break the existing, running generation. Is that correct? Ie is this a bug? What’s the best way to debug?

do you have enough ram and disk-space inside the vm?

Yes, there’s 32G RAM accessible to the VM and more than enough disk space

nixos-rebuild switch does not break in the sense of “the new generation cannot be computed or switched to”. In your case looks like the generation can be built and switched to, but your graphical environment stops working.

First place to look would probably be outputs of journalctl and systemctl status – check if there are any errors.

Also check your configuration code – maybe a module import was removed.


Even then, this shouldn’t interrupt the execution of the currently running system/generation, no?

nixos-rebuild switch builds then switches. The build of a new system doesn’t interfere with the current system, but the switch by definition replaces your old system with the new one, which requires interruption of at least portions of the running system. The process usually involves restarting some systemd units and (IIRC) graphics drivers, which could cause the behaviour you are describing. Could you be thinking of nixos-rebuild boot? In contrast to switch, it doesn’t apply your changes (other than bootloader changes) until after a reboot, so it won’t affect the current run. See this wiki page for more context.


Generally, if you’re doing a significant upgrade (e.g. 23.11 → 24.05), I’d recommend nixos-rebuild boot instead of nixos-rebuild switch.

1 Like

Thank you @physics-enthusiast and @vcunat for this explanation. How do I know when it is safe to switch vs when it is safe to boot? Ie, for a given configuration change, how can I determine which to safely use?

After trying nixos-rebuild switch, I find that it completes successfully, but after rebooting into the new generation the GUI fails to boot and I’m left back at tty1. How can I debug/fix this?

I don’t know in particular. It’s more a general observation.

Making switch work right isn’t easy, because you don’t switch all of your system immediately (kernel, your desktop environment and everything spawned from it), so various corner cases can happen during the transitional period.