From aa825b87a6a16a8a663e14f52155bc924d28bad1 Mon Sep 17 00:00:00 2001 From: Pacman99 Date: Tue, 27 Apr 2021 10:26:45 -0700 Subject: [PATCH] auto import hosts in flake.nix this allows host-specific settings to be overriden with mkMerge --- doc/concepts/hosts.md | 56 ++++++++++++++++++++++++------------------- flake.lock | 2 +- flake.nix | 9 ++++--- lib/attrs.nix | 8 +++++++ lib/flake.nix | 5 ++-- 5 files changed, 48 insertions(+), 32 deletions(-) diff --git a/doc/concepts/hosts.md b/doc/concepts/hosts.md index 770fa51b..1fa90284 100644 --- a/doc/concepts/hosts.md +++ b/doc/concepts/hosts.md @@ -1,18 +1,24 @@ # Hosts Nix flakes contain an output called `nixosConfigurations` declaring an -attribute set of valid NixOS systems. To create hosts, you can use the -`nixos.hosts` argument and pass `modules` to each host. Host-specific modules -typically go in the `hosts` folder of the template. +attribute set of valid NixOS systems. To simplify the management and creation +of these hosts, devos automatically imports every _.nix_ file inside this +directory to the mentioned attribute set, applying the projects defaults to +each. The only hard requirement is that the file contain a valid NixOS module. -Each host should follow a certain channel to define the `pkgs` of that host. -You can use the `nixos.hostDefaults` to set defaults and global modules for all -hosts. +As an example, a file `hosts/system.nix` will be available via the flake +output `nixosConfigurations.system`. You can have as many hosts as you want +and all of them will be automatically imported based on their name. For each host, the configuration automatically sets the `networking.hostName` -attribute to the name of the host. This is for convenience, since `nixos-rebuild` -automatically searches for a configuration matching the current systems hostname -if one is not specified explicitly. +attribute to the name of the file minus the _.nix_ extension. This is for +convenience, since `nixos-rebuild` automatically searches for a configuration +matching the current systems hostname if one is not specified explicitly. + +You can set channels, systems, and add extra modules to each host by editing the +`nixos.hosts` argument in flake.nix. This is the perfect place to import +host specific modules from external sources, such as the +[nixos-hardware][nixos-hardware] repository. It is recommended that the host modules only contain configuration information specific to a particular piece of hardware. Anything reusable across machines @@ -21,16 +27,29 @@ is best saved for [profile modules](./profiles.md). This is a good place to import sets of profiles, called [suites](./suites.md), that you intend to use on your machine. -Additionally, you can pass modules from [nixos-hardware][nixos-hardware] in the -`modules` argument for relevant hosts. ## Example +flake.nix: +```nix +{ + nixos.hosts = mkMerge [ + (devos.lib.importHosts ./hosts) + { + librem = { + channelName = "latest"; + modules = [ hardware.purism-librem-13v3 ]; + }; + } + ]; +} +``` + hosts/librem.nix: ```nix -{ suites, hardware, ... }: +{ suites, ... }: { - imports = suites.laptop ++ [ hardware.purism-librem-13v3 ]; + imports = suites.laptop; boot.loader.systemd-boot.enable = true; boot.loader.efi.canTouchEfiVariables = true; @@ -39,15 +58,4 @@ hosts/librem.nix: } ``` -flake.nix -```nix -{ - nixos.hosts.librem = { - system = "aarch64-linux"; - modules = ./hosts/librem.nix; - }; -} -``` - - [nixos-hardware]: https://github.com/NixOS/nixos-hardware diff --git a/flake.lock b/flake.lock index 7841db57..91c20fdd 100644 --- a/flake.lock +++ b/flake.lock @@ -81,7 +81,7 @@ "utils": "utils_2" }, "locked": { - "narHash": "sha256-r+OPJF65PToVQK1ll2INAYmIV3zmnDzWBpGLx8m4aVo=", + "narHash": "sha256-hpvEXcpq85cDKi0F5UUsuMVISKlk8hgVJiz5FF29RwA=", "path": "./lib", "type": "path" }, diff --git a/flake.nix b/flake.nix index 5468f5a5..d30cc3a9 100644 --- a/flake.nix +++ b/flake.nix @@ -61,11 +61,10 @@ home.nixosModules.home-manager ]; }; - hosts = { - NixOS = { - modules = ./hosts/NixOS.nix; - }; - }; + hosts = nixos.lib.mkMerge [ + (devos.lib.importHosts ./hosts) + { /* set host specific properties here */ } + ]; profiles = [ ./profiles ./users ]; suites = { profiles, users, ... }: with profiles; { base = [ cachix core users.nixos users.root ]; diff --git a/lib/attrs.nix b/lib/attrs.nix index d1bc7648..93f79583 100644 --- a/lib/attrs.nix +++ b/lib/attrs.nix @@ -30,6 +30,14 @@ rec { value = import path; }); + importHosts = dir: + lib.os.recImport { + inherit dir; + _import = base: { + modules = import "${toString dir}/${base}.nix"; + }; + }; + concatAttrs = lib.fold (attr: sum: lib.recursiveUpdate sum attr) { }; # Filter out packages that support given system and follow flake check requirements diff --git a/lib/flake.nix b/lib/flake.nix index 17e069c2..2513311d 100644 --- a/lib/flake.nix +++ b/lib/flake.nix @@ -48,7 +48,8 @@ safeReadDir pathsToImportedAttrs concatAttrs - filterPackages; + filterPackages + importHosts; inherit (lists) pathsIn collectProfiles unifyOverlays; inherit (strings) rgxToString; inherit modules; @@ -60,7 +61,7 @@ { lib = utils.lib // { inherit (lib) - mkFlake pathsIn; + mkFlake pathsIn importHosts; }; }