From 9a3a105ee227125fec5b6311180f789192063dd2 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Thu, 7 Dec 2023 21:31:46 +0000 Subject: [PATCH 1/5] flake: add hosts This will allow other modules to cross-reference which hosts exist on which system. My main use-case is to automatically declare home-manager configuration for the home configuration of NixOS hosts. I also include Darwin in case I ever want to use that in the future, though that is unlikely for the moment. --- flake/default.nix | 1 + flake/hosts.nix | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 flake/hosts.nix diff --git a/flake/default.nix b/flake/default.nix index 65102e1..e4b2e8f 100644 --- a/flake/default.nix +++ b/flake/default.nix @@ -13,6 +13,7 @@ flake-parts.lib.mkFlake { inherit inputs; } { ./checks.nix ./dev-shells.nix ./home-manager.nix + ./hosts.nix ./lib.nix ./nixos.nix ./overlays.nix diff --git a/flake/hosts.nix b/flake/hosts.nix new file mode 100644 index 0000000..7d95fdc --- /dev/null +++ b/flake/hosts.nix @@ -0,0 +1,21 @@ +# Define `hosts.{darwin,home,nixos}` options for consumption in other modules +{ lib, ... }: +let + mkHostsOption = description: lib.mkOption { + inherit description; + type = with lib.types; attrsOf str; + default = { }; + example = { name = "x86_64-linux"; }; + }; +in +{ + options = { + hosts = { + darwin = mkHostsOption "Darwin hosts"; + + homes = mkHostsOption "Home Manager hosts"; + + nixos = mkHostsOption "NixOS hosts"; + }; + }; +} From 1c5a775da10371250b04dc24a3b5b08216afecb7 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Thu, 7 Dec 2023 21:33:10 +0000 Subject: [PATCH 2/5] flake: nixos: use 'hosts' option --- flake/nixos.nix | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/flake/nixos.nix b/flake/nixos.nix index b48b551..97d849d 100644 --- a/flake/nixos.nix +++ b/flake/nixos.nix @@ -1,4 +1,4 @@ -{ self, inputs, lib, ... }: +{ self, config, inputs, lib, ... }: let defaultModules = [ { @@ -28,8 +28,12 @@ let }; in { - flake.nixosConfigurations = lib.mapAttrs buildHost { - aramis = "x86_64-linux"; - porthos = "x86_64-linux"; + config = { + hosts.nixos = { + aramis = "x86_64-linux"; + porthos = "x86_64-linux"; + }; + + flake.nixosConfigurations = lib.mapAttrs buildHost config.hosts.nixos; }; } From 06492fa1383375f4e399b6ab5635421301157438 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Thu, 7 Dec 2023 21:41:05 +0000 Subject: [PATCH 3/5] flake: home-manager: use 'hosts' option --- flake/home-manager.nix | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/flake/home-manager.nix b/flake/home-manager.nix index 34af375..d26d70c 100644 --- a/flake/home-manager.nix +++ b/flake/home-manager.nix @@ -1,5 +1,7 @@ -{ self, inputs, lib, ... }: +{ self, config, inputs, lib, ... }: let + inherit (config) hosts; + defaultModules = [ # Include generic settings "${self}/modules/home" @@ -39,18 +41,19 @@ let }; }; - homes = { +in +{ + hosts.homes = { "ambroisie@bazin" = "x86_64-linux"; "ambroisie@mousqueton" = "x86_64-linux"; }; -in -{ + perSystem = { system, ... }: { # Work-around for https://github.com/nix-community/home-manager/issues/3075 legacyPackages = { homeConfigurations = let - filteredHomes = lib.filterAttrs (_: v: v == system) homes; + filteredHomes = lib.filterAttrs (_: v: v == system) hosts.homes; allHomes = filteredHomes // { # Default configuration ambroisie = system; From f73212de38a9fe97c01da864d183d736e0f6bf1a Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Thu, 7 Dec 2023 21:44:01 +0000 Subject: [PATCH 4/5] flake: home-manager: refactor 'mkHome' This will allow making a similar function for NixOS homes. --- flake/home-manager.nix | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/flake/home-manager.nix b/flake/home-manager.nix index d26d70c..5a1fa87 100644 --- a/flake/home-manager.nix +++ b/flake/home-manager.nix @@ -18,7 +18,7 @@ let } ]; - mkHome = name: system: inputs.home-manager.lib.homeManagerConfiguration { + mkHomeCommon = mainModule: system: inputs.home-manager.lib.homeManagerConfiguration { # Work-around for home-manager # * not letting me set `lib` as an extraSpecialArgs # * not respecting `nixpkgs.overlays` [1] @@ -31,9 +31,7 @@ let ]; }; - modules = defaultModules ++ [ - "${self}/hosts/homes/${name}" - ]; + modules = defaultModules ++ [ mainModule ]; extraSpecialArgs = { # Inject inputs to use them in global registry @@ -41,6 +39,7 @@ let }; }; + mkHome = name: mkHomeCommon "${self}/hosts/homes/${name}"; in { hosts.homes = { From f7e91dc1b969dc14aaaf28dc2680c99daa8389ab Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Thu, 7 Dec 2023 21:56:25 +0000 Subject: [PATCH 5/5] WIP: flake: home-manager: export NixOS homes WIP: this does not take into account the `profiles` which *also* setup home-manager modules... And here is what the last few commits were building up to. This is neat, but won't be useful *very* often. --- flake/home-manager.nix | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/flake/home-manager.nix b/flake/home-manager.nix index 5a1fa87..2e385c6 100644 --- a/flake/home-manager.nix +++ b/flake/home-manager.nix @@ -40,6 +40,8 @@ let }; mkHome = name: mkHomeCommon "${self}/hosts/homes/${name}"; + + mkNixosHome = name: mkHomeCommon "${self}/hosts/nixos/${name}/home.nix"; in { hosts.homes = { @@ -57,8 +59,17 @@ in # Default configuration ambroisie = system; }; + homeManagerHomes = lib.mapAttrs mkHome allHomes; + + filteredNixosHosts = lib.filterAttrs (_: v: v == system) hosts.nixos; + nixosHomes' = lib.mapAttrs mkNixosHome filteredNixosHosts; + nixosHomes = lib.mapAttrs' (host: lib.nameValuePair "ambroisie@${host}") nixosHomes'; in - lib.mapAttrs mkHome allHomes; + lib.foldl' lib.mergeAttrs { } + [ + homeManagerHomes + nixosHomes + ]; }; }; }