diff --git a/modules/misc/gtk.nix b/modules/misc/gtk.nix index d3f1c40f8..2409376cb 100644 --- a/modules/misc/gtk.nix +++ b/modules/misc/gtk.nix @@ -73,6 +73,17 @@ in default = null; description = "Default cursor theme for all GTK versions."; }; + + colorScheme = mkOption { + type = types.nullOr ( + types.enum [ + "dark" + "light" + ] + ); + default = null; + description = "Default color scheme for all GTK versions"; + }; }; config = mkIf cfg.enable { diff --git a/modules/misc/gtk/gtk2.nix b/modules/misc/gtk/gtk2.nix index 0450350d6..ec2d91514 100644 --- a/modules/misc/gtk/gtk2.nix +++ b/modules/misc/gtk/gtk2.nix @@ -103,12 +103,14 @@ in text = let settings = gtkLib.mkGtkSettings { + gtkVersion = 2; inherit (cfg2) font theme iconTheme cursorTheme ; + colorScheme = null; }; settingsText = lib.concatMapStrings (n: "${formatGtk2Option n settings.${n}}\n") ( lib.attrNames settings diff --git a/modules/misc/gtk/gtk3.nix b/modules/misc/gtk/gtk3.nix index a085a2cc1..2c5b292e0 100644 --- a/modules/misc/gtk/gtk3.nix +++ b/modules/misc/gtk/gtk3.nix @@ -76,6 +76,18 @@ in description = "Cursor theme for GTK 3 applications."; }; + colorScheme = mkOption { + type = types.nullOr ( + types.enum [ + "dark" + "light" + ] + ); + default = cfg.colorScheme; + defaultText = literalExpression "config.gtk.colorScheme"; + description = "Color scheme for GTK 3 applications."; + }; + extraConfig = mkOption { type = with types; @@ -112,11 +124,13 @@ in text = toIni { Settings = gtkLib.mkGtkSettings { + gtkVersion = 3; inherit (cfg3) font theme iconTheme cursorTheme + colorScheme ; } // cfg3.extraConfig; @@ -134,11 +148,13 @@ in dconf.settings."org/gnome/desktop/interface" = let settings = gtkLib.mkGtkSettings { + gtkVersion = 3; inherit (cfg3) font theme iconTheme cursorTheme + colorScheme ; }; in @@ -148,6 +164,7 @@ in "icon-theme" = settings."gtk-icon-theme-name" or null; "cursor-theme" = settings."gtk-cursor-theme-name" or null; "cursor-size" = settings."gtk-cursor-theme-size" or null; + "color-scheme" = if cfg3.colorScheme != null then "prefer-${cfg3.colorScheme}" else null; }; }; } diff --git a/modules/misc/gtk/gtk4.nix b/modules/misc/gtk/gtk4.nix index a8d72cdb7..7e5c4f0bc 100644 --- a/modules/misc/gtk/gtk4.nix +++ b/modules/misc/gtk/gtk4.nix @@ -76,6 +76,18 @@ in description = "Cursor theme for GTK 4 applications."; }; + colorScheme = mkOption { + type = types.nullOr ( + types.enum [ + "dark" + "light" + ] + ); + default = cfg.colorScheme; + defaultText = literalExpression "config.gtk.colorScheme"; + description = "Color scheme for GTK 4 applications."; + }; + extraConfig = mkOption { type = with types; @@ -105,11 +117,13 @@ in text = toIni { Settings = gtkLib.mkGtkSettings { + gtkVersion = 4; inherit (cfg4) font theme iconTheme cursorTheme + colorScheme ; } // cfg4.extraConfig; diff --git a/modules/misc/gtk/lib.nix b/modules/misc/gtk/lib.nix index d524f15f0..df36c5261 100644 --- a/modules/misc/gtk/lib.nix +++ b/modules/misc/gtk/lib.nix @@ -55,10 +55,12 @@ in # 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 = @@ -72,7 +74,10 @@ in // 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 == "dark") { "gtk-interface-color-scheme" = 2; } + // optionalAttrs (gtkVersion == 4 && colorScheme == "light") { "gtk-interface-color-scheme" = 3; }; # Package collection helper for all GTK versions collectGtkPackages = diff --git a/tests/modules/misc/gtk/default.nix b/tests/modules/misc/gtk/default.nix index 016658a69..7d28a4f9d 100644 --- a/tests/modules/misc/gtk/default.nix +++ b/tests/modules/misc/gtk/default.nix @@ -3,6 +3,9 @@ gtk-per-version-override = ./gtk-per-version-override.nix; gtk-selective-enable = ./gtk-selective-enable.nix; + # ColorScheme tests + gtk-colorscheme = ./gtk-colorscheme.nix; + # GTK2 gtk2-basic-config = ./gtk2/gtk2-basic-config.nix; gtk2-config-file-location = ./gtk2/gtk2-config-file-location.nix; diff --git a/tests/modules/misc/gtk/gtk-colorscheme.nix b/tests/modules/misc/gtk/gtk-colorscheme.nix new file mode 100644 index 000000000..c35c604d8 --- /dev/null +++ b/tests/modules/misc/gtk/gtk-colorscheme.nix @@ -0,0 +1,30 @@ +{ pkgs, ... }: +{ + gtk = { + enable = true; + theme.name = "Adwaita"; + iconTheme.name = "Adwaita"; + + # Global dark colorScheme + colorScheme = "dark"; + + # GTK3 overrides to light + gtk3.colorScheme = "light"; + }; + + nmt.script = '' + # GTK3's settings should be untouched + assertFileExists home-files/.config/gtk-3.0/settings.ini + assertFileNotRegex home-files/.config/gtk-3.0/settings.ini \ + 'gtk-application-prefer-dark-theme' + + # GTK4 should inherit global dark colorScheme + assertFileExists home-files/.config/gtk-4.0/settings.ini + assertFileContains home-files/.config/gtk-4.0/settings.ini \ + 'gtk-application-prefer-dark-theme=true' + assertFileContains home-files/.config/gtk-4.0/settings.ini \ + 'gtk-interface-color-scheme=2' + + echo "ColorScheme test completed successfully" + ''; +} diff --git a/tests/modules/misc/gtk/gtk-global-inheritance-gtk3-expected.ini b/tests/modules/misc/gtk/gtk-global-inheritance-gtk3-expected.ini index 5ab5e63c4..820e72100 100644 --- a/tests/modules/misc/gtk/gtk-global-inheritance-gtk3-expected.ini +++ b/tests/modules/misc/gtk/gtk-global-inheritance-gtk3-expected.ini @@ -1,4 +1,5 @@ [Settings] +gtk-application-prefer-dark-theme=true gtk-cursor-theme-name=Adwaita gtk-cursor-theme-size=24 gtk-font-name=Ubuntu 12 diff --git a/tests/modules/misc/gtk/gtk-global-inheritance-gtk4-expected.ini b/tests/modules/misc/gtk/gtk-global-inheritance-gtk4-expected.ini index 5ab5e63c4..8e08f8d5d 100644 --- a/tests/modules/misc/gtk/gtk-global-inheritance-gtk4-expected.ini +++ b/tests/modules/misc/gtk/gtk-global-inheritance-gtk4-expected.ini @@ -1,6 +1,8 @@ [Settings] +gtk-application-prefer-dark-theme=true gtk-cursor-theme-name=Adwaita gtk-cursor-theme-size=24 gtk-font-name=Ubuntu 12 gtk-icon-theme-name=Adwaita +gtk-interface-color-scheme=2 gtk-theme-name=Adwaita-dark diff --git a/tests/modules/misc/gtk/gtk-global-inheritance.nix b/tests/modules/misc/gtk/gtk-global-inheritance.nix index 8ad2edc22..3d01f4843 100644 --- a/tests/modules/misc/gtk/gtk-global-inheritance.nix +++ b/tests/modules/misc/gtk/gtk-global-inheritance.nix @@ -19,6 +19,7 @@ name = "Adwaita"; size = 24; }; + colorScheme = "dark"; }; nmt.script = ''