home-manager/modules/misc/gtk.nix
polykernel c7592b747b
treewide: prefer XDG variables over dot directories
Currently, dot directories and XDG base directories are used
inconsistently in the Home Manager option declarations. This creates
ambiguity for the user as to where the location of the file should be
albeit this is rarely encountered in practice as it is sufficient to
read upstream documentation. The rationale is to make declarations
consistent and make a clear distinction between hardcoded and modular
specifications.

References to ~/.config in relevant nixpkgs modules were untouched as
the location is hardcoded upstream[1]. Furthermore, modules of
programs which do not follow XDG specifications were also untouched.

Generalization of tilde(~) expansions to $HOME were also considered,
however there isn't sufficient rationale despite the use of $HOME
being more universal. The expansion is standardized in POSIX[2] and is
essentially portable across all shells, thus there is no pragmatic
value to introducing the change.

[1] https://github.com/nixos/nixpkgs/blob/master/pkgs/top-level/impure.nix
[2] https://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_01
2021-12-10 23:51:44 +01:00

187 lines
5.2 KiB
Nix

{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.gtk;
cfg2 = config.gtk.gtk2;
cfg3 = config.gtk.gtk3;
toGtk3Ini = generators.toINI {
mkKeyValue = key: value:
let
value' = if isBool value then
(if value then "true" else "false")
else
toString value;
in "${key}=${value'}";
};
formatGtk2Option = n: v:
let
v' = if isBool v then
(if v then "true" else "false")
else if isString v then
''"${v}"''
else
toString v;
in "${n} = ${v'}";
themeType = types.submodule {
options = {
package = mkOption {
type = types.nullOr types.package;
default = null;
example = literalExpression "pkgs.gnome.gnome_themes_standard";
description = ''
Package providing the theme. This package will be installed
to your profile. If <literal>null</literal> then the theme
is assumed to already be available in your profile.
'';
};
name = mkOption {
type = types.str;
example = "Adwaita";
description = "The name of the theme within the package.";
};
};
};
in {
meta.maintainers = [ maintainers.rycee ];
imports = [
(mkRemovedOptionModule [ "gtk" "gtk3" "waylandSupport" ] ''
This options is not longer needed and can be removed.
'')
];
options = {
gtk = {
enable = mkEnableOption "GTK 2/3 configuration";
font = mkOption {
type = types.nullOr hm.types.fontType;
default = null;
description = ''
The font to use in GTK+ 2/3 applications.
'';
};
iconTheme = mkOption {
type = types.nullOr themeType;
default = null;
description = "The icon theme to use.";
};
theme = mkOption {
type = types.nullOr themeType;
default = null;
description = "The GTK+2/3 theme to use.";
};
gtk2 = {
extraConfig = mkOption {
type = types.lines;
default = "";
example = "gtk-can-change-accels = 1";
description = ''
Extra configuration lines to add verbatim to
<filename>~/.gtkrc-2.0</filename>.
'';
};
configLocation = mkOption {
type = types.path;
default = "${config.home.homeDirectory}/.gtkrc-2.0";
defaultText =
literalExpression ''"''${config.home.homeDirectory}/.gtkrc-2.0"'';
example =
literalExpression ''"''${config.xdg.configHome}/gtk-2.0/gtkrc"'';
description = ''
The location to put the GTK configuration file.
'';
};
};
gtk3 = {
bookmarks = mkOption {
type = types.listOf types.str;
default = [ ];
example = [ "file:///home/jane/Documents" ];
description = "Bookmarks in the sidebar of the GTK file browser";
};
extraConfig = mkOption {
type = with types; attrsOf (either bool (either int str));
default = { };
example = {
gtk-cursor-blink = false;
gtk-recent-files-limit = 20;
};
description = ''
Extra configuration options to add to
<filename>$XDG_CONFIG_HOME/gtk-3.0/settings.ini</filename>.
'';
};
extraCss = mkOption {
type = types.lines;
default = "";
description = ''
Extra configuration lines to add verbatim to
<filename>$XDG_CONFIG_HOME/gtk-3.0/gtk.css</filename>.
'';
};
};
};
};
config = mkIf cfg.enable (let
ini = optionalAttrs (cfg.font != null) {
gtk-font-name = let
fontSize =
optionalString (cfg.font.size != null) " ${toString cfg.font.size}";
in "${cfg.font.name}" + fontSize;
} // optionalAttrs (cfg.theme != null) { gtk-theme-name = cfg.theme.name; }
// optionalAttrs (cfg.iconTheme != null) {
gtk-icon-theme-name = cfg.iconTheme.name;
};
dconfIni = optionalAttrs (cfg.font != null) {
font-name = let
fontSize =
optionalString (cfg.font.size != null) " ${toString cfg.font.size}";
in "${cfg.font.name}" + fontSize;
} // optionalAttrs (cfg.theme != null) { gtk-theme = cfg.theme.name; }
// optionalAttrs (cfg.iconTheme != null) {
icon-theme = cfg.iconTheme.name;
};
optionalPackage = opt:
optional (opt != null && opt.package != null) opt.package;
in {
home.packages = optionalPackage cfg.font ++ optionalPackage cfg.theme
++ optionalPackage cfg.iconTheme;
home.file.${cfg2.configLocation}.text =
concatMapStrings (l: l + "\n") (mapAttrsToList formatGtk2Option ini)
+ cfg2.extraConfig;
home.sessionVariables.GTK2_RC_FILES = cfg2.configLocation;
xdg.configFile."gtk-3.0/settings.ini".text =
toGtk3Ini { Settings = ini // cfg3.extraConfig; };
xdg.configFile."gtk-3.0/gtk.css".text = cfg3.extraCss;
xdg.configFile."gtk-3.0/bookmarks" = mkIf (cfg3.bookmarks != [ ]) {
text = concatMapStrings (l: l + "\n") cfg3.bookmarks;
};
dconf.settings."org/gnome/desktop/interface" = dconfIni;
});
}