diff --git a/modules/home-manager/all-modules.nix b/modules/home-manager/all-modules.nix index 54fcfea3..822c1037 100644 --- a/modules/home-manager/all-modules.nix +++ b/modules/home-manager/all-modules.nix @@ -12,7 +12,7 @@ ./dunst.nix ./element-desktop.nix ./fcitx5.nix - ./firefox.nix + ./firefox ./fish.nix ./foot.nix ./freetube.nix diff --git a/modules/home-manager/firefox.nix b/modules/home-manager/firefox.nix deleted file mode 100644 index e7ba20ff..00000000 --- a/modules/home-manager/firefox.nix +++ /dev/null @@ -1,136 +0,0 @@ -{ catppuccinLib }: -{ config, lib, ... }: -let - inherit (lib) - attrNames - foldl' - hasAttr - importJSON - toSentenceCase - mapAttrs - mkIf - mkOption - optional - getAttrFromPath - setAttrByPath - types - ; - - inherit (config.catppuccin) sources; - themes = importJSON "${sources.firefox}/themes.json"; - - mkFirefoxModule = - { - name, - prettyName ? toSentenceCase name, - hmModulePath ? [ - "programs" - name - ], - }: - let - modulePath = [ - "catppuccin" - name - ]; - - getAttrStringFromPath = lib.concatStringsSep "."; - - cfg = getAttrFromPath modulePath config; - firefoxCfg = getAttrFromPath hmModulePath config; - - mkProfileOptions = - { - forceDefault ? false, - enableDefault ? null, - }: - catppuccinLib.mkCatppuccinOption ( - { - inherit name; - accentSupport = true; - } - // (lib.optionalAttrs (enableDefault != null) { - default = enableDefault; - }) - ) - // { - force = mkOption { - type = types.bool; - default = forceDefault; - description = "Forcibly override any existing configuration for Firefox Color."; - example = true; - }; - }; - in - { - options = - (setAttrByPath modulePath ( - (mkProfileOptions { }) - // { - profiles = mkOption { - type = types.attrsOf ( - types.submodule { - options = mkProfileOptions { - enableDefault = cfg.enable; - forceDefault = cfg.force; - }; - config = { - flavor = lib.mkDefault cfg.flavor; - accent = lib.mkDefault cfg.accent; - }; - } - ); - default = mapAttrs (_: _: { }) firefoxCfg.profiles; - defaultText = ""; - description = "Catppuccin settings for ${prettyName} profiles."; - }; - } - )) - # home-manager browser config - // (setAttrByPath hmModulePath { - profiles = mkOption { - type = types.attrsOf ( - types.submodule ( - { name, ... }: - let - profile = cfg.profiles.${name} or { enable = false; }; - in - { - config = mkIf profile.enable { - extensions = { - settings."FirefoxColor@mozilla.com" = { - inherit (profile) force; - settings = { - firstRunDone = true; - theme = themes.${profile.flavor}.${profile.accent}; - }; - }; - }; - }; - } - ) - ); - }; - }); - - config = { - warnings = foldl' ( - acc: name: - acc - ++ - optional (!(hasAttr name firefoxCfg.profiles)) - "${prettyName} profile '${name}' is defined in '${getAttrStringFromPath modulePath}', but not '${getAttrStringFromPath hmModulePath}'. This will have no effect." - ) [ ] (attrNames cfg.profiles); - }; - }; -in -{ - imports = map mkFirefoxModule [ - { name = "firefox"; } - { - name = "librewolf"; - prettyName = "LibreWolf"; # W in LibreWolf is uppercase - } - { name = "floorp"; } - ]; -} diff --git a/modules/home-manager/firefox/default.nix b/modules/home-manager/firefox/default.nix new file mode 100644 index 00000000..91fdd906 --- /dev/null +++ b/modules/home-manager/firefox/default.nix @@ -0,0 +1,25 @@ +{ catppuccinLib }: +{ lib, ... }: + +let + mkFirefoxModule = + args: + + lib.modules.importApply ./mkFirefoxModule.nix ( + args + // { + inherit catppuccinLib; + } + ); +in + +{ + imports = map mkFirefoxModule [ + { name = "firefox"; } + { + name = "librewolf"; + prettyName = "LibreWolf"; # W in LibreWolf is uppercase + } + { name = "floorp"; } + ]; +} diff --git a/modules/home-manager/firefox/mkFirefoxModule.nix b/modules/home-manager/firefox/mkFirefoxModule.nix new file mode 100644 index 00000000..5ee59897 --- /dev/null +++ b/modules/home-manager/firefox/mkFirefoxModule.nix @@ -0,0 +1,132 @@ +{ + catppuccinLib, + + name, + prettyName ? null, + hmModulePath ? [ + "programs" + name + ], +}: +{ config, lib, ... }: + +let + inherit (lib) + attrNames + const + foldl' + genAttrs + hasAttr + importJSON + toSentenceCase + optional + mkIf + mkOption + getAttrFromPath + setAttrByPath + types + ; + + getAttrStringFromPath = lib.concatStringsSep "."; + + inherit (config.catppuccin) sources; + themes = importJSON "${sources.firefox}/themes.json"; + + cfg = getAttrFromPath modulePath config; + firefoxCfg = getAttrFromPath hmModulePath config; + + modulePath = [ + "catppuccin" + name + ]; + prettyName' = if prettyName == null then toSentenceCase name else prettyName; + + optionsModule = { + options = + catppuccinLib.mkCatppuccinOption { + inherit name; + accentSupport = true; + } + // { + force = mkOption { + type = types.bool; + default = false; + description = "Forcibly override any existing configuration for Firefox Color."; + example = true; + }; + }; + }; + + catppuccinProfilesSubmodule = { + imports = [ optionsModule ]; + + config = lib.mapAttrs (lib.const lib.mkDefault) { + inherit (cfg) + enable + flavor + accent + force + ; + }; + }; + + catppuccinOptions = setAttrByPath modulePath ( + lib.mkOption { + type = lib.types.submodule { + imports = [ optionsModule ]; + + options = { + profiles = mkOption { + type = types.attrsOf (types.submodule catppuccinProfilesSubmodule); + default = genAttrs (attrNames firefoxCfg.profiles) (const { }); + defaultText = ""; + description = "Catppuccin settings for ${prettyName'} profiles."; + }; + }; + }; + default = { }; + description = "Catppuccin settings for ${prettyName'}."; + } + ); + + firefoxProfilesSubmodule = + { name, ... }: + + let + profile = cfg.profiles.${name} or { enable = false; }; + in + + { + config = mkIf profile.enable { + extensions = { + settings."FirefoxColor@mozilla.com" = { + inherit (profile) force; + settings = { + firstRunDone = true; + theme = themes.${profile.flavor}.${profile.accent}; + }; + }; + }; + }; + }; + + homeOptions = setAttrByPath hmModulePath { + profiles = mkOption { + type = types.attrsOf (types.submodule firefoxProfilesSubmodule); + }; + }; +in + +{ + options = catppuccinOptions // homeOptions; + + config = { + warnings = foldl' ( + acc: name: + acc + ++ + optional (!(hasAttr name firefoxCfg.profiles)) + "${prettyName'} profile '${name}' is defined in '${getAttrStringFromPath modulePath}', but not '${getAttrStringFromPath hmModulePath}'. This will have no effect." + ) [ ] (attrNames cfg.profiles); + }; +}