|
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 | + 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 | + |
44 | 98 | imageScalingMode = lib.mkOption { |
45 | 99 | type = lib.types.enum [ |
46 | 100 | "stretch" |
|
80 | 134 | # and not anything indirect such as filling a template, otherwise |
81 | 135 | # the output of the palette generator will not be protected from |
82 | 136 | # 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 | + }; |
89 | 140 | }; |
90 | 141 |
|
91 | 142 | palette = lib.mkOption { |
92 | 143 | type = lib.types.attrs; |
93 | 144 | description = "The palette generated by the palette generator."; |
94 | 145 | readOnly = true; |
95 | 146 | internal = true; |
96 | | - default = (lib.importJSON cfg.generated.json) // { |
| 147 | + default = validateBase16Palette (lib.importJSON cfg.generated.json) // { |
97 | 148 | author = "Stylix"; |
98 | 149 | scheme = "Stylix"; |
99 | 150 | slug = "stylix"; |
|
0 commit comments