auto import hosts in flake.nix

this allows host-specific settings to be overriden with mkMerge
This commit is contained in:
Pacman99 2021-04-27 10:26:45 -07:00
parent df39cb692e
commit aa825b87a6
5 changed files with 48 additions and 32 deletions

View File

@ -1,18 +1,24 @@
# Hosts # Hosts
Nix flakes contain an output called `nixosConfigurations` declaring an Nix flakes contain an output called `nixosConfigurations` declaring an
attribute set of valid NixOS systems. To create hosts, you can use the attribute set of valid NixOS systems. To simplify the management and creation
`nixos.hosts` argument and pass `modules` to each host. Host-specific modules of these hosts, devos automatically imports every _.nix_ file inside this
typically go in the `hosts` folder of the template. 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. As an example, a file `hosts/system.nix` will be available via the flake
You can use the `nixos.hostDefaults` to set defaults and global modules for all output `nixosConfigurations.system`. You can have as many hosts as you want
hosts. and all of them will be automatically imported based on their name.
For each host, the configuration automatically sets the `networking.hostName` For each host, the configuration automatically sets the `networking.hostName`
attribute to the name of the host. This is for convenience, since `nixos-rebuild` attribute to the name of the file minus the _.nix_ extension. This is for
automatically searches for a configuration matching the current systems hostname convenience, since `nixos-rebuild` automatically searches for a configuration
if one is not specified explicitly. 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 It is recommended that the host modules only contain configuration information
specific to a particular piece of hardware. Anything reusable across machines 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), This is a good place to import sets of profiles, called [suites](./suites.md),
that you intend to use on your machine. 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 ## Example
flake.nix:
```nix
{
nixos.hosts = mkMerge [
(devos.lib.importHosts ./hosts)
{
librem = {
channelName = "latest";
modules = [ hardware.purism-librem-13v3 ];
};
}
];
}
```
hosts/librem.nix: hosts/librem.nix:
```nix ```nix
{ suites, hardware, ... }: { suites, ... }:
{ {
imports = suites.laptop ++ [ hardware.purism-librem-13v3 ]; imports = suites.laptop;
boot.loader.systemd-boot.enable = true; boot.loader.systemd-boot.enable = true;
boot.loader.efi.canTouchEfiVariables = 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 [nixos-hardware]: https://github.com/NixOS/nixos-hardware

View File

@ -81,7 +81,7 @@
"utils": "utils_2" "utils": "utils_2"
}, },
"locked": { "locked": {
"narHash": "sha256-r+OPJF65PToVQK1ll2INAYmIV3zmnDzWBpGLx8m4aVo=", "narHash": "sha256-hpvEXcpq85cDKi0F5UUsuMVISKlk8hgVJiz5FF29RwA=",
"path": "./lib", "path": "./lib",
"type": "path" "type": "path"
}, },

View File

@ -61,11 +61,10 @@
home.nixosModules.home-manager home.nixosModules.home-manager
]; ];
}; };
hosts = { hosts = nixos.lib.mkMerge [
NixOS = { (devos.lib.importHosts ./hosts)
modules = ./hosts/NixOS.nix; { /* set host specific properties here */ }
}; ];
};
profiles = [ ./profiles ./users ]; profiles = [ ./profiles ./users ];
suites = { profiles, users, ... }: with profiles; { suites = { profiles, users, ... }: with profiles; {
base = [ cachix core users.nixos users.root ]; base = [ cachix core users.nixos users.root ];

View File

@ -30,6 +30,14 @@ rec {
value = import path; 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) { }; concatAttrs = lib.fold (attr: sum: lib.recursiveUpdate sum attr) { };
# Filter out packages that support given system and follow flake check requirements # Filter out packages that support given system and follow flake check requirements

View File

@ -48,7 +48,8 @@
safeReadDir safeReadDir
pathsToImportedAttrs pathsToImportedAttrs
concatAttrs concatAttrs
filterPackages; filterPackages
importHosts;
inherit (lists) pathsIn collectProfiles unifyOverlays; inherit (lists) pathsIn collectProfiles unifyOverlays;
inherit (strings) rgxToString; inherit (strings) rgxToString;
inherit modules; inherit modules;
@ -60,7 +61,7 @@
{ {
lib = utils.lib // { lib = utils.lib // {
inherit (lib) inherit (lib)
mkFlake pathsIn; mkFlake pathsIn importHosts;
}; };
} }