buildClan function: export all machines via nixosModules/darwinModules
We want each machine not only to be exposed via nixosConfigurations but also as a module. This allows re-importing the machine in tests and override the architecture for example.
This commit is contained in:
@@ -100,7 +100,7 @@ are loaded when using Clan:
|
||||
};
|
||||
in
|
||||
{
|
||||
inherit (clan) nixosConfigurations clanInternals;
|
||||
inherit (clan) nixosConfigurations nixosModules clanInternals;
|
||||
|
||||
# elided for brevity
|
||||
};
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -26,7 +26,4 @@
|
||||
inherit name;
|
||||
};
|
||||
};
|
||||
|
||||
# TODO: move into nixosModules
|
||||
networking.hostName = lib.mkDefault name;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -14,6 +14,8 @@
|
||||
topLevel = [
|
||||
"clanInternals"
|
||||
"nixosConfigurations"
|
||||
"nixosModules"
|
||||
"darwinConfigurations"
|
||||
"darwinModules"
|
||||
];
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -62,6 +62,6 @@
|
||||
in
|
||||
{
|
||||
clan = clan_attrs_json;
|
||||
inherit (clan) nixosConfigurations clanInternals;
|
||||
inherit (clan) nixosConfigurations nixosModules clanInternals;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -44,6 +44,6 @@
|
||||
};
|
||||
in
|
||||
{
|
||||
inherit (clan) nixosConfigurations clanInternals;
|
||||
inherit (clan) nixosConfigurations nixosModules clanInternals;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -19,6 +19,6 @@
|
||||
};
|
||||
in
|
||||
{
|
||||
inherit (clan) nixosConfigurations clanInternals;
|
||||
inherit (clan) nixosConfigurations nixosModules clanInternals;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -10,6 +10,6 @@
|
||||
in
|
||||
{
|
||||
# all machines managed by Clan
|
||||
inherit (clan) nixosConfigurations clanInternals;
|
||||
inherit (clan) nixosConfigurations nixosModules clanInternals;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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 =
|
||||
|
||||
Reference in New Issue
Block a user