Tips for setting up specific configuration per machine

Like as many here, i would like to have a config to rule them all , but i have a few machines that i need to setup nvdia proprietary drivers to work properly and from the wiki (Nvidia - NixOS Wiki) is recommended to put in configuration.nix, but i would like to have a way to use this only for nvidia machines and let the configuration.nix to be the generalist as possible.
So, how do you guy manage this ?

Put the nvidia configuration in a separate module and add them to the imports of a per-host module. I.e.:

# configuration.nix
{
  # Generic stuff
}
# hosts/<hostname>.nix
{ config, lib, ... }: {
  imports = [
    ../configuration.nix
    ../nvidia.nix
    ./hardware-configuration.nix
  ];

  # Other host-specific stuff and overrides, for example
  hardware.nvidia.package = lib.mkForce config.boot.kernelPackages.nvidiaPackages.legacy_470;
}
# nvidia.nix
{
  # Generic nvidia config
}

Then you just point nixos-rebuild at your host-specific config (either through flake attrs or -I 'nixos-config=').

This leverages the NixOS module system and all its features for composition properly, so - very IMO - any other approach is invalid. The fixed-point and override system is specifically designed for this kind of configuration management. It requires thinking a bit differently for folks who think about programming imperatively, but it’s very expressive once you get used to it.

thanks, and just to see if i get, its a good practice to have the hosts on the same flake.nix and then do a rebuild by this flake per host ?

Ah, if you’re using flakes you would set up a separate nixosConfiguration for each host, only adding their respective entrypoint in the hosts directory I implied to the modules arg.

Of course, if you’re maintaining cattle-style hosts, you’d have one shared host configuration for subsets of cattle. But once we’re talking about that kind of deployment advice takes more effort and insight into your stack than I have right now.

Anyway, my point is, flakes just express the structure of nix projects, and should have no impact on your architecture as far as NixOS goes. To the point that I don’t think flakes should actually be used for NixOS projects - but that’s a different story.