From 9a9524a71eba787f394f3c7b63737cef1411c10f Mon Sep 17 00:00:00 2001 From: reo101 Date: Sun, 27 Nov 2022 15:18:40 +0200 Subject: [PATCH] feat(flake): refactor configuration definitions Now a bunch of configurations may be created from two functions and a set: `pred` - a validator that says whether a configuration is valid `mkHost` - a generator for that system's configuration `machines` - a nested attrset containing all the info for the machines --- flake.nix | 126 +++++++++++++++++++++++++++--------------------------- 1 file changed, 62 insertions(+), 64 deletions(-) diff --git a/flake.nix b/flake.nix index 94cf593..1af9c1f 100644 --- a/flake.nix +++ b/flake.nix @@ -166,77 +166,75 @@ extraSpecialArgs = { inherit inputs outputs; }; }; - # Final configurations + createConfigurations = + pred: mkHost: machines: + nixpkgs.lib.foldAttrs + (acc: x: acc) + [] + (builtins.attrValues + (builtins.mapAttrs + (system: hosts: + nixpkgs.lib.attrsets.filterAttrs + (host: config: config != null) + (builtins.mapAttrs + (host: config: + if (pred system host config) + then mkHost system host config + else null) + hosts)) + machines)); + + # Final configurations nixosConfigurations = - nixpkgs.lib.foldAttrs - (acc: x: acc) - [] - (builtins.attrValues - (builtins.mapAttrs - (system: hosts: - nixpkgs.lib.attrsets.filterAttrs - (host: config: config != null) - (builtins.mapAttrs - (host: config: - if (hasFiles [ "configuration.nix" ] config) - then mkNixosHost system host - else null) - hosts)) - nixosMachines)); + createConfigurations + (system: host: config: + hasFiles + [ "configuration.nix" ] + config) + (system: host: config: + mkNixosHost + system + host) + nixosMachines; nixOnDroidConfigurations = - nixpkgs.lib.foldAttrs - (acc: x: acc) - [] - (builtins.attrValues - (builtins.mapAttrs - (system: hosts: - nixpkgs.lib.attrsets.filterAttrs - (host: config: config != null) - (builtins.mapAttrs - (host: config: - if (hasFiles [ "configuration.nix" "home.nix" ] config) - then mkNixOnDroidHost system host - else null) - hosts)) - nixOnDroidMachines)); + createConfigurations + (system: host: config: + hasFiles + [ "configuration.nix" "home.nix" ] + config) + (system: host: config: + mkNixOnDroidHost + system + host) + nixOnDroidMachines; darwinConfigurations = - nixpkgs.lib.foldAttrs - (acc: x: acc) - [] - (builtins.attrValues - (builtins.mapAttrs - (system: hosts: - nixpkgs.lib.attrsets.filterAttrs - (host: config: config != null) - (builtins.mapAttrs - (host: config: - if (hasFiles [ "configuration.nix" ] config) - then mkNixDarwinHost system host - (builtins.map - (nixpkgs.lib.strings.removeSuffix ".nix") - (builtins.attrNames (config."home" or {}))) - else null) - hosts)) - nixDarwinMachines)); + createConfigurations + (system: host: config: + hasFiles + [ "configuration.nix" ] + config) + (system: host: config: + mkNixDarwinHost + system + host + (builtins.map + (nixpkgs.lib.strings.removeSuffix ".nix") + (builtins.attrNames (config."home" or {})))) + nixDarwinMachines; homeConfigurations = - nixpkgs.lib.foldAttrs - (acc: x: acc) - [] - (builtins.attrValues - (builtins.mapAttrs - (system: hosts: - nixpkgs.lib.attrsets.filterAttrs - (host: config: config != null) - (builtins.mapAttrs - (host: config: - if (hasFiles [ "home.nix" ] config) - then mkNixOnDroidHost system host - else null) - hosts)) - homeManagerMachines)); + createConfigurations + (system: host: config: + hasFiles + [ "home.nix" ] + config) + (system: host: config: + mkHomeManagerHost + system + host) + homeManagerMachines; }; }