From be6320c4b16bc9ee8ee3e81e07bb7257ebef9063 Mon Sep 17 00:00:00 2001 From: seth Date: Mon, 17 Apr 2023 12:44:07 -0400 Subject: [PATCH] feat(modules): add util library (#25) --- modules/home-manager/alacritty.nix | 22 ++------ modules/home-manager/bat.nix | 10 +--- modules/home-manager/bottom.nix | 10 +--- modules/home-manager/btop.nix | 10 +--- modules/home-manager/default.nix | 41 +++++++------- modules/home-manager/gtk.nix | 30 +++-------- modules/home-manager/helix.nix | 11 ++-- modules/home-manager/kitty.nix | 19 ++----- modules/home-manager/polybar.nix | 10 +--- modules/home-manager/starship.nix | 10 +--- modules/home-manager/tmux.nix | 10 +--- modules/lib/default.nix | 86 ++++++++++++++++++++++++++++++ modules/lib/mkExtLib.nix | 1 + modules/nixos/default.nix | 20 ++++--- 14 files changed, 152 insertions(+), 138 deletions(-) create mode 100644 modules/lib/default.nix create mode 100644 modules/lib/mkExtLib.nix diff --git a/modules/home-manager/alacritty.nix b/modules/home-manager/alacritty.nix index 80d875a..89bcb2e 100644 --- a/modules/home-manager/alacritty.nix +++ b/modules/home-manager/alacritty.nix @@ -1,29 +1,13 @@ { config, pkgs, lib, ... }: let cfg = config.programs.alacritty.catppuccin; in { - options.programs.alacritty.catppuccin = with lib; { - enable = mkEnableOption "Catppuccin theme"; - flavour = mkOption { - type = types.enum [ "latte" "frappe" "macchiato" "mocha" ]; - default = config.catppuccin.flavour; - description = "Catppuccin flavour for alacritty"; - }; - }; + options.programs.alacritty.catppuccin = + lib.ctp.mkCatppuccinOpt "alacritty" config; config.programs.alacritty.settings = with builtins; with lib; with pkgs; let - # path -> a - # fromJSON but for yaml - fromYaml = file: - let - # convert to json - json = runCommand "converted.json" { } '' - ${yj}/bin/yj < ${file} > $out - ''; - in fromJSON (readFile json); - file = fetchFromGitHub { owner = "catppuccin"; repo = "alacritty"; @@ -31,5 +15,5 @@ in { sha256 = "sha256-w9XVtEe7TqzxxGUCDUR9BFkzLZjG8XrplXJ3lX6f+x0="; } + "/catppuccin-${cfg.flavour}.yml"; - in mkIf cfg.enable (fromYaml file); + in mkIf cfg.enable (ctp.fromYaml pkgs file); } diff --git a/modules/home-manager/bat.nix b/modules/home-manager/bat.nix index 84f9c46..b64f23b 100644 --- a/modules/home-manager/bat.nix +++ b/modules/home-manager/bat.nix @@ -1,14 +1,8 @@ { config, pkgs, lib, ... }: let cfg = config.programs.bat.catppuccin; in { - options.programs.bat.catppuccin = { - enable = lib.mkEnableOption "Catppuccin theme"; - flavour = lib.mkOption { - type = lib.types.enum [ "latte" "frappe" "macchiato" "mocha" ]; - default = config.catppuccin.flavour; - description = "Catppuccin flavour for bat"; - }; - }; + options.programs.bat.catppuccin = + lib.ctp.mkCatppuccinOpt "bat" config; config = { home.activation.batCache = "${pkgs.bat}/bin/bat cache --build"; diff --git a/modules/home-manager/bottom.nix b/modules/home-manager/bottom.nix index 48fc156..bd110f1 100644 --- a/modules/home-manager/bottom.nix +++ b/modules/home-manager/bottom.nix @@ -1,14 +1,8 @@ { config, pkgs, lib, ... }: let cfg = config.programs.bottom.catppuccin; in { - options.programs.bottom.catppuccin = with lib; { - enable = mkEnableOption "Catppuccin theme"; - flavour = mkOption { - type = types.enum [ "latte" "frappe" "macchiato" "mocha" ]; - default = config.catppuccin.flavour; - description = "Catppuccin flavour for bottom"; - }; - }; + options.programs.bottom.catppuccin = + lib.ctp.mkCatppuccinOpt "bottom" config; config.programs.bottom.settings = with builtins; with lib; diff --git a/modules/home-manager/btop.nix b/modules/home-manager/btop.nix index 29d2994..919c5d9 100644 --- a/modules/home-manager/btop.nix +++ b/modules/home-manager/btop.nix @@ -9,14 +9,8 @@ let sha256 = "sha256-QoPPx4AzxJMYo/prqmWD/CM7e5vn/ueyx+XQ5+YfHF8="; } + themePath; in { - options.programs.btop.catppuccin = with lib; { - enable = mkEnableOption "Catppuccin theme"; - flavour = mkOption { - type = types.enum [ "latte" "frappe" "macchiato" "mocha" ]; - default = config.catppuccin.flavour; - description = "Catppuccin flavour for btop"; - }; - }; + options.programs.btop.catppuccin = + lib.ctp.mkCatppuccinOpt "btop" config; # xdg is required for this to work config.xdg.enable = with lib; mkIf cfg.enable (mkForce true); diff --git a/modules/home-manager/default.nix b/modules/home-manager/default.nix index 3adaf04..03c1c51 100644 --- a/modules/home-manager/default.nix +++ b/modules/home-manager/default.nix @@ -1,24 +1,29 @@ -{ config, pkgs, lib, ... }: { - imports = [ - ./alacritty.nix - ./bat.nix - ./bottom.nix - ./btop.nix - ./kitty.nix - ./starship.nix - ./helix.nix - ./gtk.nix - ./polybar.nix - ./tmux.nix - ]; - options.catppuccin = { - flavour = lib.mkOption { - type = lib.types.enum [ "latte" "frappe" "macchiato" "mocha" ]; +{ config, pkgs, lib, ... }: let + extendedLib = import ../lib/mkExtLib.nix lib; +in { + imports = let + files = [ + ./alacritty.nix + ./bat.nix + ./bottom.nix + ./btop.nix + ./kitty.nix + ./starship.nix + ./helix.nix + ./gtk.nix + ./polybar.nix + ./tmux.nix + ]; + in extendedLib.ctp.mapModules config pkgs extendedLib files; + + options.catppuccin = with extendedLib; { + flavour = mkOption { + type = ctp.types.flavourOption; default = "latte"; description = "Global Catppuccin flavour"; }; - accent = lib.mkOption { - type = lib.types.enum [ "blue" "flamingo" "green" "lavender" "maroon" "mauve" "peach" "pink" "red" "rosewater" "sapphire" "sky" "teal" "yellow" ]; + accent = mkOption { + type = ctp.types.accentOption; default = "teal"; description = "Global Catppuccin accent"; }; diff --git a/modules/home-manager/gtk.nix b/modules/home-manager/gtk.nix index 9c7d8c8..d4c5c7f 100644 --- a/modules/home-manager/gtk.nix +++ b/modules/home-manager/gtk.nix @@ -1,18 +1,9 @@ { config, pkgs, lib, ... }: let cfg = config.gtk.catppuccin; in { - options.gtk.catppuccin = with lib; { - enable = mkEnableOption "Catppuccin theme"; - flavour = mkOption { - type = types.enum [ "latte" "frappe" "macchiato" "mocha" ]; - default = config.catppuccin.flavour; - description = "Catppuccin flavour for gtk"; - }; - accent = mkOption { - type = types.enum [ "blue" "flamingo" "green" "lavender" "maroon" "mauve" "peach" "pink" "red" "rosewater" "sapphire" "sky" "teal" "yellow" ]; - default = config.catppuccin.accent; - description = "Catppuccin accents for gtk"; - }; + options.gtk.catppuccin = with lib; + ctp.mkCatppuccinOpt "gtk" config // { + accent = ctp.mkAccentOpt "gtk" config; size = mkOption { type = types.enum [ "standard" "compact" ]; default = "standard"; @@ -25,17 +16,12 @@ in { }; }; - config.gtk.theme = with lib; - with builtins; + config.gtk.theme = with builtins; + with lib; let - # string -> string - # this capitalizes the first letter in a string - # it's used to set the theme name correctly here - mkUpper = word: (toUpper (substring 0 1 word)) + (substring 1 (stringLength word) word); - - flavourUpper = mkUpper cfg.flavour; - accentUpper = mkUpper cfg.accent; - sizeUpper = mkUpper cfg.size; + flavourUpper = ctp.mkUpper cfg.flavour; + accentUpper = ctp.mkUpper cfg.accent; + sizeUpper = ctp.mkUpper cfg.size; # use the light gtk theme for latte gtkTheme = if cfg.flavour == "latte" then "Light" else "Dark"; diff --git a/modules/home-manager/helix.nix b/modules/home-manager/helix.nix index 14251f1..2ba8c05 100644 --- a/modules/home-manager/helix.nix +++ b/modules/home-manager/helix.nix @@ -1,15 +1,10 @@ { config, pkgs, lib, ... }: let cfg = config.programs.helix.catppuccin; in { - options.programs.helix.catppuccin = with lib; { - enable = mkEnableOption "Catppuccin theme"; - flavour = mkOption { - type = types.enum [ "latte" "frappe" "macchiato" "mocha" ]; - default = config.catppuccin.flavour; - description = "Catppuccin flavour for Helix"; + options.programs.helix.catppuccin = with lib; + ctp.mkCatppuccinOpt "helix" config // { + useItalics = mkEnableOption "Italics in Catppuccin theme for Helix"; }; - useItalics = mkEnableOption "Italics in Catppuccin theme for Helix"; - }; config.programs.helix = with builtins; with lib; diff --git a/modules/home-manager/kitty.nix b/modules/home-manager/kitty.nix index 8be64db..723b9b5 100644 --- a/modules/home-manager/kitty.nix +++ b/modules/home-manager/kitty.nix @@ -1,23 +1,10 @@ { config, lib, ... }: let cfg = config.programs.kitty.catppuccin; in { - options.programs.kitty.catppuccin = with lib; { - enable = mkEnableOption "Catppuccin theme"; - flavour = mkOption { - type = types.enum [ "latte" "frappe" "macchiato" "mocha" ]; - default = config.catppuccin.flavour; - description = "Catppuccin flavour for kitty"; - }; - }; + options.programs.kitty.catppuccin = + lib.ctp.mkCatppuccinOpt "kitty" config; config.programs.kitty = with lib; - let - # string -> string - # this capitalizes the first letter in a string - # it's used to set the theme name correctly here - mkUpper = word: - (toUpper (substring 0 1 word)) + (substring 1 (stringLength word) word); - - flavourUpper = mkUpper cfg.flavour; + let flavourUpper = ctp.mkUpper cfg.flavour; in mkIf cfg.enable { theme = "Catppuccin-${flavourUpper}"; }; } diff --git a/modules/home-manager/polybar.nix b/modules/home-manager/polybar.nix index 99cb966..f8d8a9a 100644 --- a/modules/home-manager/polybar.nix +++ b/modules/home-manager/polybar.nix @@ -1,14 +1,8 @@ { config, pkgs, lib, ... }: let cfg = config.services.polybar.catppuccin; in { - options.services.polybar.catppuccin = with lib; { - enable = mkEnableOption "Catppuccin theme"; - flavour = mkOption { - type = types.enum [ "latte" "frappe" "macchiato" "mocha" ]; - default = config.catppuccin.flavour; - description = "Catppuccin flavour for polybar"; - }; - }; + options.services.polybar.catppuccin = + lib.ctp.mkCatppuccinOpt "polybar" config; config.services.polybar.extraConfig = with builtins; with lib; diff --git a/modules/home-manager/starship.nix b/modules/home-manager/starship.nix index ea02684..f3c05db 100644 --- a/modules/home-manager/starship.nix +++ b/modules/home-manager/starship.nix @@ -1,14 +1,8 @@ { config, pkgs, lib, ... }: let cfg = config.programs.starship.catppuccin; in { - options.programs.starship.catppuccin = { - enable = lib.mkEnableOption "Catppuccin theme"; - flavour = lib.mkOption { - type = lib.types.enum [ "latte" "frappe" "macchiato" "mocha" ]; - default = config.catppuccin.flavour; - description = "Catppuccin flavour for starship"; - }; - }; + options.programs.starship.catppuccin = + lib.ctp.mkCatppuccinOpt "starship" config; config.programs.starship.settings = lib.mkIf cfg.enable ({ diff --git a/modules/home-manager/tmux.nix b/modules/home-manager/tmux.nix index 41e6fbf..c87b549 100644 --- a/modules/home-manager/tmux.nix +++ b/modules/home-manager/tmux.nix @@ -16,14 +16,8 @@ let }; }; in { - options.programs.tmux.catppuccin = with lib; { - enable = mkEnableOption "Catppuccin theme"; - flavour = mkOption { - type = types.enum [ "latte" "frappe" "macchiato" "mocha" ]; - default = config.catppuccin.flavour; - description = "Catppuccin flavour for tmux"; - }; - }; + options.programs.tmux.catppuccin = + lib.ctp.mkCatppuccinOpt "tmux" config; config.programs.tmux.plugins = with lib; mkIf cfg.enable [ { diff --git a/modules/lib/default.nix b/modules/lib/default.nix new file mode 100644 index 0000000..aefe19d --- /dev/null +++ b/modules/lib/default.nix @@ -0,0 +1,86 @@ +lib: +with builtins; +with lib; rec { + # string -> string + # this capitalizes the first letter in a string, + # which is sometimes needed in order to format + # the names of themes correctly + mkUpper = str: + (toUpper (substring 0 1 str)) + (substring 1 (stringLength str) str); + + # a -> path -> a + # fromJSON but for yaml (and without readFile) + # a should be the local pkgs attrset + fromYaml = pkgs: file: + let + # convert to json + json = with pkgs; runCommand "converted.json" { } '' + ${yj}/bin/yj < ${file} > $out + ''; + in fromJSON (readFile json); + + # a -> a -> [path] -> [path] + # this imports a list of paths while inheriting + # multiple attributes + mapModules = config: pkgs: extendedLib: + map (m: + (import m { + inherit config pkgs; + lib = extendedLib; + })); + + types = { + flavourOption = lib.types.enum [ "latte" "frappe" "macchiato" "mocha" ]; + accentOption = lib.types.enum [ + "blue" + "flamingo" + "green" + "lavender" + "maroon" + "mauve" + "peach" + "pink" + "red" + "rosewater" + "sapphire" + "sky" + "teal" + "yellow" + ]; + }; + + # string -> type -> string -> a -> a + # this is an internal function and shouldn't be + # used unless you know what you're doing. it takes + # a string (the name of the property, i.e., flavour + # or accent), the type of the property, the name of + # the module, followed by local config attrset + mkBasicOpt = attr: type: name: config: + mkOption { + inherit type; + default = config.catppuccin.${attr}; + description = "Catppuccin ${attr} for ${name}"; + }; + + # string -> a -> a + # this creates a flavour option for modules + # the first string should be the name of the module, + # followed by the local config attrset + mkFlavourOpt = mkBasicOpt "flavour" types.flavourOption; + + # string -> a -> a + # this creates an accent option for modules + # the first string should be the name of the module, + # followed by the local config attrset + mkAccentOpt = mkBasicOpt "accent" types.accentOption; + + # string -> a -> a + # this creates a basic attrset only containing an + # enable and flavour option. the fist string should + # be the name of the module, followed by the local config + # attrset + mkCatppuccinOpt = name: config: { + enable = mkEnableOption "Catppuccin theme"; + flavour = mkFlavourOpt name config; + }; +} diff --git a/modules/lib/mkExtLib.nix b/modules/lib/mkExtLib.nix new file mode 100644 index 0000000..4250954 --- /dev/null +++ b/modules/lib/mkExtLib.nix @@ -0,0 +1 @@ +lib: with builtins; lib.extend (self: _: { ctp = import ./. self; }) diff --git a/modules/nixos/default.nix b/modules/nixos/default.nix index ebc4712..2a7f126 100644 --- a/modules/nixos/default.nix +++ b/modules/nixos/default.nix @@ -1,11 +1,17 @@ -{ lib, ... }: { - imports = [ - ./grub.nix - ]; +{ config, pkgs, lib, ... }: let + extendedLib = import ../lib/mkExtLib.nix lib; +in { + imports = let + files = [ + ./grub.nix + ]; + in + extendedLib.ctp.mapModules config pkgs extendedLib files; - options.catppuccin = { - flavour = lib.mkOption { - type = lib.types.enum [ "latte" "frappe" "macchiato" "mocha" ]; + + options.catppuccin = with extendedLib; { + flavour = mkOption { + type = ctp.types.flavourOption; default = "latte"; description = "Global Catppuccin flavour"; };