Skip to content

Commit

Permalink
feat(modules): add util library (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
getchoo authored Apr 17, 2023
1 parent 8b7aa60 commit be6320c
Show file tree
Hide file tree
Showing 14 changed files with 152 additions and 138 deletions.
22 changes: 3 additions & 19 deletions modules/home-manager/alacritty.nix
Original file line number Diff line number Diff line change
@@ -1,35 +1,19 @@
{ 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";
rev = "3c808cbb4f9c87be43ba5241bc57373c793d2f17";
sha256 = "sha256-w9XVtEe7TqzxxGUCDUR9BFkzLZjG8XrplXJ3lX6f+x0=";
} + "/catppuccin-${cfg.flavour}.yml";

in mkIf cfg.enable (fromYaml file);
in mkIf cfg.enable (ctp.fromYaml pkgs file);
}
10 changes: 2 additions & 8 deletions modules/home-manager/bat.nix
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
10 changes: 2 additions & 8 deletions modules/home-manager/bottom.nix
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
10 changes: 2 additions & 8 deletions modules/home-manager/btop.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
41 changes: 23 additions & 18 deletions modules/home-manager/default.nix
Original file line number Diff line number Diff line change
@@ -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";
};
Expand Down
30 changes: 8 additions & 22 deletions modules/home-manager/gtk.nix
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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";
Expand Down
11 changes: 3 additions & 8 deletions modules/home-manager/helix.nix
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
19 changes: 3 additions & 16 deletions modules/home-manager/kitty.nix
Original file line number Diff line number Diff line change
@@ -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}"; };
}
10 changes: 2 additions & 8 deletions modules/home-manager/polybar.nix
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
10 changes: 2 additions & 8 deletions modules/home-manager/starship.nix
Original file line number Diff line number Diff line change
@@ -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
({
Expand Down
10 changes: 2 additions & 8 deletions modules/home-manager/tmux.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
{
Expand Down
86 changes: 86 additions & 0 deletions modules/lib/default.nix
Original file line number Diff line number Diff line change
@@ -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;
};
}
1 change: 1 addition & 0 deletions modules/lib/mkExtLib.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lib: with builtins; lib.extend (self: _: { ctp = import ./. self; })
20 changes: 13 additions & 7 deletions modules/nixos/default.nix
Original file line number Diff line number Diff line change
@@ -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";
};
Expand Down

0 comments on commit be6320c

Please sign in to comment.