fork/home-manager

git clone https://git.julia.blue/fork/home-manager

Robert HelgessonMerge PR #421575790441

master
4.5 KiB136 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7  cfg = config.dconf;
8
9  toDconfIni = generators.toINI { mkKeyValue = mkIniKeyValue; };
10
11  mkIniKeyValue = key: value: "${key}=${toString (hm.gvariant.mkValue value)}";
12
13  # The dconf keys managed by this configuration. We store this as part of the
14  # generation state to be able to reset keys that become unmanaged during
15  # switch.
16  stateDconfKeys = pkgs.writeText "dconf-keys.json" (builtins.toJSON
17    (concatLists (mapAttrsToList
18      (dir: entries: mapAttrsToList (key: _: "/${dir}/${key}") entries)
19      cfg.settings)));
20
21in {
22  meta.maintainers = [ maintainers.rycee ];
23
24  options = {
25    dconf = {
26      enable = mkOption {
27        type = types.bool;
28        # While technically dconf on darwin could work, our activation step
29        # requires dbus, which only *lightly* supports Darwin in general, and
30        # not at all in the way it's packaged in nixpkgs. Because of this, we
31        # just disable dconf for darwin hosts by default.
32        # In the future, if someone gets dbus working, this _could_ be
33        # re-enabled, unclear whether there's actual value in it though.
34        default = !pkgs.stdenv.hostPlatform.isDarwin;
35        visible = false;
36        description = ''
37          Whether to enable dconf settings.
38
39          Note, if you use NixOS then you must add
40          `programs.dconf.enable = true`
41          to your system configuration. Otherwise you will see a systemd error
42          message when your configuration is activated.
43        '';
44      };
45
46      settings = mkOption {
47        type = with types; attrsOf (attrsOf hm.types.gvariant);
48        default = { };
49        example = literalExpression ''
50          {
51            "org/gnome/calculator" = {
52              button-mode = "programming";
53              show-thousands = true;
54              base = 10;
55              word-size = 64;
56              window-position = lib.hm.gvariant.mkTuple [100 100];
57            };
58          }
59        '';
60        description = ''
61          Settings to write to the dconf configuration system.
62
63          Note that the database is strongly-typed so you need to use the same types
64          as described in the GSettings schema. For example, if an option is of type
65          `uint32` (`u`), you need to wrap the number
66          using the `lib.hm.gvariant.mkUint32` constructor.
67          Otherwise, since Nix integers are implicitly coerced to `int32`
68          (`i`), it would get stored in the database as such, and GSettings
69          might be confused when loading the setting.
70
71          You might want to use [dconf2nix](https://github.com/gvolpe/dconf2nix)
72          to convert dconf database dumps into compatible Nix expression.
73        '';
74      };
75    };
76  };
77
78  config = mkIf (cfg.enable && cfg.settings != { }) {
79    # Make sure the dconf directory exists.
80    xdg.configFile."dconf/.keep".source = builtins.toFile "keep" "";
81
82    home.extraBuilderCommands = ''
83      mkdir -p $out/state/
84      ln -s ${stateDconfKeys} $out/state/${stateDconfKeys.name}
85    '';
86
87    home.activation.dconfSettings = hm.dag.entryAfter [ "installPackages" ] (let
88      iniFile = pkgs.writeText "hm-dconf.ini" (toDconfIni cfg.settings);
89
90      statePath = "state/${stateDconfKeys.name}";
91
92      cleanup = pkgs.writeShellScript "dconf-cleanup" ''
93        set -euo pipefail
94
95        ${config.lib.bash.initHomeManagerLib}
96
97        PATH=${makeBinPath [ pkgs.dconf pkgs.jq ]}''${PATH:+:}$PATH
98
99        oldState="$1"
100        newState="$2"
101
102        # Can't do cleanup if we don't know the old state.
103        if [[ ! -f $oldState ]]; then
104          exit 0
105        fi
106
107        # Reset all keys that are present in the old generation but not the new
108        # one.
109        jq -r -n \
110            --slurpfile old "$oldState" \
111            --slurpfile new "$newState" \
112            '($old[] - $new[])[]' \
113          | while read -r key; do
114              $VERBOSE_ECHO "Resetting dconf key \"$key\""
115              $DRY_RUN_CMD $DCONF_DBUS_RUN_SESSION dconf reset "$key"
116            done
117      '';
118    in ''
119      if [[ -v DBUS_SESSION_BUS_ADDRESS ]]; then
120        export DCONF_DBUS_RUN_SESSION=""
121      else
122        export DCONF_DBUS_RUN_SESSION="${pkgs.dbus}/bin/dbus-run-session --dbus-daemon=${pkgs.dbus}/bin/dbus-daemon"
123      fi
124
125      if [[ -v oldGenPath ]]; then
126        ${cleanup} \
127          "$oldGenPath/${statePath}" \
128          "$newGenPath/${statePath}"
129      fi
130
131      $DRY_RUN_CMD $DCONF_DBUS_RUN_SESSION ${pkgs.dconf}/bin/dconf load / < ${iniFile}
132
133      unset DCONF_DBUS_RUN_SESSION
134    '');
135  };
136}