When copying configuration from one machine to another, what to do about boot loader config options?

I’ve heard it said that one nice thing about NixOS is that one can copy a configuration from one machine to another, but then I notice a line like this in configuration.nix:

boot.loader.grub.device = "/dev/nvme0n1";

I’m a bit surprised that this isn’t in hardware-configuration.nix, since it seems that it’s specific to a given machine.

Do you typically just copy parts of the old configuration.nix into the new one, piecemeal, being mindful of what’s hardware-specific? Or is there a fancier way of copying a config from one machine to another?

I classify NixOS modules in two categories: modules that declare new bespoke options (and possibly use those bespoke options to control other config), and modules that merely define (set) options.

The former are put in a directory structure called modules/ and I import them all unconditionally in all configs.

The latter are host-specific because hosts are never identical. These modules live under hosts/. Each module is a different host’s entry-point. (In my case, I store all my hosts’ configs in the same repo.)

Hence I don’t believe the “copy config” propaganda, I define essentially different configs by host, and only import common config where it makes sense.

It doesn’t really matter what filenames you assign to a module, as long as you know what the code’s doing. hardware-configuration.nix is not a special filename, I assimilated it into my host-specific config and deleted that file.

3 Likes

Would I be correct in guessing that you (1) have a flake-based config, and (2), that config does more than just import configuration.nix?

I’ve been working on flakifying my config on my laptop, with the goal of getting something that I can mostly copy to my desktop once I install NixOS on it.

I happen to use flakes for NixOS config, but that’s not a requirement for this setup. After observing the divergences in flakes behavior in the (at least 3) nix implementations and what a mess lazy trees turned out to be, I’m probably going to get rid of flakes from the config…

Anyway, you could easily use e.g. nixos-rebuild boot --sudo -I nixos-config=./hosts/myhost.nix to achieve the same effect of having individual entry-points per host.

It really is as simple as adding configuration.nix (or whatever you named your entry point) to modules/imports, though.

Oh, I know that much. I’ve already done it.