1
0
Fork 0
mirror of https://github.com/nix-community/home-manager.git synced 2025-12-06 17:11:03 +01:00

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 committed by Weathercold
parent c7fdb7e90b
commit f8429e7ed6

View file

@ -6,6 +6,9 @@
... ...
}: }:
let
cfg = config.specialisation;
in
{ {
imports = [ (lib.mkRenamedOptionModule [ "specialization" ] [ "specialisation" ]) ]; imports = [ (lib.mkRenamedOptionModule [ "specialization" ] [ "specialisation" ]) ];
@ -39,12 +42,22 @@
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.
@ -79,8 +92,15 @@
''; '';
}; };
config = lib.mkIf (config.specialisation != { }) { config = lib.mkIf (cfg != { }) {
assertions = map (n: { assertions =
[
{
assertion = count (s: s.default) (attrValues cfg) <= 1;
message = "There can only be one default specialisation";
}
]
++ map (n: {
assertion = !lib.hasInfix "/" n; assertion = !lib.hasInfix "/" n;
message = "<name> in specialisation.<name> cannot contain a forward slash."; message = "<name> in specialisation.<name> cannot contain a forward slash.";
}) (lib.attrNames config.specialisation); }) (lib.attrNames config.specialisation);
@ -96,7 +116,17 @@
in in
'' ''
mkdir $out/specialisation mkdir $out/specialisation
${lib.concatStringsSep "\n" (lib.mapAttrsToList link config.specialisation)} ${lib.concatStringsSep "\n" (lib.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
'';
});
}; };
} }