Skip to content

Commit cf40aa4

Browse files
committed
palette: add option to allow a user to swap the colorscheme generator logic
Signed-off-by: lucasew <[email protected]>
1 parent c33226f commit cf40aa4

File tree

1 file changed

+58
-7
lines changed

1 file changed

+58
-7
lines changed

stylix/palette.nix

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,35 @@
88
let
99
cfg = config.stylix;
1010
opts = options.stylix;
11+
12+
# Validates that a palette contains all required base16 colors
13+
validateBase16Palette =
14+
palette:
15+
let
16+
requiredColors = [
17+
"base00"
18+
"base01"
19+
"base02"
20+
"base03"
21+
"base04"
22+
"base05"
23+
"base06"
24+
"base07"
25+
"base08"
26+
"base09"
27+
"base0A"
28+
"base0B"
29+
"base0C"
30+
"base0D"
31+
"base0E"
32+
"base0F"
33+
];
34+
missingColors = lib.filter (c: !(palette ? ${c})) requiredColors;
35+
in
36+
if missingColors != [ ] then
37+
throw "Palette generator output is missing required base16 colors: ${lib.concatStringsSep ", " missingColors}"
38+
else
39+
palette;
1140
in
1241
{
1342
options.stylix = {
@@ -41,6 +70,31 @@ in
4170
default = null;
4271
};
4372

73+
paletteGeneratorFunction = lib.mkOption {
74+
description = ''
75+
Function to generate palette from image. Must return a derivation that
76+
produces a JSON file with a base16 color scheme (base00-base0F).
77+
78+
The function receives an attribute set with the following arguments:
79+
- `polarity`: The polarity setting ("either", "light", or "dark")
80+
- `image`: Path to the wallpaper image
81+
82+
Example:
83+
```nix
84+
paletteGeneratorFunction = { polarity, image }:
85+
pkgs.runCommand "palette.json" {} \'\'
86+
''${pkgs.matugen}/bin/matugen json ''${image} > $out
87+
\'\';
88+
```
89+
'';
90+
type = lib.types.functionTo lib.types.package;
91+
default =
92+
{ polarity, image }:
93+
pkgs.runCommand "palette.json" { } ''
94+
${lib.getExe cfg.paletteGenerator} "${polarity}" ${lib.escapeShellArg image} "$out"
95+
'';
96+
};
97+
4498
imageScalingMode = lib.mkOption {
4599
type = lib.types.enum [
46100
"stretch"
@@ -80,20 +134,17 @@ in
80134
# and not anything indirect such as filling a template, otherwise
81135
# the output of the palette generator will not be protected from
82136
# garbage collection.
83-
default = pkgs.runCommand "palette.json" { } ''
84-
${lib.getExe cfg.paletteGenerator} \
85-
"${cfg.polarity}" \
86-
${lib.escapeShellArg cfg.image} \
87-
"$out"
88-
'';
137+
default = cfg.paletteGeneratorFunction {
138+
inherit (cfg) polarity image;
139+
};
89140
};
90141

91142
palette = lib.mkOption {
92143
type = lib.types.attrs;
93144
description = "The palette generated by the palette generator.";
94145
readOnly = true;
95146
internal = true;
96-
default = (lib.importJSON cfg.generated.json) // {
147+
default = validateBase16Palette (lib.importJSON cfg.generated.json) // {
97148
author = "Stylix";
98149
scheme = "Stylix";
99150
slug = "stylix";

0 commit comments

Comments
 (0)