about summary refs log tree commit diff
path: root/nixos/modules/config/gtk/gtk.nix
blob: 22f1e5d748359054cf396809360ebac7fd1fc383 (plain) (blame)
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
{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.gtk;
  gtk2 = cfg.enable && cfg.gtk2;

  toGtk2File = key: value:
    let
      value' =
        if isBool value then (if value then "true" else "false")
        else if isString value then "\"${value}\""
        else toString value;
    in
      "${key} = ${value'}";
  toGtk3File = generators.toINI {
    mkKeyValue = key: value:
      let
        value' =
          if isBool value then (if value then "true" else "false")
          else toString value;
      in
        "${key}=${value'}";
  };

  settings =
    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; }
    //
    optionalAttrs (cfg.cursorTheme != null)
      { gtk-cursor-theme-name = cfg.cursorTheme.name; };

  fontType = types.submodule {
    options = {
      package = mkOption {
        internal = true;
        type = types.nullOr types.package;
        default = null;
      };
      name = mkOption {
        internal = true;
        type = types.str;
      };
    };
  };
  themeType = types.submodule {
    options = {
      package = mkOption {
        internal = true;
        type = types.nullOr types.package;
        default = null;
      };
      name = mkOption {
        internal = true;
        type = types.str;
      };
    };
  };

  optionalPackage = opt:
    optional (opt != null && opt.package != null) opt.package;
in
{
  options = {
    gtk = {
      enable = mkEnableOption "Gtk theming configuration";

      gtk2 = mkOption {
        type = types.bool;
        default = true;
        description = ''
          Whether to enable theming for obsolete GTK2 engine.
        '';
      };

      font = mkOption {
        type = types.nullOr fontType;
        default = null;
        example = literalExample ''
          {
            name = "Cantarell 11";
            package = pkgs.cantarell-fonts;
          };
        '';
        description = ''
          The font to use in GTK+ applications.
        '';
      };

      iconTheme = mkOption {
        type = types.nullOr themeType;
        default = null;
        example = literalExample ''
          {
            name = "Adwaita";
            package = pkgs.gnome3.adwaita-icon-theme;
          };
        '';
        description = "The icon theme to use.";
      };

      cursorTheme = mkOption {
        type = types.nullOr themeType;
        default = null;
        example = literalExample ''
          {
            name = "Adwaita";
            package = pkgs.gnome3.adwaita-icon-theme;
          };
        '';
        description = "The cursor theme to use.";
      };

      theme = mkOption {
        type = types.nullOr themeType;
        default = null;
        example = literalExample ''
          {
            name = "Adwaita";
            package = pkgs.gnome-themes-extra;
          };
        '';
        description = "The GTK+ theme to use.";
      };
    };
  };

  config = mkMerge [

    (mkIf gtk2 {
      environment.etc."xdg/gtk-2.0/gtkrc".text =
        concatStringsSep "\n" (
          mapAttrsToList toGtk2File settings
        );
    })

    (mkIf cfg.enable {
      environment.systemPackages =
        optionalPackage cfg.font
        ++ optionalPackage cfg.theme
        ++ optionalPackage cfg.iconTheme
        ++ optionalPackage cfg.cursorTheme;

      environment.etc."xdg/gtk-3.0/settings.ini".text =
        toGtk3File { Settings = settings; };

      # TODO: support Wayland/XSettings
      # once https://github.com/NixOS/nixpkgs/issues/54150 is fixed
    })
  ];

  meta.maintainers = [ maintainers.gnidorah ];
}