Merge pull request 'refactor: clan expose the clan module as is' (#4101) from hsjobeki/clan into main

Reviewed-on: https://git.clan.lol/clan/clan-core/pulls/4101
This commit is contained in:
hsjobeki
2025-06-26 11:44:24 +00:00
4 changed files with 76 additions and 41 deletions

View File

@@ -11,45 +11,70 @@ let
buildClanModule = clan-core.clanLib.buildClanModule;
publicAttrs = import ../lib/modules/public.nix;
# Create output options only for listed attributes
# TODO: Refactor this into an explicit module, so we can have description and other attributes to be listed in flake-parts
outputModule = {
clan = lib.genAttrs publicAttrs.clan (
name: config.clan.${name} or (throw "Output: clan.${name} not found.")
);
topLevel = {
options = lib.genAttrs publicAttrs.topLevel (_: lib.mkOption { });
config = lib.genAttrs publicAttrs.topLevel (
name: config.clan.${name} or (throw "Output: clan.${name} not found.")
in
{
# Backwards compatibility
imports = [
(lib.mkRenamedOptionModule [ "clan" ] [ "flake" "clan" ])
];
# Our module is completely public, so we dont need to map it
# Mapped top level outputs
options.flake = {
# Backwards compat
clanInternals = lib.mkOption {
description = "Internals as used by the clan cli. Deprecated use clan.clanInternals";
visible = false;
readOnly = true;
default = config.flake.clan.clanInternals;
apply = lib.warn "Use clan.clanInternals instead";
};
# The one and only clan module
clan = lib.mkOption {
description = "The evaluated clan module";
default = { };
type = types.submoduleWith {
specialArgs = {
inherit clan-core self;
inherit (inputs) nixpkgs nix-darwin;
# TODO: inject the inventory interface
# inventoryInterface = {};
};
modules = [
buildClanModule.flakePartsModule
];
};
};
# Mapped flake toplevel outputs
darwinConfigurations = lib.mkOption {
type = types.lazyAttrsOf types.raw;
description = "darwinConfigurations produced by clan for a specific machine";
apply = lib.mapAttrs (
k: v: {
_file = "#nixosModules.${k}";
imports = [ v ];
}
);
};
darwinModules = lib.mkOption {
type = types.lazyAttrsOf types.deferredModule;
description = "darwinModules produced by clan for a specific machine";
apply = lib.mapAttrs (
k: v: {
_file = "#nixosModules.${k}";
imports = [ v ];
}
);
};
};
in
{
options.clan = lib.mkOption {
default = { };
type = types.submoduleWith {
specialArgs = {
inherit clan-core self;
inherit (inputs) nixpkgs nix-darwin;
# TODO: inject the inventory interface
# inventoryInterface = {};
};
modules = [
buildClanModule.flakePartsModule
];
};
};
options.flake = {
clan = lib.mkOption { type = types.raw; };
} // outputModule.topLevel.options;
config = {
flake = {
clan = outputModule.clan;
} // outputModule.topLevel.config;
# Use normal prio, to allow merging with user values
config.flake = {
inherit (config.flake.clan)
nixosConfigurations
nixosModules
darwinConfigurations
darwinModules
;
};
_file = __curPos.file;

View File

@@ -34,6 +34,7 @@
Public attributes of buildClan. As specified in publicAttrs.
*/
buildClanWith =
{
clan-core,
# TODO: Below should be module options such that the user can override them?

View File

@@ -1,6 +1,7 @@
{
self,
self',
lib,
pkgs,
flakeOptions,
...
@@ -23,7 +24,19 @@ let
_module.args = { inherit (self) clanLib; };
});
clanSchema = jsonLib.parseOptions (flakeOptions.clan.type.getSubOptions [ "clan" ]) { };
opts = (flakeOptions.flake.type.getSubOptions [ "flake" ]);
clanOpts = opts.clan.type.getSubOptions [ "clan" ];
include = [
"directory"
"inventory"
"machines"
"meta"
"modules"
"outputs"
"secrets"
"templates"
];
clanSchema = jsonLib.parseOptions (lib.filterAttrs (n: _v: lib.elem n include) clanOpts) { };
renderSchema = pkgs.writers.writePython3Bin "render-schema" {
flakeIgnore = [

View File

@@ -187,8 +187,6 @@ ClanMetaType = Unknown
ClanModulesType = dict[str, dict[str, Any] | list[Any] | bool | float | int | str | None]
ClanOutputsType = Output
ClanSecretsType = Secret
ClanSelfType = dict[str, Any] | list[Any] | bool | float | int | str
ClanSpecialargsType = dict[str, dict[str, Any] | list[Any] | bool | float | int | str | None]
ClanTemplatesType = Template
class Clan(TypedDict):
@@ -199,6 +197,4 @@ class Clan(TypedDict):
modules: NotRequired[ClanModulesType]
outputs: NotRequired[ClanOutputsType]
secrets: NotRequired[ClanSecretsType]
self: NotRequired[ClanSelfType]
specialArgs: NotRequired[ClanSpecialargsType]
templates: NotRequired[ClanTemplatesType]