feat(modules): add util library (#25)

This commit is contained in:
seth 2023-04-17 12:44:07 -04:00 committed by GitHub
parent 8b7aa60e3f
commit be6320c4b1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 152 additions and 138 deletions

View file

@ -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);
}

View file

@ -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";

View file

@ -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;

View file

@ -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);

View file

@ -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";
};

View file

@ -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";

View file

@ -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;

View file

@ -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}"; };
}

View file

@ -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;

View file

@ -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
({

View file

@ -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 [
{

86
modules/lib/default.nix Normal file
View file

@ -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
modules/lib/mkExtLib.nix Normal file
View file

@ -0,0 +1 @@
lib: with builtins; lib.extend (self: _: { ctp = import ./. self; })

View file

@ -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";
};