|
8 | 8 | let |
9 | 9 | cfg = config.stylix; |
10 | 10 | 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; |
11 | 40 | in |
12 | 41 | { |
13 | 42 | options.stylix = { |
|
41 | 70 | default = null; |
42 | 71 | }; |
43 | 72 |
|
| 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 | +
|
| 85 | + paletteGeneratorFunction = { polarity, image }: |
| 86 | + let |
| 87 | + color = "ffffff"; |
| 88 | + colormap = { |
| 89 | + base00 = color; |
| 90 | + base01 = color; |
| 91 | + base02 = color; |
| 92 | + base03 = color; |
| 93 | + base04 = color; |
| 94 | + base05 = color; |
| 95 | + base06 = color; |
| 96 | + base07 = color; |
| 97 | + base08 = color; |
| 98 | + base09 = color; |
| 99 | + base0A = color; |
| 100 | + base0B = color; |
| 101 | + base0C = color; |
| 102 | + base0D = color; |
| 103 | + base0E = color; |
| 104 | + base0F = color; |
| 105 | + }; |
| 106 | + in |
| 107 | + pkgs.runCommand "palette.json" {color = "ffffff"; } \'\' |
| 108 | + echo \'\$\{builtins.toJSON colormap}\' > $out |
| 109 | + \'\'; |
| 110 | + ``` |
| 111 | + ''; |
| 112 | + type = lib.types.functionTo lib.types.package; |
| 113 | + default = |
| 114 | + { polarity, image }: |
| 115 | + pkgs.runCommand "palette.json" { } '' |
| 116 | + ${lib.getExe cfg.paletteGenerator} "${polarity}" ${lib.escapeShellArg image} "$out" |
| 117 | + ''; |
| 118 | + }; |
| 119 | + |
44 | 120 | imageScalingMode = lib.mkOption { |
45 | 121 | type = lib.types.enum [ |
46 | 122 | "stretch" |
|
80 | 156 | # and not anything indirect such as filling a template, otherwise |
81 | 157 | # the output of the palette generator will not be protected from |
82 | 158 | # garbage collection. |
83 | | - default = pkgs.runCommand "palette.json" { } '' |
84 | | - ${lib.getExe cfg.paletteGenerator} \ |
85 | | - "${cfg.polarity}" \ |
86 | | - ${lib.escapeShellArg cfg.image} \ |
87 | | - "$out" |
88 | | - ''; |
| 159 | + default = cfg.paletteGeneratorFunction { |
| 160 | + inherit (cfg) polarity image; |
| 161 | + }; |
89 | 162 | }; |
90 | 163 |
|
91 | 164 | palette = lib.mkOption { |
92 | 165 | type = lib.types.attrs; |
93 | 166 | description = "The palette generated by the palette generator."; |
94 | 167 | readOnly = true; |
95 | 168 | internal = true; |
96 | | - default = (lib.importJSON cfg.generated.json) // { |
| 169 | + default = validateBase16Palette (lib.importJSON cfg.generated.json) // { |
97 | 170 | author = "Stylix"; |
98 | 171 | scheme = "Stylix"; |
99 | 172 | slug = "stylix"; |
|
0 commit comments