From f8429e7ed6a8a16b7fc18ede54e4d493f847d1fc Mon Sep 17 00:00:00 2001 From: Weathercold Date: Sat, 8 Oct 2022 16:29:25 -0400 Subject: [PATCH] specialisation: add default specialisation option Add the option `specialisation..default`, which activates this specialisation by default. --- modules/misc/specialisation.nix | 44 +++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/modules/misc/specialisation.nix b/modules/misc/specialisation.nix index de07bc1bd..2ee6a2de5 100644 --- a/modules/misc/specialisation.nix +++ b/modules/misc/specialisation.nix @@ -6,6 +6,9 @@ ... }: +let + cfg = config.specialisation; +in { imports = [ (lib.mkRenamedOptionModule [ "specialization" ] [ "specialisation" ]) ]; @@ -39,12 +42,22 @@ 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 = { }; 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 have specialisations named "light" and "dark" that apply light and dark color theme configurations. @@ -79,11 +92,18 @@ ''; }; - config = lib.mkIf (config.specialisation != { }) { - assertions = map (n: { - assertion = !lib.hasInfix "/" n; - message = " in specialisation. cannot contain a forward slash."; - }) (lib.attrNames config.specialisation); + config = lib.mkIf (cfg != { }) { + assertions = + [ + { + assertion = count (s: s.default) (attrValues cfg) <= 1; + message = "There can only be one default specialisation"; + } + ] + ++ map (n: { + assertion = !lib.hasInfix "/" n; + message = " in specialisation. cannot contain a forward slash."; + }) (lib.attrNames config.specialisation); home.extraBuilderCommands = let @@ -96,7 +116,17 @@ in '' 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 + ''; + }); }; }