aboutsummaryrefslogtreecommitdiff
path: root/home-manager/modules/misc/gtk.nix
blob: 1222db4ecab82e4438bfc8ec20d2fe68b7516026 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
{ 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'}";

  fontType = types.submodule {
    options = {
      package = mkOption {
        type = types.nullOr types.package;
        default = null;
        example = literalExample "pkgs.dejavu_fonts";
        description = ''
          Package providing the font. This package will be installed
          to your profile. If <literal>null</literal> then the font
          is assumed to already be available in your profile.
        '';
      };

      name = mkOption {
        type = types.str;
        example = "DejaVu Sans 8";
        description = ''
          The family name and size of the font within the package.
        '';
      };
    };
  };

  themeType = types.submodule {
    options = {
      package = mkOption {
        type = types.nullOr types.package;
        default = null;
        example = literalExample "pkgs.gnome3.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 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>.
          '';
        };
      };

      gtk3 = {
        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>~/.config/gtk-3.0/settings.ini</filename>.
          '';
        };

        extraCss = mkOption {
          type = types.lines;
          default = "";
          description = ''
            Extra configuration lines to add verbatim to
            <filename>~/.config/gtk-3.0/gtk.css</filename>.
          '';
        };
      };
    };
  };

  config = mkIf cfg.enable (
    let
      ini =
        optionalAttrs (cfg.font != null)
          { gtk-font-name = cfg.font.name; }
        //
        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 = cfg.font.name; }
        //
        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.".gtkrc-2.0".text =
          concatStringsSep "\n" (
            mapAttrsToList formatGtk2Option ini
          ) + "\n" + cfg2.extraConfig;

        xdg.configFile."gtk-3.0/settings.ini".text =
          toGtk3Ini { Settings = ini // cfg3.extraConfig; };

        xdg.configFile."gtk-3.0/gtk.css".text = cfg3.extraCss;

        dconf.settings."org/gnome/desktop/interface" = dconfIni;
      }
    );
}