aboutsummaryrefslogtreecommitdiff
path: root/home-manager/modules/misc/dconf.nix
blob: f5c9bf7145648114a5d9b9b0e5c49dbc91af0098 (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
{ config, lib, pkgs, ... }:

with lib;

let

  cfg = config.dconf;

  toDconfIni = generators.toINI { mkKeyValue = mkIniKeyValue; };

  mkIniKeyValue = key: value:
    let
      tweakVal = v:
        if isString v then "'${v}'"
        else if isList v then tweakList v
        else if isBool v then (if v then "true" else "false")
        else toString v;

      # Assume empty list is a list of strings, see #769
      tweakList = v:
        if v == [] then "@as []"
        else "[" + concatMapStringsSep "," tweakVal v + "]";

    in
      "${key}=${tweakVal value}";

  primitive = with types; either bool (either int (either float str));

in

{
  meta.maintainers = [ maintainers.gnidorah maintainers.rycee ];

  options = {
    dconf = {
      enable = mkOption {
        type = types.bool;
        default = true;
        visible = false;
        description = ''
          Whether to enable dconf settings.
        '';
      };

      settings = mkOption {
        type = with types;
          attrsOf (attrsOf (either primitive (listOf primitive)));
        default = {};
        example = literalExample ''
          {
            "org/gnome/calculator" = {
              button-mode = "programming";
              show-thousands = true;
              base = 10;
              word-size = 64;
            };
          }
        '';
        description = ''
          Settings to write to the dconf configuration system.
        '';
      };
    };
  };

  config = mkIf (cfg.enable && cfg.settings != {}) {
    home.activation.dconfSettings = hm.dag.entryAfter ["installPackages"] (
      let
        iniFile = pkgs.writeText "hm-dconf.ini" (toDconfIni cfg.settings);
      in
        ''
          if [[ -v DBUS_SESSION_BUS_ADDRESS ]]; then
            DCONF_DBUS_RUN_SESSION=""
          else
            DCONF_DBUS_RUN_SESSION="${pkgs.dbus}/bin/dbus-run-session"
          fi

          if [[ -v DRY_RUN ]]; then
            echo $DCONF_DBUS_RUN_SESSION ${pkgs.gnome3.dconf}/bin/dconf load / "<" ${iniFile}
          else
            $DCONF_DBUS_RUN_SESSION ${pkgs.gnome3.dconf}/bin/dconf load / < ${iniFile}
          fi

          unset DCONF_DBUS_RUN_SESSION
        ''
    );
  };
}