Skip to content

Commit

Permalink
refactor(modules): cleanup module importing and lib injection (#385)
Browse files Browse the repository at this point in the history
* refactor(modules): share global option definitions

* refactor: use mkCatppuccinOpt for all main options

* refactor(modules): lib.ctp.mkCatppuccinOpt -> lib.ctp.mkCatppuccinOption

Meant to be more inline with `lib.mkOption` from Nixpkgs

* refactor(modules): types.{accent,flavor}Option -> types.{accent,flavor}

* refactor(modules): inject custom lib with `lib.modules.importApply`

A bit cleaner than our current solution, and creates a clear separation
between our functions and those from Nixpkgs

* refactor(modules): use standalone attrset for lib

We don't need to extend nixpkgs.lib anymore

* refactor(modules): remove unnecessary config guards

We don't need to be doing this when the upstream modules already guard
against their own `enable` option

* chore(tests): remove unused arg
  • Loading branch information
getchoo authored Dec 17, 2024
1 parent 20b6328 commit 21310cd
Show file tree
Hide file tree
Showing 63 changed files with 796 additions and 566 deletions.
23 changes: 18 additions & 5 deletions modules/home-manager/globals.nix → modules/global.nix
Original file line number Diff line number Diff line change
@@ -1,27 +1,40 @@
{ lib, ... }:
{ catppuccinModules }:
{
config,
lib,
pkgs,
...
}:

let
catppuccinLib = import ./lib { inherit config lib pkgs; };
in

{
config = {
assertions = [ (lib.ctp.assertMinimumVersion "24.11") ];
assertions = [ (catppuccinLib.assertMinimumVersion "24.11") ];
};

imports = catppuccinLib.applyToModules catppuccinModules;

options.catppuccin = {
enable = lib.mkEnableOption "Catppuccin globally";

flavor = lib.mkOption {
type = lib.ctp.types.flavorOption;
type = catppuccinLib.types.flavor;
default = "mocha";
description = "Global Catppuccin flavor";
};

accent = lib.mkOption {
type = lib.ctp.types.accentOption;
type = catppuccinLib.types.accent;
default = "mauve";
description = "Global Catppuccin accent";
};

sources =
let
defaultSources = import ../../.sources;
defaultSources = import ../.sources;
in
lib.mkOption {
type = lib.types.lazyAttrsOf lib.types.raw;
Expand Down
11 changes: 7 additions & 4 deletions modules/home-manager/aerc.nix
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
{ catppuccinLib }:
{ config, lib, ... }:

let
inherit (config.catppuccin) sources;

cfg = config.programs.aerc.catppuccin;
enable = cfg.enable && config.programs.aerc.enable;
themeName = "catppuccin-${cfg.flavor}";
in

{
options.programs.aerc.catppuccin = lib.ctp.mkCatppuccinOpt { name = "aerc"; };
options.programs.aerc.catppuccin = catppuccinLib.mkCatppuccinOption { name = "aerc"; };

config = lib.mkIf enable {
config = lib.mkIf cfg.enable {
programs.aerc = {
stylesets.${themeName} = builtins.readFile "${sources.aerc}/dist/${themeName}";
stylesets.${themeName} = lib.fileContents "${sources.aerc}/dist/${themeName}";
extraConfig = {
ui = {
styleset-name = themeName;
Expand Down
13 changes: 9 additions & 4 deletions modules/home-manager/alacritty.nix
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
{ catppuccinLib }:
{ config, lib, ... }:

let
inherit (config.catppuccin) sources;

cfg = config.programs.alacritty.catppuccin;
enable = cfg.enable && config.programs.alacritty.enable;
in

{
options.programs.alacritty.catppuccin = lib.ctp.mkCatppuccinOpt { name = "alacritty"; };
options.programs.alacritty.catppuccin = catppuccinLib.mkCatppuccinOption { name = "alacritty"; };

config = lib.mkIf enable {
programs.alacritty.settings = lib.importTOML "${sources.alacritty}/catppuccin-${cfg.flavor}.toml";
config = lib.mkIf cfg.enable {
programs.alacritty = {
settings = lib.importTOML "${sources.alacritty}/catppuccin-${cfg.flavor}.toml";
};
};
}
1 change: 0 additions & 1 deletion modules/home-manager/all-modules.nix
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
./gh-dash.nix
./gitui.nix
./glamour.nix
./globals.nix
./gtk.nix
./helix.nix
./hyprland.nix
Expand Down
11 changes: 7 additions & 4 deletions modules/home-manager/bat.nix
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
{ catppuccinLib }:
{ config, lib, ... }:

let
inherit (config.catppuccin) sources;

cfg = config.programs.bat.catppuccin;
enable = cfg.enable && config.programs.bat.enable;
themeName = "Catppuccin ${lib.ctp.mkUpper cfg.flavor}";
themeName = "Catppuccin ${catppuccinLib.mkUpper cfg.flavor}";
in

{
options.programs.bat.catppuccin = lib.ctp.mkCatppuccinOpt { name = "bat"; };
options.programs.bat.catppuccin = catppuccinLib.mkCatppuccinOption { name = "bat"; };

config = lib.mkIf enable {
config = lib.mkIf cfg.enable {
programs.bat = {
config.theme = themeName;

Expand Down
9 changes: 6 additions & 3 deletions modules/home-manager/bottom.nix
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
{ catppuccinLib }:
{ config, lib, ... }:

let
inherit (config.catppuccin) sources;

cfg = config.programs.bottom.catppuccin;
enable = cfg.enable && config.programs.bottom.enable;
in

{
options.programs.bottom.catppuccin = lib.ctp.mkCatppuccinOpt { name = "bottom"; };
options.programs.bottom.catppuccin = catppuccinLib.mkCatppuccinOption { name = "bottom"; };

config = lib.mkIf enable {
config = lib.mkIf cfg.enable {
programs.bottom = {
settings = lib.importTOML "${sources.bottom}/themes/${cfg.flavor}.toml";
};
Expand Down
16 changes: 13 additions & 3 deletions modules/home-manager/btop.nix
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
{ catppuccinLib }:
{ config, lib, ... }:

let
inherit (config.catppuccin) sources;

cfg = config.programs.btop.catppuccin;
enable = cfg.enable && config.programs.btop.enable;

themeFile = "catppuccin_${cfg.flavor}.theme";
themePath = "/themes/${themeFile}";
theme = sources.btop + themePath;
in

{
options.programs.btop.catppuccin = lib.ctp.mkCatppuccinOpt { name = "btop"; };
options.programs.btop.catppuccin = catppuccinLib.mkCatppuccinOption { name = "btop"; };

config = lib.mkIf enable {
xdg.configFile."btop${themePath}".source = theme;
xdg.configFile = {
"btop${themePath}".source = theme;
};

programs.btop.settings.color_theme = themeFile;
programs.btop = {
settings = {
color_theme = themeFile;
};
};
};
}
13 changes: 9 additions & 4 deletions modules/home-manager/cava.nix
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
{ catppuccinLib }:
{ config, lib, ... }:

let
inherit (config.catppuccin) sources;

cfg = config.programs.cava.catppuccin;
enable = cfg.enable && config.programs.cava.enable;
flavor = "${cfg.flavor}" + lib.optionalString cfg.transparent "-transparent";
in

{
options.programs.cava.catppuccin = lib.ctp.mkCatppuccinOpt { name = "cava"; } // {
options.programs.cava.catppuccin = catppuccinLib.mkCatppuccinOption { name = "cava"; } // {
transparent = lib.mkEnableOption "transparent version of flavor";
};

config.programs.cava = lib.mkIf enable {
settings = lib.ctp.fromINIRaw (sources.cava + "/themes/${flavor}.cava");
config = lib.mkIf cfg.enable {
programs.cava = {
settings = catppuccinLib.fromINIRaw (sources.cava + "/themes/${flavor}.cava");
};
};
}
28 changes: 18 additions & 10 deletions modules/home-manager/cursor.nix
Original file line number Diff line number Diff line change
@@ -1,35 +1,43 @@
{ catppuccinLib }:
{
config,
pkgs,
lib,
...
}:

let
inherit (lib) ctp mkIf;
cfg = config.catppuccin.pointerCursor;

# "dark" and "light" can be used alongside the regular accents
cursorAccentType = ctp.mergeEnums ctp.types.accentOption (
cursorAccentType = catppuccinLib.mergeEnums catppuccinLib.types.accent (
lib.types.enum [
"dark"
"light"
]
);
in

{
options.catppuccin.pointerCursor =
ctp.mkCatppuccinOpt {
catppuccinLib.mkCatppuccinOption {
name = "pointer cursors";
# NOTE: we exclude this from the global `catppuccin.enable` as there is no
# `enable` option in the upstream module to guard it
enableDefault = false;
# NOTE: We exclude this as there is no `enable` option in the upstream
# module to guard it
useGlobalEnable = false;
}
// {
accent = ctp.mkBasicOpt "accent" cursorAccentType "cursors";
accent = lib.mkOption {
type = cursorAccentType;
default = config.catppuccin.accent;
description = "Catppuccin accent for pointer cursors";
};
};

config.home.pointerCursor = mkIf cfg.enable {
name = "catppuccin-${cfg.flavor}-${cfg.accent}-cursors";
package = pkgs.catppuccin-cursors.${cfg.flavor + ctp.mkUpper cfg.accent};
config = lib.mkIf cfg.enable {
home.pointerCursor = {
name = "catppuccin-${cfg.flavor}-${cfg.accent}-cursors";
package = pkgs.catppuccin-cursors.${cfg.flavor + catppuccinLib.mkUpper cfg.accent};
};
};
}
15 changes: 5 additions & 10 deletions modules/home-manager/default.nix
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
{ lib, ... }:

{
config,
lib,
pkgs,
...
}:
{
imports = import ../lib/import-modules.nix {
inherit config lib pkgs;
modules = import ./all-modules.nix;
};
imports = [
(lib.modules.importApply ../global.nix { catppuccinModules = import ./all-modules.nix; })
];
}
6 changes: 5 additions & 1 deletion modules/home-manager/delta.nix
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
{ catppuccinLib }:
{ config, lib, ... }:

let
inherit (config.catppuccin) sources;

cfg = config.programs.git.delta.catppuccin;
enable = cfg.enable && config.programs.git.delta.enable;
in

{
options.programs.git.delta.catppuccin = lib.ctp.mkCatppuccinOpt { name = "delta"; };
options.programs.git.delta.catppuccin = catppuccinLib.mkCatppuccinOption { name = "delta"; };

config = lib.mkIf enable {
programs.git = {
Expand Down
20 changes: 13 additions & 7 deletions modules/home-manager/dunst.nix
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
{ catppuccinLib }:
{ config, lib, ... }:

let
inherit (config.catppuccin) sources;

cfg = config.services.dunst.catppuccin;
enable = cfg.enable && config.services.dunst.enable;
in

{
options.services.dunst.catppuccin = lib.ctp.mkCatppuccinOpt { name = "dunst"; } // {
options.services.dunst.catppuccin = catppuccinLib.mkCatppuccinOption { name = "dunst"; } // {
prefix = lib.mkOption {
type = lib.types.str;
default = "00";
Expand All @@ -16,11 +20,13 @@ in
# Dunst currently has no "include" functionality, but has "drop-ins"
# Unfortunately, this may cause inconvenience as it overrides ~/.config/dunst/dunstrc
# but it can be overridden by another drop-in.
config.xdg.configFile = lib.mkIf enable {
# Using a prefix like this is necessary because drop-ins' precedence depends on lexical order
# such that later drop-ins override earlier ones
# This way, users have better control over precedence
"dunst/dunstrc.d/${cfg.prefix}-catppuccin.conf".source =
sources.dunst + "/themes/${cfg.flavor}.conf";
config = lib.mkIf enable {
xdg.configFile = {
# Using a prefix like this is necessary because drop-ins' precedence depends on lexical order
# such that later drop-ins override earlier ones
# This way, users have better control over precedence
"dunst/dunstrc.d/${cfg.prefix}-catppuccin.conf".source =
sources.dunst + "/themes/${cfg.flavor}.conf";
};
};
}
44 changes: 28 additions & 16 deletions modules/home-manager/fcitx5.nix
Original file line number Diff line number Diff line change
@@ -1,31 +1,43 @@
{ catppuccinLib }:
{ config, lib, ... }:

let
inherit (config.catppuccin) sources;

cfg = config.i18n.inputMethod.fcitx5.catppuccin;
enable = cfg.enable && config.i18n.inputMethod.enabled == "fcitx5";
in

{
options.i18n.inputMethod.fcitx5.catppuccin = lib.ctp.mkCatppuccinOpt { name = "Fcitx5"; } // {
accent = lib.ctp.mkAccentOpt "Fcitx5";
apply = lib.mkOption {
type = lib.types.bool;
default = true;
description = ''
Applies the theme by overwriting `$XDG_CONFIG_HOME/fcitx5/conf/classicui.conf`.
If this is disabled, you must manually set the theme (e.g. by using `fcitx5-configtool`).
'';
options.i18n.inputMethod.fcitx5.catppuccin =
catppuccinLib.mkCatppuccinOption {
name = "Fcitx5";
accentSupport = true;
}
// {
apply = lib.mkOption {
type = lib.types.bool;
default = true;
description = ''
Applies the theme by overwriting `$XDG_CONFIG_HOME/fcitx5/conf/classicui.conf`.
If this is disabled, you must manually set the theme (e.g. by using `fcitx5-configtool`).
'';
};
};
};

config = lib.mkIf enable {
xdg.dataFile."fcitx5/themes/catppuccin-${cfg.flavor}-${cfg.accent}" = {
source = "${sources.fcitx5}/src/catppuccin-${cfg.flavor}-${cfg.accent}";
recursive = true;
xdg.dataFile = {
"fcitx5/themes/catppuccin-${cfg.flavor}-${cfg.accent}" = {
source = "${sources.fcitx5}/src/catppuccin-${cfg.flavor}-${cfg.accent}";
recursive = true;
};
};

xdg.configFile."fcitx5/conf/classicui.conf" = lib.mkIf cfg.apply {
text = lib.generators.toINIWithGlobalSection { } {
globalSection.Theme = "catppuccin-${cfg.flavor}-${cfg.accent}";
xdg.configFile = {
"fcitx5/conf/classicui.conf" = lib.mkIf cfg.apply {
text = lib.generators.toINIWithGlobalSection { } {
globalSection.Theme = "catppuccin-${cfg.flavor}-${cfg.accent}";
};
};
};
};
Expand Down
Loading

0 comments on commit 21310cd

Please sign in to comment.