It does also result in a lot of overhead in a file whose intent is to primarily be metadata. Personally I would use the per-host entrypoints to import the other modules, e.g.:
# ./hosts/craptop/configuration.nix
{
imports = [
../../generic.nix
./disk-config.nix
./hardware-configuration.nix
];
}
# ./generic.nix
{ pkgs, inputs, ... }: {
imports = [
inputs.disko.nixosModules.disko
];
_module.args.pkgs-unstable = inputs.nixpkgs-unstable.legacyPackages.${pkgs.stdenv.hostPlatform.system};
}
# flake.nix
{
outputs = {
self,
nixpkgs,
...
} @ inputs: {
nixosConfigurations.craptop = nixpkgs.lib.nixosSystem {
specialArgs = {
inherit inputs;
};
modules = [
./hosts/craptop/configuration.nix
];
};
};
}
If you’d like, this then also lets you DRY out the rest of the boilerplate completely, and replace it with a directory walk:
# flake.nix
{
outputs = {
self,
nixpkgs,
...
} @ inputs: {
nixosConfigurations = let
hostNames = nixpkgs.lib.pipe (builtins.readDir ./hosts) [
(nixpkgs.lib.filterAttrs (_: type: type == "directory")
nixpkgs.lib.attrNames
];
in
nixpkgs.lib.listToAttrs (map (name: {
inherit name;
value = nixpkgs.lib.nixosSystem {
specialArgs = { inherit inputs; };
modules = [ (./hosts + "./${name}/configuration.nix") ];
};
}) hostNames);
};
}
Et voila, we’ve replaced all our entrypoints with a bit of boilerplatey function calling. Stick it in a separate project and call it a framework, if that pleases you. YMMV, all that code was written on my phone with no nix binary in sight to check even just syntax.
It does come down to personal preference which route you take. The other approach has the benefit of telling you at a glance in flake.nix what each system consists of, if your file naming is up to scratch, while this approach revolves more around the per-host entrypoints telling you what the hosts are about.
I think @anon32574585 's approach is nicer for cattle-style deployments, mine more for the type of varied pet systems I deploy. The difference is mostly that with the former you end up coding in a deeply-nested structure, which is fine for less divergent hosts, but a royal PITA if you have a lot of modules.
My approach is also more resistant to upcoming changes to flakes, since all the actual meaningful code remains in the module system; it effectively just uses flakes for input management (which IMO is the best way to use them for NixOS). Makes you less hopelessly bought-in in case you want to switch to npins or something one day.
Some additional notes that are less high-level, I’ve already put them into action up there:
-
The
systemarg is deprecated, don’t specify it here, usehardware-configuration.nix- To hand a system to your
pkgs-unstable, do that inside your modules throughpkgs.stdenv.hostPlatform.systeminstead of hard-coding it on the flake level. This way you don’t need to repeat that, either.
- To hand a system to your
-
You can also put your
disko.nixosModules.diskoinside a sharedconfiguration.nix’simports; there’s no reason to repeat that with everylib.nixosSystem. -
Personally, I’d avoid using
let ... inas @anon32574585 suggests, because this makes those values bound opaquely, and inaccessible to the module system. You can useconfig.references instead.I concur however that you shouldn’t take DRY too far; its benefits are mainly preventing mistake-by-copy-paste stuff and drift over time; weigh the need for that by likekihood of such issues. Likely very minimal for a personal config with one contributor, and more complex projects (in terms of contribution complexity) almost certainly won’t have a need for the type of deduplication you’d employ.