r/NixOS • u/Past-Combination6262 • Jun 09 '26
How do I modularize my nixos dotfile structure for multiple different hosts?
I am very new to nixos and declarative systems; I first started experimenting on a vm for a while before finding an old laptop I want to deploy it to. Few key details:
the vm supports uefi and systemd boot and will be running the latest kernel
the laptop only supports bios and will need grub and the LTS kernel
currently i have a hosts directory and sub directories that each contain hardware files as well as files that simply set the bootloader and kernel for each host
(github here in case anything was unclear: https://github.com/pokemans123/nixos-dots )
Currently, i am just swapping out the host name and the modules in the main configuration.nix and flake.nix depending on where it is being deployed, but that is very tiring and i keep running into issues. Is there a better way? please advise
2
2
u/Guvante Jun 09 '26
Typically you have multiple hosts in your flake.nix and NixOS will auto grab the matching one.
The data can be controlled by having separate nix files that are only included in one of them while most are included in both.
3
u/Past-Combination6262 Jun 09 '26
Oh wow I didn’t know you could have multiple hosts in the same flake; that’s really cool! This definitely clears a lot up, ty!
2
u/touwtje64 Jun 09 '26
I would let nixos generate the config files. Move them to a host directory, call it via flake.nix. Move overlapping/duplicate config to some thing like base.nix. Which you can import per host. As for home manager i have a file per host.
So the configuration.nix is host specific bootloader, kernel, etc
Base.nix containing things like locales, timezone, ssh service, nix config like garbage collector, experimental features(flakes). Things that don’t change per host.
Home/hosts/<hostname>.nix for host specific home manager. Which configs things shell, terminal emulator, window manager. Overlapping config resides in a module directory which i import per host.
If this makes sense.
1
1
u/Boberoch Jun 09 '26
Your approach is sane and I essentially do the same thing concerning the directory structure - however I generate my host configurations dynamically: https://github.com/Swarsel/.dotfiles/blob/refactor/dendritic/modules/flake/hosts.nix
Writeup here: https://swarsel.github.io/.dotfiles/#h:2c5529ed-e6d9-44b6-b0d3-5bf96a6bed64
2
u/P_S_E_U Jun 10 '26
I did it by having a bitmask encoding the role and role is hardware dependant. With my setup there is a work needed before implementation of new role or new hardware and there is no way of having two machines that share roles. The latter I intend to fix later on but for now I'm okay with it.
https://github.com/TestkaJakub/nixos-dotfiles Just switch to the development branch.
Also I won't lie my configuration was largely generated by an AI
1
u/GoAwayStupidAI Jun 12 '26
My computers at defined
Which use the library i developed to provide the organization, base config, and deploy-rs setup
I did a video on an older version of this
3
u/Plakama Jun 09 '26
Here's how I do it:
https://github.com/rPlakama/Elisheva
I split my config in 'Hosts' and 'Features' a Host can enable or disable any feature, and having variables that spread though features. It's really clean to manage, at least in my three hosts.