diff --git a/docs/site/guides/getting-started/secrets.md b/docs/site/guides/getting-started/secrets.md index 0fa9bb710..5d13559b0 100644 --- a/docs/site/guides/getting-started/secrets.md +++ b/docs/site/guides/getting-started/secrets.md @@ -100,7 +100,7 @@ are loaded when using Clan: }; in { - inherit (clan) nixosConfigurations clanInternals; + inherit (clan) nixosConfigurations nixosModules clanInternals; # elided for brevity }; diff --git a/lib/build-clan/interface.nix b/lib/build-clan/interface.nix index f8fa919ba..107806707 100644 --- a/lib/build-clan/interface.nix +++ b/lib/build-clan/interface.nix @@ -162,6 +162,32 @@ in default = { }; }; + nixosModules = lib.mkOption { + # Hide from documentation. + # Exposed at the top-level of the flake, clan.nixosModules should not used by the user. + # Instead, the user should use the `.#nixosModules` attribute of the flake output. + visible = false; + type = types.lazyAttrsOf types.raw; + default = { }; + description = '' + NixOS modules that are generated by clan. + These are used to generate the `nixosConfigurations`. + ''; + }; + + darwinModules = lib.mkOption { + # Hide from documentation. + # Exposed at the top-level of the flake, clan.darwinModules should not used by the user. + # Instead, the user should use the `.#darwinModules` attribute of the flake output. + visible = false; + type = types.lazyAttrsOf types.raw; + default = { }; + description = '' + Darwin modules that are generated by clan. + These are used to generate the `darwinConfigurations`. + ''; + }; + # flake.clanInternals clanInternals = lib.mkOption { # Hide from documentation. Exposes internals to the cli. diff --git a/lib/build-clan/machineModules/forName.nix b/lib/build-clan/machineModules/forName.nix index 06dc4bf11..e537ba55d 100644 --- a/lib/build-clan/machineModules/forName.nix +++ b/lib/build-clan/machineModules/forName.nix @@ -26,7 +26,4 @@ inherit name; }; }; - - # TODO: move into nixosModules - networking.hostName = lib.mkDefault name; } diff --git a/lib/build-clan/module.nix b/lib/build-clan/module.nix index 589fa89b8..7ccd5c3ed 100644 --- a/lib/build-clan/module.nix +++ b/lib/build-clan/module.nix @@ -7,11 +7,16 @@ ... }: let + inherit (lib) + flip + mapAttrs' + ; + inherit (config) directory + inventory pkgsForSystem specialArgs - inventory ; inherit (clan-core.clanLib.inventory) buildInventory; @@ -75,6 +80,31 @@ let } ) allMachines; + # Expose reusable modules these can be imported or wrapped or instantiated + # - by the user + # - by some test frameworks + # IMPORTANT!: It is utterly important that we don't add any logic outside of these modules, as it would get tested. + nixosModules' = lib.filterAttrs ( + name: _: inventory.machines.${name}.machineClass or "nixos" == "nixos" + ) (config.outputs.moduleForMachine); + darwinModules' = lib.filterAttrs ( + name: _: inventory.machines.${name}.machineClass or "nixos" == "darwin" + ) (config.outputs.moduleForMachine); + + nixosModules = flip mapAttrs' nixosModules' ( + name: machineModule: { + name = "clan-machine-${name}"; + value = machineModule; + } + ); + + darwinModules = flip mapAttrs' darwinModules' ( + name: machineModule: { + name = "clan-machine-${name}"; + value = machineModule; + } + ); + nixosConfigurations = lib.filterAttrs (name: _: machineClasses.${name} == "nixos") configurations; darwinConfigurations = lib.filterAttrs (name: _: machineClasses.${name} == "darwin") configurations; @@ -180,6 +210,9 @@ in # - darwinModules (_class = darwin) (lib.optionalAttrs (clan-core ? "${_class}Modules") clan-core."${_class}Modules".clanCore) ] ++ lib.optionals (_class == "nixos") (v.machineImports or [ ]); + + # default hostname + networking.hostName = lib.mkDefault name; } ) ) inventoryClass.machines) @@ -193,6 +226,10 @@ in self = lib.mkDefault config.self; }; + # expose all machines as modules for re-use + inherit nixosModules; + inherit darwinModules; + # Ready to use configurations # These are only shallow wrapping the 'nixosModules' or 'darwinModules' with # lib.nixosSystem @@ -200,17 +237,6 @@ in inherit darwinConfigurations; clanInternals = { - # Expose reusable modules these can be imported or wrapped or instantiated - # - by the user - # - by some test frameworks - # IMPORTANT!: It is utterly important that we don't add any logic outside of these modules, as it would get tested. - nixosModules = lib.filterAttrs ( - name: _: inventory.machines.${name}.machineClass or "nixos" == "nixos" - ) (config.outputs.moduleForMachine); - darwinModules = lib.filterAttrs ( - name: _: inventory.machines.${name}.machineClass or "nixos" == "darwin" - ) (config.outputs.moduleForMachine); - inherit inventoryClass; # Endpoint that can be called to get a service schema diff --git a/lib/build-clan/public.nix b/lib/build-clan/public.nix index 3f1783a9a..cc302ac49 100644 --- a/lib/build-clan/public.nix +++ b/lib/build-clan/public.nix @@ -14,6 +14,8 @@ topLevel = [ "clanInternals" "nixosConfigurations" + "nixosModules" "darwinConfigurations" + "darwinModules" ]; } diff --git a/lib/build-clan/tests.nix b/lib/build-clan/tests.nix index 875f6e538..ee0f14080 100644 --- a/lib/build-clan/tests.nix +++ b/lib/build-clan/tests.nix @@ -157,6 +157,24 @@ in ]; }; + test_machines_are_modules = + let + result = buildClan { + self = { + inputs = { }; + }; + directory = ../../.; + meta.name = "test-clan-core"; + }; + in + { + expr = builtins.attrNames result.nixosModules; + expected = [ + "clan-machine-test-backup" + "clan-machine-test-inventory-machine" + ]; + }; + test_buildClan_all_machines = let result = buildClan { diff --git a/lib/clanTest/flake-module.nix b/lib/clanTest/flake-module.nix index 9446647aa..cbbace26a 100644 --- a/lib/clanTest/flake-module.nix +++ b/lib/clanTest/flake-module.nix @@ -6,13 +6,14 @@ }: let inherit (lib) + flatten + flip + mapAttrs' + mapAttrsToList mkOption removePrefix types - mapAttrsToList - flip unique - flatten ; clanLib = config.flake.clanLib; @@ -143,7 +144,12 @@ in # Inherit all nodes from the clan # i.e. nodes.jon <- clan.machines.jon # clanInternals.nixosModules contains nixosModules per node - nodes = clanFlakeResult.clanInternals.nixosModules; + nodes = flip mapAttrs' clanFlakeResult.nixosModules ( + name: machineModule: { + name = removePrefix "clan-machine-" name; + value = machineModule; + } + ); # !WARNING: Write a detailed comment if adding new options here # We should be very careful about adding new options here because it affects all tests diff --git a/pkgs/clan-cli/clan_cli/tests/test_flake_with_core/flake.nix b/pkgs/clan-cli/clan_cli/tests/test_flake_with_core/flake.nix index 35a11821a..f09d2d200 100644 --- a/pkgs/clan-cli/clan_cli/tests/test_flake_with_core/flake.nix +++ b/pkgs/clan-cli/clan_cli/tests/test_flake_with_core/flake.nix @@ -62,6 +62,6 @@ in { clan = clan_attrs_json; - inherit (clan) nixosConfigurations clanInternals; + inherit (clan) nixosConfigurations nixosModules clanInternals; }; } diff --git a/pkgs/clan-cli/clan_cli/tests/test_flake_with_core_and_pass/flake.nix b/pkgs/clan-cli/clan_cli/tests/test_flake_with_core_and_pass/flake.nix index 73a1b4269..463bf4cf9 100644 --- a/pkgs/clan-cli/clan_cli/tests/test_flake_with_core_and_pass/flake.nix +++ b/pkgs/clan-cli/clan_cli/tests/test_flake_with_core_and_pass/flake.nix @@ -44,6 +44,6 @@ }; in { - inherit (clan) nixosConfigurations clanInternals; + inherit (clan) nixosConfigurations nixosModules clanInternals; }; } diff --git a/pkgs/clan-cli/clan_cli/tests/test_flake_with_core_dynamic_machines/flake.nix b/pkgs/clan-cli/clan_cli/tests/test_flake_with_core_dynamic_machines/flake.nix index c68f6491e..e6b4fb02a 100644 --- a/pkgs/clan-cli/clan_cli/tests/test_flake_with_core_dynamic_machines/flake.nix +++ b/pkgs/clan-cli/clan_cli/tests/test_flake_with_core_dynamic_machines/flake.nix @@ -19,6 +19,6 @@ }; in { - inherit (clan) nixosConfigurations clanInternals; + inherit (clan) nixosConfigurations nixosModules clanInternals; }; } diff --git a/templates/clan/minimal/flake.nix b/templates/clan/minimal/flake.nix index 868e304a6..bc7e4b895 100644 --- a/templates/clan/minimal/flake.nix +++ b/templates/clan/minimal/flake.nix @@ -10,6 +10,6 @@ in { # all machines managed by Clan - inherit (clan) nixosConfigurations clanInternals; + inherit (clan) nixosConfigurations nixosModules clanInternals; }; } diff --git a/templates/clan/new-clan/flake.nix b/templates/clan/new-clan/flake.nix index 3459ee14c..846a81081 100644 --- a/templates/clan/new-clan/flake.nix +++ b/templates/clan/new-clan/flake.nix @@ -26,7 +26,7 @@ }; in { - inherit (clan) nixosConfigurations clanInternals; + inherit (clan) nixosConfigurations nixosModules clanInternals; # Add the Clan cli tool to the dev shell. # Use "nix develop" to enter the dev shell. devShells =