feat(lib): Add helper fns to handle theme naming

Rose Pine names the default theme flavor something like "Rose Pine", and
when a theme needs to be kebab case that requires extra logic to avoid a
hanging - when the main flavor is selected.
This commit is contained in:
punkfairie 2024-11-04 13:05:50 -08:00
parent 91374e5df4
commit 0f2722c5f7
Signed by: punkfairie
GPG key ID: 01823C057725C266

View file

@ -3,17 +3,20 @@
lib,
pkgs,
...
}: let
}:
let
# this is a recursive attribute with all the functions below
inherit (lib) rp;
in {
in
{
# string -> type -> string -> 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., flavor
# or accent), the type of the property, the name of
# the module, followed by local config attrset
mkBasicOpt = attr: type: name:
mkBasicOpt =
attr: type: name:
lib.mkOption {
inherit type;
default = config.rose-pine.${attr};
@ -42,47 +45,65 @@ in {
];
};
# string -> string
# this returns a blank string when string == "main",
# because Rose Pine names the default flavor rose pine
getFlavor = flavor: (if flavor == "main" then "" else flavor);
# string -> string
# convert flavor to full theme name in kebab case
# a convenience to deal with the main flavor naming
getKebabTheme = flavor: (if flavor == "main" then "rose-pine" else "rose-pine-${flavor}");
# 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:
mkUpper =
str:
(lib.toUpper (builtins.substring 0 1 str)) + (builtins.substring 1 (builtins.stringLength str) str);
# a -> path -> a
# fromJSON but for yaml (and without readFile)
# a should be the local pkgs attrset
fromYaml = file: let
# convert to json
json = pkgs.runCommand "converted.json" {} ''
${lib.getExe pkgs.yj} < ${file} > $out
'';
in
fromYaml =
file:
let
# convert to json
json = pkgs.runCommand "converted.json" { } ''
${lib.getExe pkgs.yj} < ${file} > $out
'';
in
builtins.fromJSON (builtins.readFile json);
# a -> path -> a
# fromJSON but for ini (and without readFile)
# a should be the local pkgs attrset
fromINI = file: let
# convert to json
json = pkgs.runCommand "converted.json" {} ''
${lib.getExe pkgs.jc} --ini < ${file} > $out
'';
in
fromINI =
file:
let
# convert to json
json = pkgs.runCommand "converted.json" { } ''
${lib.getExe pkgs.jc} --ini < ${file} > $out
'';
in
builtins.fromJSON (builtins.readFile json);
# a -> path -> a
# fromJSON but for raw ini (and without readFile)
# a should be the local pkgs attrset
fromINIRaw = file: let
inherit (builtins) fromJSON readFile;
fromINIRaw =
file:
let
inherit (builtins) fromJSON readFile;
# convert to json
json = with pkgs;
runCommand "converted.json" {} ''
${jc}/bin/jc --ini -r < ${file} > $out
'';
in
# convert to json
json =
with pkgs;
runCommand "converted.json" { } ''
${jc}/bin/jc --ini -r < ${file} > $out
'';
in
fromJSON (readFile json);
# string -> a
@ -91,18 +112,18 @@ in {
# of the module, while `enableDefault` is a boolean
# representing the default of the created `enable`
# option
mkRosePineOpt = {
name,
enableDefault ? config.rose-pine.enable,
}: {
enable =
lib.mkEnableOption "Rose Pine theme for ${name}"
// {
mkRosePineOpt =
{
name,
enableDefault ? config.rose-pine.enable,
}:
{
enable = lib.mkEnableOption "Rose Pine theme for ${name}" // {
default = enableDefault;
};
flavor = rp.mkFlavorOpt name;
};
flavor = rp.mkFlavorOpt name;
};
# string -> a
# this creates an accent option for modules
@ -120,17 +141,18 @@ in {
# returns the current release version of nixos or home-manager. throws an evaluation error if neither are
# found
getModuleRelease =
config.home.version.release
or config.system.nixos.release
or (throw "Couldn't determine release version!");
config.home.version.release or config.system.nixos.release
or (throw "Couldn't determine release version!");
# string -> a -> a
# if the current module release is less than `minVersion`, all options are made no-ops with
# `lib.mkSinkUndeclaredOptions`
mkVersionedOpts = minVersion: option:
if lib.versionAtLeast rp.getModuleRelease minVersion
then option
else lib.mkSinkUndeclaredOptions {};
mkVersionedOpts =
minVersion: option:
if lib.versionAtLeast rp.getModuleRelease minVersion then
option
else
lib.mkSinkUndeclaredOptions { };
# string -> a
# this is to ensure users are running a supported version of nixos/home-manager