stylix: refactor base16Scheme and lib.stylix.colors assertion (#1446)

Instead of having a nullable `base16Scheme` option, the default will
throw when `stylix.image` is null. Not only does this better represent
the "required" nature of this option, it also ensures the assertion is
evaluated early enough to actually be checked.

This causes the assertion to evaluate when the default definition is
read, instead of later when the `assertions` option is checked.

Link: https://github.com/nix-community/stylix/pull/1446

Tested-by: https://github.com/NovaViper
Approved-by: https://github.com/NovaViper
Reviewed-by: NAHO <90870942+trueNAHO@users.noreply.github.com>
This commit is contained in:
Matt Sturgeon 2025-06-12 23:00:37 +01:00 committed by GitHub
parent 75411fe2b9
commit 58b1de7ebf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -8,7 +8,7 @@
let
cfg = config.stylix;
opts = options.stylix;
in
{
options.stylix = {
@ -117,18 +117,23 @@ in
'';
type =
with lib.types;
nullOr (oneOf [
oneOf [
path
lines
attrs
]);
# defaults to null when cfg.image is also null, in order to trigger the assertion below correctly
default = if cfg.image == null then null else cfg.generated.palette;
];
# We add an assertion here, because this value is used throughout Stylix.
# Using the assertions option is ineffective because it is checked after
# the errors caused by reading `base16Scheme` have occurred.
# See https://github.com/nix-community/stylix/pull/1408
# and https://github.com/nix-community/stylix/pull/1446
default =
lib.throwIf (cfg.image == null)
"stylix: one of `${opts.image}' or `${opts.base16Scheme}' must be set."
cfg.generated.palette;
defaultText = lib.literalMD ''
The colors used in the theming.
Those are automatically selected from the background image by default,
but could be overridden manually.
Automatically selected from the background image. The default will
throw if `stylix.image` is null.
'';
};
@ -167,24 +172,7 @@ in
config = {
# This attrset can be used like a function too, see
# https://github.com/SenchoPens/base16.nix/blob/b390e87cd404e65ab4d786666351f1292e89162a/README.md#theme-step-22
#
# NOTE: base16Scheme is nullable. If null, we cannot pass it to mkSchemeAttrs.
# See https://github.com/nix-community/stylix/pull/1408
lib.stylix.colors =
if cfg.base16Scheme == null then
null
else
(cfg.base16.mkSchemeAttrs cfg.base16Scheme).override cfg.override;
assertions = lib.mkIf cfg.enable [
{
assertion = !(cfg.image == null && cfg.base16Scheme == null);
message = ''
stylix: one of `stylix.image` or `stylix.base16Scheme` must be set
'';
}
];
lib.stylix.colors = (cfg.base16.mkSchemeAttrs cfg.base16Scheme).override cfg.override;
stylix.generated.fileTree = {
# The raw output of the palette generator.