How to rebuild the configuration of a NixOS installation from Live CD

nixos

I installed NixOS 18.03 from Ubuntu on another partition following the NixOS manual's "2.4. Installing from another Linux distribution" section. Everything went fine, but I did a couple idiotic things [?], namely:

  • Forgot to add the extra GRUB boot loader entry for the Ubuntu installation before nixos-install. Added it as an afterthought after install, and did a reboot (of course, no Ubuntu entry)

  • Did not enable any networking in configuration.nix, and ended up with no network configuration commands after reboot to connect to wifi. The catch 22 is that nixos-rebuild switch requires a network connection, so I couldn't finalize any changes.

So my thinking was that I can boot from a NixOS Live CD (17.03),connect to our wifi and somehow rebuild the config of the installation.

It is more than possible that I am missing something essential, have incorrect assumptions above etc; fairly new at nix and NixOS.


EDIT: I forgot to include how my partitions are set up and what I tried before successfully installing NixOS.

Partitions (mountpoints from Ubuntu):

sda
├─sda1         ntfs     Recovery              # some Win7 artifact
├─sda2         vfat               /boot/efi
├─sda3         vfat     NIXBOOT               # boot partition (esp, boot)
├─sda4         ext4     onyx                  # NixOS data
├─sda5         swap                           # Ubuntu swap
│ └─cryptswap1 swap               [SWAP]
├─sda6         ext4                           # (Arch install)
├─sda7         ext4               /           # Ubuntu install
├─sda8         swap     nixswap   
└─sda9         ext4     home      

I didn't want to mess up the Ubuntu boot partition, so I created another one (/dev/sda3). My plan was to later include a menu entry in Ubuntu's GRUB for NixOS, but for now, install, reboot and test booting NixOS from GRUB console (set root=..., linux ..., initrd ..., boot)

sudo PATH="$PATH" NIX_PATH="$NIX_PATH" `which nixos-install` --root /mnt --no-bootloader

After reboot, I couldn't see anything on the NixOS boot partition. Went back to Ubunut, installed without --no-bootloader, remembered to add an entry for Ubuntu and reboot. (It was only after this that I realized that systemd-boot and GRUB are two completely different things…)


UPDATE: I was able to get back to Ubuntu by selecting the Ubuntu boot partition as an alternative boot device in BIOS, and the usual GRUB menu came up. I may just redo the install with the right config.

Best Answer

The simplest way to go is to install from the LiveCD.

nixos-generate-config will regenerate the hardware config, but if it finds configuration.nix already exists it will leave it alone. And nixos-install is designed such that it can be safely executed as many times as needed.

This means you can follow the main installation guide using the filesystem (and configuration) you already created for NixOS and just continue where you left off.

Some things to be mindful of:

  • NixOS will install systemd-boot by default on EFI systems. So you'll end up with a new EFI executable along side the ones you already have.
  • nixos-install will also attempt to set systemd-boot as the default boot manager. I believe you can disable this by setting boot.loader.efi.canTouchEfiVariables to false in configuration.nix
  • I recommend installing NixOS with a basic config; For example, setup networking, users, and install a text editor but not much else. The reason is that the LiveCD uses a Nix store which is held in RAM. Your system will be first installed to this RAM-backed Nix store and then copied to disk. Once installed and bootable you can safely proceed with the rest of the configuration.
Related Question