Files
home-manager/modules/misc/gtk/lib.nix
MithicSpirit 0b2f112917 gtk: fix gtk-interface-color-scheme
Fixes an issue introduced in [1] where gtk4 apps would print the
following warning:

> Error setting gtk-interface-color-scheme in
> /home/mithic/.config/gtk-4.0/settings.ini: Key file contains key
> “gtk-interface-color-scheme” which has a value that cannot be
> interpreted.

The value should be set to the nickname ("light" or "dark" in this
case), rather than the actual numerical value of the enum.  This lookup
happens at [2].

These nicknames appear to be undocumented and automatically generated at
compile time, but in general they appear to follow the pattern where an
enum value FOO_BAR_BAZ_QUUX of the enum FOO_BAR has nickname "baz-quux".
Regardless, they can be looked up in the generated file
gtktypebuiltins.c (it unfortunately appears that nix only keeps
gtktypebuiltins.h, which does not contain the nicknames).

[1] https://github.com/nix-community/home-manager/pull/7763
[2] https://gitlab.gnome.org/GNOME/gtk/-/blob/4.22.4/gtk/gtksettings.c#L1863
2026-09-22 21:00:14 -05:00

101 lines
2.9 KiB
Nix

{ lib }:
let
inherit (lib)
literalExpression
mkOption
optionalAttrs
optionalString
types
;
in
{
# Generic theme type factory function
mkThemeType =
{
typeName,
hasSize ? false,
packageExample,
nameExample ? "Adwaita",
}:
types.submodule {
options = {
package = mkOption {
type = types.nullOr types.package;
default = null;
example = literalExpression packageExample;
description = ''
Package providing the ${typeName}. This package will be installed
to your profile. If `null` then the ${typeName}
is assumed to already be available in your profile.
''
+ optionalString (typeName == "theme") ''
For the theme to apply to GTK 4, this option is mandatory.
'';
};
name = mkOption {
type = types.str;
example = nameExample;
description = "The name of the ${typeName} within the package.";
};
}
// optionalAttrs hasSize {
size = mkOption {
type = types.nullOr types.int;
default = null;
example = 16;
description = "The size of the cursor.";
};
};
};
# Helper function to generate the settings attribute set for a given version
mkGtkSettings =
{
gtkVersion,
font,
theme,
iconTheme,
cursorTheme,
colorScheme,
}:
optionalAttrs (font != null) {
gtk-font-name =
let
fontSize = if font.size != null then font.size else 11;
in
"${font.name} ${toString fontSize}";
}
// optionalAttrs (theme != null) { "gtk-theme-name" = theme.name; }
// optionalAttrs (iconTheme != null) { "gtk-icon-theme-name" = iconTheme.name; }
// optionalAttrs (cursorTheme != null) { "gtk-cursor-theme-name" = cursorTheme.name; }
// optionalAttrs (cursorTheme != null && cursorTheme.size != null) {
"gtk-cursor-theme-size" = cursorTheme.size;
}
// optionalAttrs (colorScheme == "dark") { "gtk-application-prefer-dark-theme" = true; }
// optionalAttrs (gtkVersion == 4 && colorScheme != null) {
"gtk-interface-color-scheme" = colorScheme;
};
# Package collection helper for all GTK versions
collectGtkPackages =
versionConfigs:
let
collectPackages =
cfgVersion:
lib.filter (pkg: pkg != null) (
lib.optionals cfgVersion.enable (
lib.optionals (cfgVersion.theme != null) [ cfgVersion.theme.package ]
++ lib.optionals (cfgVersion.iconTheme != null) [ cfgVersion.iconTheme.package ]
++ lib.optionals (cfgVersion.cursorTheme != null) [ cfgVersion.cursorTheme.package ]
++ lib.optionals (cfgVersion.font != null) [ cfgVersion.font.package ]
)
);
allPackages = lib.concatMap collectPackages versionConfigs;
in
lib.unique allPackages;
}