Installing NixOS on a new drive

Previously, my system had two physical drives, and I had / and /home mounted on each of them.

The user profile was managed by home-manager (using the NixOS module).

Since my / was getting too full, I purchased a new drive and started moving the system to it. Install was pretty straightforward:

  • Partition the new drive (GPT): create two partitions, fat32 for boot and a filesystem of choice for /.
  • Mount the partitions under /mnt/new/boot and /mnt/new respectively.
  • Copy over /etc/nixos to the new drive.
  • nixos-generate-config on the new drive.
  • Add /boot to the hardware-configuration.nix on the new drive.
  • nixos-install --root /mnt/new

All of this worked as expected, however, I now see there are some issues.

My /home contains symlinks like these:

lrwxrwxrwx 1 ashesh users 45 Jun 22  2019 /home/ashesh/.nix-profile -> /nix/var/nix/profiles/per-user/ashesh/profile

and I’m not sure how to migrate these to the new installation.
I’ve so far found no guides on this and any suggestions will be very helpful. I thought I can just copy over /nix/var/nix/profiles to the new disk but I’m not even sure that’s safe.

How should I proceed?


Edit: This post even talks about copying over a few directories from /var/lib.

The .nix-profile link is managed by Nix. You don’t need to copy it over; it’ll get recreated when your user profile is activated in your new installation.

If you have other links pointing to locations under /nix/store, those will be recreated by Home Manager and you don’t need to worry about them either.

1 Like

That involves having /home mounted and doing nixos-rebuild, correct?

Depending on your configuration it might not exist until you install Home Manager, and I don’t actually know if there’s a difference here between installing Home Manager as a module versus using the standalone installation instructions. Ultimately Nix will create what it needs, and we can largely remain ignorant of the details.

So, in the end, this turned out to be slightly nontrivial.

I had to also generate the directory as indicated here and then chown it as my user.

Things worked flawlessly afterward.


Steps:

  1. Install new drive into the system.
  2. Create a GPT partition table, with at least two partitions. A fat32 boot partition (labelled boot-new) and a root partition (labelled root-new) with a filesystem of your choice.
  3. Ensure the boot flag is set on the boot partition (without it, in my experience, nixos-install wasn’t installing the bootloader).
  4. Create a new directory for the installation, say /mnt/new.
  5. Mount partitions:
mount /dev/disk/by-label/root-new /mnt/new && \
mkdir /mnt/new/boot  && \
mount /dev/disk/by-label/boot-new /mnt/new/boot
  1. mkdir /mnt/new/etc && cp -r /etc/nixos /mnt/new/etc/
  2. cd /mnt/new
  3. nixos-generate-config and adjust values in filesystems
  4. nixos-install --root /mnt/new

This should install NixOS on the new drive. Once done, reboot and select the new drive from the boot order in BIOS.

Once booted as su, do,

  1. mkdir -m 0755 -p /nix/var/nix/{profiles,gcroots}/per-user/$USER where $USER is your username.
  2. chown -R $USER /nix/var/nix/{profiles,gcroots}/per-user/$USER
  3. nixos-rebuild switch

As usual, with some software, reproducibility goes out of the window. I needed to re-instantiate nextcloud-client and gnome-keyring. Trivial, but doesn’t work out of the box.