1
0
Fork 0
mirror of https://github.com/nix-community/home-manager synced 2024-11-27 05:29:46 +01: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,21 +62,55 @@ 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.
jq -r -n \
--slurpfile old "$oldState" \
--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 else
$DCONF_DBUS_RUN_SESSION ${pkgs.dconf}/bin/dconf load / < ${iniFile} export DCONF_DBUS_RUN_SESSION="${pkgs.dbus}/bin/dbus-run-session"
fi fi
${cleanup} \
"$oldGenPath/${statePath}" \
"$newGenPath/${statePath}"
$DRY_RUN_CMD $DCONF_DBUS_RUN_SESSION ${pkgs.dconf}/bin/dconf load / < ${iniFile}
unset DCONF_DBUS_RUN_SESSION unset DCONF_DBUS_RUN_SESSION
''); '');
}; };