Sharing configuration across hosts

I now have two NixOS machines which I’m using the same configuration for. However there need to be differences between them (primarily the hostname and kernel modules for now).

How do people manage these differences in a sane way? I’ve previously used Org-mode in Emacs for this kind of thing, but I’m not an Emacs user any more so would like another approach.

Have 2 directories in a single git repo, each with a configuration.nix, then a 3rd common directory, which both configs import, which has the shared settings.


I have all my configurations in a single flake repo. I usually put shared configuration into modules that I can enable and disable (and potentially customize) on each host.

1 Like

Seems incredibly obvious now you say it! Thanks, I’ll get it configured using that method.

I do this, but instead of a directory per host, I have a branch per host.

  • All the stuff in the common directory (usually) only gets edited on the main branch, and merged to each host as they’re updated.
  • This means I can still have divergences on the per-host branch in the common files if needed.
  • I try and avoid it, and take it as a code smell that the common module is insufficiently general, but it lets me separate the change that identifies that need from the change that fixes the wrinkle.

This is slightly more cognitive load, but it works for me, and I chose this method when I was first starting out with nixos, because I knew how to maintain code branches and didn’t know how to make nix code conditional in the ways I might need.

I’m not likely to change this method until I migrate to system flakes, where the hostname ⇒ configuration lookup is automatically supported. I haven’t done that yet, mainly for two reasons:

  • This is (so far) the only compelling reason I can see to make the switch, and what I have works well until some other reason comes along.
  • I have a nagging feeling that I would probably want to keep separate branches for each host anyway, to track the flake.lock file for each host, since moving that from the channel update into the repo for more reproducible configs is another point of flakes.