Rebuild NixOS Offline

I use /etc/nixos/configuration.nix to mange my networks. The problem I have run into is that to connect to a new network after I add it to /etc/nixos/configuration.nix, I have to run nixos-rebuild switch, but this requires a network connection to run properly. Is there a way to rebuild nixos offine?

in general case, no, there isn’t. The easiest is to boot from installation drive (assuming you have it burned somewhere), where internet works and reinstall using existing configuration.nix.

If the only thing you do is change network (like, choose another WiFi network), it shouldn’t use network for rebuild. But there can be situations:

  • you may have updated channels, but didn’t rebuild system. Then (when network is off) you try to rebuild system with new config, but it now wants to redownload stuff, thus fails
  • you have impure derivations (import from remote derivation, fetchTarball without hash, etc), where caches do expire and you have to download stuff
  • you have import from derivation, but have run GC and build inputs for that IFD have to be redownloaded

So, the best you can do with this is NOT to manage network entirely declarative. New network connection should be possible to setup in CLI (and you should know “how”). I use NetworkManager and nmtui command, which helps a lot when I forget about different wpa_supplicant flags.

Besides the fact that it is obviously dangerous to remotely update the network on a remote machine in case of configuration typos and mistakes, here are the steps I take to make it work (when I do not forget…).

  1. Run nixos-rebuild build to check that it actually builds correctly
  2. Enable persistent user instances in systemd [1].
  3. Run nixos-rebuild {switch/test/boot} inside a screen (or tmux ?) session to keep it running after the ssh session ends.
  4. remove the ./result symlink left by nixos-rebuild build

For a long discussion of users caught by the same issue, read [1] further :smiley: .


PS: Running nixos-rebuild in a persistent session should have been implemented years ago. We are still waiting for someone to write the patch :wink:

1 Like