gtk: use mkTarget (#1596)

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

Reviewed-by: NAHO <90870942+trueNAHO@users.noreply.github.com>
This commit is contained in:
awwpotato 2025-07-08 14:50:08 -07:00 committed by GitHub
parent d9338c105c
commit 0b8ec64a42
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 91 additions and 86 deletions

View file

@ -1,28 +1,16 @@
{
mkTarget,
pkgs,
config,
lib,
options,
...
}:
mkTarget {
name = "gtk";
humanName = "all GTK3, GTK4 and Libadwaita apps";
let
cfg = config.stylix.targets.gtk;
baseCss = config.lib.stylix.colors {
template = ./gtk.css.mustache;
extension = ".css";
};
finalCss = pkgs.runCommandLocal "gtk.css" { } ''
cat ${baseCss} >>$out
echo ${lib.escapeShellArg cfg.extraCss} >>$out
'';
in
{
options.stylix.targets.gtk = {
enable = config.lib.stylix.mkEnableTarget "all GTK3, GTK4 and Libadwaita apps" true;
extraOptions = {
extraCss = lib.mkOption {
description = ''
Extra code added to `gtk-3.0/gtk.css` and `gtk-4.0/gtk.css`.
@ -38,76 +26,94 @@ in
flatpakSupport.enable = config.lib.stylix.mkEnableTarget "support for theming Flatpak apps" true;
};
config = lib.mkIf (config.stylix.enable && cfg.enable) (
lib.mkMerge [
{
warnings =
lib.optional (config.gtk.gtk3.extraCss != "" || config.gtk.gtk4.extraCss != "")
"stylix: `gtk.gtk3.extraCss` and `gtk.gtk4.extraCss` have no effect. Use `stylix.targets.gtk.extraCss` instead.";
configElements = [
{
warnings =
lib.optional (config.gtk.gtk3.extraCss != "" || config.gtk.gtk4.extraCss != "")
"stylix: `gtk.gtk3.extraCss` and `gtk.gtk4.extraCss` have no effect. Use `stylix.targets.gtk.extraCss` instead.";
# 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 = {
# programs.dconf.enable = true; required in system config
gtk.enable = true;
}
(
{ fonts }:
{
gtk.font = {
inherit (fonts.sansSerif) package name;
size = fonts.sizes.applications;
};
}
)
(
{ cfg, colors }:
let
baseCss = colors {
template = ./gtk.css.mustache;
extension = ".css";
};
finalCss = pkgs.runCommandLocal "gtk.css" { } ''
cat ${baseCss} >>$out
echo ${lib.escapeShellArg cfg.extraCss} >>$out
'';
in
lib.mkMerge [
{
gtk.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}";
(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
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
{
name = "flattenedGtkTheme";
src = "${config.gtk.theme.package}/share/themes/${config.gtk.theme.name}";
# 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}
installPhase = ''
cp --recursive . $out
cat ${finalCss} | tee --append $out/gtk-{3,4}.0/gtk.css
[Environment]
GTK_THEME=${theme}
'';
};
}
(
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}
'';
}
)
]
))
]
);
}
)
]
))
]
)
];
}

View file

@ -1,10 +1,9 @@
{ config, lib, ... }:
{ mkTarget, ... }:
mkTarget {
name = "gtk";
humanName = "all GTK3, GTK4 and Libadwaita apps";
{
options.stylix.targets.gtk.enable =
config.lib.stylix.mkEnableTarget "all GTK3, GTK4 and Libadwaita apps" true;
config = lib.mkIf (config.stylix.enable && config.stylix.targets.gtk.enable) {
configElements = {
# Required for Home Manager's GTK settings to work
programs.dconf.enable = true;
};