Reinstall NixOS -- with flakes and home manager

I want to reinstall and recreate my system. I have /home files backed up.
The system uses flakes and home manager, and the config files are stored in /home.
Unstable branch.

If I just run the automated installer, boot into the new system, copy over my config file, and rebuild, I will get errors about home manager not being installed etc.
(I know because I tried…)
So what’s the best (or most painless) strategy to get going?

Not the most experienced NixOS user here but I’ve installed NixOS on my machine several times and also have helped move my friends over to NixOS.

TL;DR: I install NixOS with the bare minimum config I need and once I’ve booted into the persistent system, I apply the config I want. It’s safer this way for me since I can always just rollback.

Here’s the approach I follow now:

PS: I don’t use the graphical installer; so my steps may get a little technical.

  • Once on the bootable media, I launch gparted and partition the way my friends want. A simple scheme I go for is a 1024 MiB fat32 boot partition and the remaining to be the actual usable space.
  • Second, I mount the “remaining” partition to /mnt and then the boot partition to /mnt/boot.
  • Third, I run sudo nixos-generate-config --root /mnt which creates two files (config and hardware config)
  • Now, I edit these files to configure some basic things like:
    • Creating a user
    • Setting a hostname
    • Making sure I install a browser and a text editor
  • Once done and saved, I run sudo nixos-install --root /mnt and then wait.
  • Reboot into your new system.

Doing it this way means I have to apply my personal config later but it gives me that 1st persistent generation to fallback on since otherwise I’d have to redo the entire thing all over.

2 Likes

This is where my troubles begin. When applying my config, it complains that home manager is missing, and other errors.

In that case, please share a link to your config, if public, otherwise the relevant bits of it; and of course, the error(s) you get.

Are you using the home-manager nixos module in your flake’s systemConfiguration? When you change your home-manager configuration do you use nixos-rebuild or home-manager switch?

https://nix-community.github.io/home-manager/index.xhtml#sec-flakes-nixos-module

Yes, home-manager is in the flake, as below.
I do nixos-rebuild switch --flake ~/nixos#nixos

{
    inputs = {
    nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
    home-manager.url = "github:nix-community/home-manager";
    home-manager.inputs.nixpkgs.follows = "nixpkgs";
    nix-snapd.url = "github:io12/nix-snapd";
    nix-snapd.inputs.nixpkgs.follows = "nixpkgs";
  };

  outputs = { self, nixpkgs, home-manager, nix-snapd }: {

    nixosConfigurations.nixos = nixpkgs.lib.nixosSystem {
      system = "x86_64-linux";
      modules = [
      ./configuration.nix
      home-manager.nixosModules.home-manager
      nix-snapd.nixosModules.default

      {
    home-manager.useGlobalPkgs = true;
    home-manager.useUserPackages = true;
    home-manager.users.shmuel = import ./home.nix;
    services.snap.enable = true;
  }
      ];

    };
  };
}

Unsure what nix-snapd is, but shouldn’t that home manager config be within home-manager.nixosModules.home-manager instead of nix-snapd.nixosModules.default?

Maybe the problem would be solved by running this before rebuild?

sudo nix-channel --add https://github.com/nix-community/home-manager/archive/master.tar.gz home-manager

sudo nix-channel --update

That looks correct to me.

When I install on a new computer I haven’t used yet I usually start with the base configuration from the live image and integrate the hardware modifications into my flake.

If I’m reinstalling I partition the disk, mount the necessary partitions in /mnt and run nix-shell -p git sudo nixos-install --flake "https://url-to-my-flake#system-name directly from the live CD. The nix-shell -p git part is because IIRC git is not installed by default on the live image and I cannot install some flake deps without it. I documented this in my flake’s readme: steinuil/flakes - flakes - mani mani

If you have a spare system with Nix installed, you can try cding into your flake directory, run nix repl, then :lf . and then :b nixosConfigurations.your-system.build.toplevel to see if your system configuration builds correctly outside of the live CD.

Nope, those config options go into the nixos configuration, so they’re fine there!

2 Likes