Also see here, where I try to repair (not fully rebuild) my /nix/store by a similar method


Discovered corruption in one of my /nix/store files so trying to rebuild my entire /nix/store from scratch, similar to last time.

Taking the opportunity to refine and re-document the process

Steps:

  1. Make a fucking backup of your computer first

  2. Boot into nixos installer USB

  3. Run sudo su

    All of the rest of the commands are to be run as superuser

  4. Prep /mnt/etc/nixos/configuration.nix

    zpool import -af
    mkdir -p /per /mnt/etc/nixos/
    mount -t zfs rpool/eyd/per /per
    ln -s /per/config/systems/lake.nix /mnt/etc/nixos/configuration.nix
    
    

    Details aside, the point here is just that I’m symlinking /mnt/etc/nixos/configuration.nix, which is where nixos-install will look for a config, to the actual config.

    To do this I mount the part of my filesystem containing the config, which is the ZFS dataset rpool/eyd/per, to /per, and then create the symlink into /per.

    I think it may also work to just run nixos-install with -I nixos-config=/per/config/systems/lake.nix, but I haven’t tried it.

    Note to self: it’s necessary to mount rpool/eyd/per as specifically /per, because the configuration.nix uses builtins.toString /per. Another option is to swap that out for builtins.toString /mounted/location/of/per for install.

  5. Prep /mnt/nix

    mkdir /mnt/nix
    mount -t zfs rpool/eyd/nix /mnt/nix
    

    Here I’m mounting my computer’s actual /nix to /mnt/nix. We want to rebuild it, so let’s nuke it:

    rm -rf /mnt/nix/*
    
  6. Prep /mnt/boot

    mkdir /mnt/boot
    mount /dev/nvme0n1p3 /mnt/boot
    

    The /dev/nvme0n1p3 device is my boot partition; yours may differ. I did not delete anything from /mnt/boot or “nuke” it like I did to /mnt/nix; I just left it as-is.

  7. Rebuild!

    nixos-install \\
    	--no-root-password \\
    	-I nixpkgs='<https://releases.nixos.org/nixos/23.05/nixos-23.05.1092.c7ff1b9b956/nixexprs.tar.xz>'
    

    Here -I nixpkgs= is pinning the nixpkgs used to the correct version (this shouldn’t be necessary if your system config is a flake). This is super important!

    And --no-root-password is asking nixos-install to skip the final installation step, which is setting a password for the root account. This is unnecessary for us since we throw out /mnt/etc/shadow anyway.

    Running nixos-rebuild is gonna populate a bunch of stuff under /mnt, but all we’ll keep is /mnt/nix and /mnt/boot since those are the only actual mountpoints.