Skip to content

Commit

Permalink
gtk: add support for theming Flatpak applications (#693)
Browse files Browse the repository at this point in the history
Add support for theming Flatpak applications by making the GTK theme
accessible to them. [1]

[1]: https://itsfoss.com/flatpak-app-apply-theme

Closes: #465
Link: #693

Reviewed-by: NAHO <[email protected]>
Tested-by: NAHO <[email protected]>
(cherry picked from commit 963e77a)
  • Loading branch information
brckd authored and github-actions[bot] committed Jan 4, 2025
1 parent 13037a5 commit dd154ff
Showing 1 changed file with 62 additions and 18 deletions.
80 changes: 62 additions & 18 deletions modules/gtk/hm.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ pkgs, config, lib, ... }:
{ pkgs, config, lib, options, ... }:

let
cfg = config.stylix.targets.gtk;
Expand All @@ -12,7 +12,6 @@ let
cat ${baseCss} >>$out
echo ${lib.escapeShellArg cfg.extraCss} >>$out
'';

in {
options.stylix.targets.gtk = {
enable = config.lib.stylix.mkEnableTarget
Expand All @@ -29,25 +28,70 @@ in {
window.background { border-radius: 0; }
'';
};

flatpakSupport.enable =
config.lib.stylix.mkEnableTarget "support for theming Flatpak apps" true;
};

config = lib.mkIf cfg.enable {
# programs.dconf.enable = true; required in system config
gtk = {
enable = true;
font = {
inherit (config.stylix.fonts.sansSerif) package name;
size = config.stylix.fonts.sizes.applications;
config = lib.mkIf cfg.enable (lib.mkMerge [
{
# programs.dconf.enable = true; required in system config
gtk = {
enable = true;
font = {
inherit (config.stylix.fonts.sansSerif) package name;
size = config.stylix.fonts.sizes.applications;
};
theme = {
package = pkgs.adw-gtk3;
name = "adw-gtk3";
};
};
theme = {
package = pkgs.adw-gtk3;
name = "adw-gtk3";

xdg.configFile = {
"gtk-3.0/gtk.css".source = finalCss;
"gtk-4.0/gtk.css".source = finalCss;
};
};
}

xdg.configFile = {
"gtk-3.0/gtk.css".source = finalCss;
"gtk-4.0/gtk.css".source = finalCss;
};
};
(lib.mkIf cfg.flatpakSupport.enable (lib.mkMerge [
{
# Flatpak apps apparently don't consume the CSS config. This workaround appends it to the theme directly.
home.file.".themes/${config.gtk.theme.name}".source = pkgs.stdenvNoCC.mkDerivation {
name = "flattenedGtkTheme";
src = "${config.gtk.theme.package}/share/themes/${config.gtk.theme.name}";

installPhase = ''
cp --recursive . $out
cat ${finalCss} | tee --append $out/gtk-{3,4}.0/gtk.css
'';
};
}
(
let
filesystem = "${config.home.homeDirectory}/.themes/${config.gtk.theme.name}:ro";
theme = config.gtk.theme.name;
in
if options ? services.flatpak.overrides
then {
# Let Flatpak apps read the theme and force them to use it.
# This requires nix-flatpak to be imported externally.
services.flatpak.overrides.global = {
Context.filesystems = [filesystem];
Environment.GTK_THEME = theme;
};
}
else {
# This is likely incompatible with other modules that write to this file.
xdg.dataFile."flatpak/overrides/global".text = ''
[Context]
filesystems=${filesystem}
[Environment]
GTK_THEME=${theme}
'';
}
)
]))
]);
}

0 comments on commit dd154ff

Please sign in to comment.