specialisation: add default specialisation option

Add the option `specialisation.<name>.default`, which activates this
specialisation by default.
This commit is contained in:
Weathercold 2022-10-08 16:29:25 -04:00
parent 304a011325
commit 01a518ba06
No known key found for this signature in database
GPG key ID: 059102E35214D867

View file

@ -2,7 +2,8 @@
with lib; with lib;
{ let cfg = config.specialisation;
in {
imports = imports =
[ (mkRenamedOptionModule [ "specialization" ] [ "specialisation" ]) ]; [ (mkRenamedOptionModule [ "specialization" ] [ "specialisation" ]) ];
@ -10,7 +11,8 @@ with lib;
type = types.attrsOf (types.submodule { type = types.attrsOf (types.submodule {
options = { options = {
configuration = mkOption { configuration = mkOption {
type = let type =
let
extended = extendModules { extended = extendModules {
modules = [{ modules = [{
# Prevent infinite recursion # Prevent infinite recursion
@ -24,18 +26,29 @@ with lib;
_module.args.name = mkForce name; _module.args.name = mkForce name;
}]; }];
}; };
in extended.type; in
extended.type;
default = { }; default = { };
visible = "shallow"; visible = "shallow";
description = '' description = ''
Arbitrary Home Manager configuration settings. Arbitrary Home Manager configuration settings.
''; '';
}; };
default = mkOption {
type = types.bool;
default = false;
description = ''
Whether this specialisation is activated by default.
Note that setting this option will override the default activation
script, making it impossible to activate the default
configuration.
'';
};
}; };
}); });
default = { }; default = { };
description = '' description = ''
A set of named specialized configurations. These can be used to extend A set of named specialised configurations. These can be used to extend
your base configuration with additional settings. For example, you can your base configuration with additional settings. For example, you can
have specialisations named "light" and "dark" have specialisations named "light" and "dark"
that apply light and dark color theme configurations. that apply light and dark color theme configurations.
@ -70,14 +83,31 @@ with lib;
''; '';
}; };
config = mkIf (config.specialisation != { }) { config = mkIf (cfg != { }) {
home.extraBuilderCommands = let assertions = [{
assertion = count (s: s.default) (attrValues cfg) <= 1;
message = "There can only be one default specialisation";
}];
home.extraBuilderCommands =
let
link = n: v: link = n: v:
let pkg = v.configuration.home.activationPackage; let pkg = v.configuration.home.activationPackage;
in "ln -s ${pkg} $out/specialisation/${n}"; in "ln -s ${pkg} $out/specialisation/${n}";
in '' in
''
mkdir $out/specialisation mkdir $out/specialisation
${concatStringsSep "\n" (mapAttrsToList link config.specialisation)} ${concatStringsSep "\n" (mapAttrsToList link cfg)}
''; '';
home.activation =
let
defaultSpecialisation = findFirst (s: s.default) null (attrValues cfg);
in
mkIf (defaultSpecialisation != null) (mkForce {
activateSpecialisation = ''
${defaultSpecialisation.configuration.home.activationPackage}/activate
'';
});
}; };
} }