Skip to content

Commit

Permalink
draft: reworked hm-module
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniD3v committed Jul 21, 2024
1 parent d6ce6cf commit 8cce73b
Showing 1 changed file with 59 additions and 101 deletions.
160 changes: 59 additions & 101 deletions module.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,76 +9,30 @@ matugen: {
osCfg = inputs.osConfig.programs.matugen or {};

tomlFormat = pkgs.formats.toml {};

capitalize = str: let
inherit (builtins) substring stringLength;
firstChar = substring 0 1 str;
restOfString = substring 1 (stringLength str) str;
in
lib.concatStrings [(lib.toUpper firstChar) restOfString];

# don't use ~, use $HOME
sanitizedTemplates =
builtins.mapAttrs (_: v: {
mode = capitalize cfg.variant;
input_path = builtins.toString v.input_path;
output_path = builtins.replaceStrings ["$HOME"] ["~"] v.output_path;
})
cfg.templates;

matugenConfig = tomlFormat.generate "matugen-config.toml" {
config = {};
templates = sanitizedTemplates;
};

# get matugen package
pkg = matugen.packages.${pkgs.system}.default;

themePackage = pkgs.runCommandLocal "matugen-themes-${cfg.variant}" {} ''
mkdir -p $out
cd $out
export HOME=$(pwd)
${pkg}/bin/matugen \
image ${cfg.wallpaper} \
${
if cfg.templates != {}
then "--config ${matugenConfig}"
else ""
} \
--mode ${cfg.variant} \
--type ${cfg.type} \
--json ${cfg.jsonFormat} \
--quiet \
> $out/theme.json
'';
colors = builtins.fromJSON (builtins.readFile "${themePackage}/theme.json");
in {
options.programs.matugen = {
enable = lib.mkEnableOption "Matugen declarative theming";

wallpaper = lib.mkOption {
type = lib.types.path;
default = osCfg.wallpaper or "${pkgs.nixos-artwork.wallpapers.simple-blue}/share/backgrounds/nixos/nix-wallpaper-simple-blue.png";
type = with lib.types; nullOr path;
default = osCfg.wallpaper or null;

example = "../wallpaper/astolfo.png";
description = "Path to `wallpaper` that matugen will generate the colorschemes from";
defaultText = lib.literalExample ''
"${pkgs.nixos-artwork.wallpapers.simple-blue}/share/backgrounds/nixos/nix-wallpaper-simple-blue.png"
'';
};

templates = lib.mkOption {
type = with lib.types;
attrsOf (submodule {
type = with lib;
types.attrsOf (types.submodule {
options = {
input_path = lib.mkOption {
type = path;
input_path = mkOption {
type = types.path;

example = "./style.css";
description = "Path to the template";
};
output_path = lib.mkOption {
type = str;
output_path = mkOption {
type = types.str;

example = "~/.config/sytle.css";
description = "Path where the generated file will be written to";
Expand All @@ -93,56 +47,60 @@ in {
'';
};

type = lib.mkOption {
type = lib.types.enum ["scheme-content" "scheme-expressive" "scheme-fidelity" "scheme-fruit-salad" "scheme-monochrome" "scheme-neutral" "scheme-rainbow" "scheme-tonal-spot"];
default = osCfg.palette or "scheme-tonal-spot";

example = "triadic";
description = "Palette used when generating the colorschemes.";
};
settings = lib.mkOption {
inherit (tomlFormat) type;
default = osCfg.settings or {};

jsonFormat = lib.mkOption {
type = lib.types.enum ["rgb" "rgba" "hsl" "hsla" "hex" "strip"];
default = osCfg.jsonFormat or "strip";

example = "rgba";
description = "Color format of the colorschemes.";
};
example = ''
config.reload_apps_list = {
gtk_theme = true;
kitty = true;
}
'';
description = ''
Matugen configuration file in nix syntax.
variant = lib.mkOption {
type = lib.types.enum ["light" "dark" "amoled"];
default = osCfg.variant or "dark";

example = "light";
description = "Colorscheme variant.";
Written to {file}`$XDG_CONFIG_HOME/matugen/config.toml`
A manual for configuring matugen can be found at <https://github.com/InioX/matugen/wiki/Configuration>.
'';
};
};

theme.files = lib.mkOption {
type = lib.types.package;
readOnly = true;
default =
if builtins.hasAttr "templates" osCfg
then
if cfg.templates != osCfg.templates
then themePackage
else osCfg.theme.files
else themePackage;

description = "Generated theme files. Including only the variant chosen.";
};
config = let
package = matugen.packages.${pkgs.system}.default;

mergedCfg =
cfg.settings
// (
if cfg.templates != {}
then {inherit (cfg) templates;}
else {}
);

configFile = tomlFormat.generate "config.toml" mergedCfg;

resultPackage =
if (cfg.wallpaper != null)
then [
(
pkgs.runCommand "results" {}
''
echo "out path: $out"
echo "running command: ${package}/bin/matugen image ${cfg.wallpaper} --config ${configFile} --prefix $out"
mkdir -p $out/${config.home.homeDirectory}
${package}/bin/matugen image ${cfg.wallpaper} --config ${configFile} --prefix $out
''
)
]
else [];
in
lib.mkIf cfg.enable {
home.packages =
[package]
++ resultPackage;

theme.colors = lib.mkOption {
inherit (pkgs.formats.json {}) type;
readOnly = true;
default =
if builtins.hasAttr "templates" osCfg
then
if cfg.templates != osCfg.templates
then colors
else osCfg.theme.colors
else colors;

description = "Generated theme colors. Includes all variants.";
xdg.configFile."matugen/config.toml".source =
lib.mkIf (mergedCfg != {}) configFile;
};
};
}

0 comments on commit 8cce73b

Please sign in to comment.