1
0
mirror of https://github.com/nix-community/home-manager synced 2024-06-29 01:48:31 +02:00

dconf: reset keys that become unmanaged on switch

This commit is contained in:
ilkecan 2022-07-01 21:33:43 +00:00 committed by Robert Helgesson
parent b37a909508
commit 5597b3a742
No known key found for this signature in database
GPG Key ID: 36BDAA14C2797E89

View File

@ -10,6 +10,14 @@ let
mkIniKeyValue = key: value: "${key}=${toString (hm.gvariant.mkValue value)}"; mkIniKeyValue = key: value: "${key}=${toString (hm.gvariant.mkValue value)}";
# The dconf keys managed by this configuration. We store this as part of the
# generation state to be able to reset keys that become unmanaged during
# switch.
stateDconfKeys = pkgs.writeText "dconf-keys.json" (builtins.toJSON
(concatLists (mapAttrsToList
(dir: entries: mapAttrsToList (key: _: "/${dir}/${key}") entries)
cfg.settings)));
in { in {
meta.maintainers = [ maintainers.rycee ]; meta.maintainers = [ maintainers.rycee ];
@ -54,22 +62,56 @@ in {
# Make sure the dconf directory exists. # Make sure the dconf directory exists.
xdg.configFile."dconf/.keep".source = builtins.toFile "keep" ""; xdg.configFile."dconf/.keep".source = builtins.toFile "keep" "";
home.activation.dconfSettings = hm.dag.entryAfter [ "installPackages" ] home.extraBuilderCommands = ''
(let iniFile = pkgs.writeText "hm-dconf.ini" (toDconfIni cfg.settings); mkdir -p $out/state/
in '' ln -s ${stateDconfKeys} $out/state/${stateDconfKeys.name}
if [[ -v DBUS_SESSION_BUS_ADDRESS ]]; then '';
DCONF_DBUS_RUN_SESSION=""
else home.activation.dconfSettings = hm.dag.entryAfter [ "installPackages" ] (let
DCONF_DBUS_RUN_SESSION="${pkgs.dbus}/bin/dbus-run-session" iniFile = pkgs.writeText "hm-dconf.ini" (toDconfIni cfg.settings);
statePath = "state/${stateDconfKeys.name}";
cleanup = pkgs.writeShellScript "dconf-cleanup" ''
set -euo pipefail
${config.lib.bash.initHomeManagerLib}
PATH=${makeBinPath [ pkgs.dconf pkgs.jq ]}''${PATH:+:}$PATH
oldState="$1"
newState="$2"
# Can't do cleanup if we don't know the old state.
if [[ ! -f $oldState ]]; then
exit 0
fi fi
if [[ -v DRY_RUN ]]; then # Reset all keys that are present in the old generation but not the new
echo $DCONF_DBUS_RUN_SESSION ${pkgs.dconf}/bin/dconf load / "<" ${iniFile} # one.
else jq -r -n \
$DCONF_DBUS_RUN_SESSION ${pkgs.dconf}/bin/dconf load / < ${iniFile} --slurpfile old "$oldState" \
fi --slurpfile new "$newState" \
'($old[] - $new[])[]' \
| while read -r key; do
$VERBOSE_ECHO "Resetting dconf key \"$key\""
$DRY_RUN_CMD $DCONF_DBUS_RUN_SESSION dconf reset "$key"
done
'';
in ''
if [[ -v DBUS_SESSION_BUS_ADDRESS ]]; then
export DCONF_DBUS_RUN_SESSION=""
else
export DCONF_DBUS_RUN_SESSION="${pkgs.dbus}/bin/dbus-run-session"
fi
unset DCONF_DBUS_RUN_SESSION ${cleanup} \
''); "$oldGenPath/${statePath}" \
"$newGenPath/${statePath}"
$DRY_RUN_CMD $DCONF_DBUS_RUN_SESSION ${pkgs.dconf}/bin/dconf load / < ${iniFile}
unset DCONF_DBUS_RUN_SESSION
'');
}; };
} }