From 0e2dc4be3006ef8354d782d34301f9a09946cfc2 Mon Sep 17 00:00:00 2001 From: Thiago Kenji Okada Date: Thu, 4 Mar 2021 00:20:17 -0300 Subject: [PATCH] qt: add qt.style option (#1839) This allows you to set a theme for Qt applications. For example, if you want to use `adwaita-qt` theme to have uniform look between Gtk and Qt applications, you can use it like this: ```nix { qt = { enable = true; platformTheme = "gnome"; style = { name = "adwaita"; package = pkgs.adwaita-qt; }; }; } ``` --- modules/misc/qt.nix | 59 ++++++++++++++++++- tests/default.nix | 1 + tests/modules/misc/qt/default.nix | 4 ++ .../misc/qt/qt-platform-theme-gnome.nix | 27 +++++++++ .../modules/misc/qt/qt-platform-theme-gtk.nix | 15 +++++ 5 files changed, 103 insertions(+), 3 deletions(-) create mode 100644 tests/modules/misc/qt/default.nix create mode 100644 tests/modules/misc/qt/qt-platform-theme-gnome.nix create mode 100644 tests/modules/misc/qt/qt-platform-theme-gtk.nix diff --git a/modules/misc/qt.nix b/modules/misc/qt.nix index ff38f842c810..1fdaf9f9b949 100644 --- a/modules/misc/qt.nix +++ b/modules/misc/qt.nix @@ -44,19 +44,72 @@ in { ''; }; + + style = { + name = mkOption { + type = types.nullOr types.str; + default = null; + example = "adwaita-dark"; + relatedPackages = [ "adwaita-qt" [ "libsForQt5" "qtstyleplugins" ] ]; + description = '' + Selects the style to use for Qt5 applications. + The options are + + + adwaita + adwaita-dark + Use Adwaita Qt style with + adwaita + + + + cleanlooks + gtk2 + motif + plastique + Use styles from + qtstyleplugins + + + + ''; + }; + + package = mkOption { + type = types.nullOr types.package; + default = null; + example = literalExample "pkgs.adwaita-qt"; + description = "Theme package to be used in Qt5 applications."; + }; + }; }; }; config = mkIf (cfg.enable && cfg.platformTheme != null) { - home.sessionVariables.QT_QPA_PLATFORMTHEME = - if cfg.platformTheme == "gnome" then "gnome" else "gtk2"; + assertions = [{ + assertion = (cfg.platformTheme == "gnome") + -> ((cfg.style.name != null) && (cfg.style.package != null)); + message = '' + `qt.platformTheme` "gnome" must have `qt.style` set to a theme that + supports both Qt and Gtk, for example "adwaita" or "adwaita-dark". + ''; + }]; + + # Necessary because home.sessionVariables is of types.attrs + home.sessionVariables = (filterAttrs (n: v: v != null) { + QT_QPA_PLATFORMTHEME = + if cfg.platformTheme == "gnome" then "gnome" else "gtk2"; + QT_STYLE_OVERRIDE = cfg.style.name; + }); home.packages = if cfg.platformTheme == "gnome" then [ pkgs.qgnomeplatform ] + ++ lib.optionals (cfg.style.package != null) [ cfg.style.package ] else [ pkgs.libsForQt5.qtstyleplugins ]; - xsession.importedVariables = [ "QT_QPA_PLATFORMTHEME" ]; + xsession.importedVariables = [ "QT_QPA_PLATFORMTHEME" ] + ++ lib.optionals (cfg.style != null) [ "QT_STYLE_OVERRIDE" ]; # Enable GTK+ style for Qt4 in either case. # It doesn’t support the platform theme packages. diff --git a/tests/default.nix b/tests/default.nix index ba5de77f29c1..50bd590ffdaf 100644 --- a/tests/default.nix +++ b/tests/default.nix @@ -84,6 +84,7 @@ import nmt { ./modules/misc/debug ./modules/misc/numlock ./modules/misc/pam + ./modules/misc/qt ./modules/misc/xdg ./modules/misc/xsession ./modules/programs/abook diff --git a/tests/modules/misc/qt/default.nix b/tests/modules/misc/qt/default.nix new file mode 100644 index 000000000000..c676b784e550 --- /dev/null +++ b/tests/modules/misc/qt/default.nix @@ -0,0 +1,4 @@ +{ + qt-platform-theme-gtk = ./qt-platform-theme-gtk.nix; + qt-platform-theme-gnome = ./qt-platform-theme-gnome.nix; +} diff --git a/tests/modules/misc/qt/qt-platform-theme-gnome.nix b/tests/modules/misc/qt/qt-platform-theme-gnome.nix new file mode 100644 index 000000000000..e9aa1a250632 --- /dev/null +++ b/tests/modules/misc/qt/qt-platform-theme-gnome.nix @@ -0,0 +1,27 @@ +{ config, lib, pkgs, ... }: + +{ + config = { + qt = { + enable = true; + platformTheme = "gnome"; + style = { + name = "adwaita"; + package = pkgs.dummyTheme; + }; + }; + + nixpkgs.overlays = [ + (self: super: { + dummyTheme = pkgs.runCommandLocal "theme" { } "mkdir $out"; + }) + ]; + + nmt.script = '' + assertFileRegex home-path/etc/profile.d/hm-session-vars.sh \ + 'QT_QPA_PLATFORMTHEME="gnome"' + assertFileRegex home-path/etc/profile.d/hm-session-vars.sh \ + 'QT_STYLE_OVERRIDE="adwaita"' + ''; + }; +} diff --git a/tests/modules/misc/qt/qt-platform-theme-gtk.nix b/tests/modules/misc/qt/qt-platform-theme-gtk.nix new file mode 100644 index 000000000000..d332465283ad --- /dev/null +++ b/tests/modules/misc/qt/qt-platform-theme-gtk.nix @@ -0,0 +1,15 @@ +{ config, lib, pkgs, ... }: + +{ + config = { + qt = { + enable = true; + platformTheme = "gtk"; + }; + + nmt.script = '' + assertFileRegex home-path/etc/profile.d/hm-session-vars.sh \ + 'QT_QPA_PLATFORMTHEME="gtk2"' + ''; + }; +}