fork/home-manager
git clone https://git.julia.blue/fork/home-manager
75790441
master
1{ config , lib , pkgs , ...} : 2 3with lib ; 4 5let 6 7cfg = config . dconf ; 8 9toDconfIni = generators . toINI { mkKeyValue = mkIniKeyValue ; }; 10 11mkIniKeyValue = 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. 16stateDconfKeys = pkgs . writeText "dconf-keys.json" ( builtins . toJSON 17( concatLists ( mapAttrsToList 18( dir :entries :mapAttrsToList ( key :_ :"/ ${ dir } / ${ key } " ) entries ) 19cfg . settings ))); 20 21in { 22meta . maintainers = [ maintainers . rycee ]; 23 24options = { 25dconf = { 26enable = mkOption { 27type = 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. 34default = ! pkgs . stdenv . hostPlatform . isDarwin ; 35visible = false ; 36description = '' 37Whether to enable dconf settings. 38 39Note, if you use NixOS then you must add 40`programs.dconf.enable = true` 41to your system configuration. Otherwise you will see a systemd error 42message when your configuration is activated. 43'' ; 44}; 45 46settings = mkOption { 47type = with types ; attrsOf ( attrsOf hm . types . gvariant ); 48default = { }; 49example = literalExpression '' 50{ 51"org/gnome/calculator" = { 52button-mode = "programming"; 53show-thousands = true; 54base = 10; 55word-size = 64; 56window-position = lib.hm.gvariant.mkTuple [100 100]; 57}; 58} 59'' ; 60description = '' 61Settings to write to the dconf configuration system. 62 63Note that the database is strongly-typed so you need to use the same types 64as described in the GSettings schema. For example, if an option is of type 65`uint32` (`u`), you need to wrap the number 66using the `lib.hm.gvariant.mkUint32` constructor. 67Otherwise, since Nix integers are implicitly coerced to `int32` 68(`i`), it would get stored in the database as such, and GSettings 69might be confused when loading the setting. 70 71You might want to use [dconf2nix](https://github.com/gvolpe/dconf2nix) 72to convert dconf database dumps into compatible Nix expression. 73'' ; 74}; 75}; 76}; 77 78config = mkIf ( cfg . enable && cfg . settings != { }) { 79# Make sure the dconf directory exists. 80xdg . configFile . "dconf/.keep" . source = builtins . toFile "keep" "" ; 81 82home . extraBuilderCommands = '' 83mkdir -p $out/state/ 84ln -s ${ stateDconfKeys } $out/state/ ${ stateDconfKeys . name } 85'' ; 86 87home . activation . dconfSettings = hm . dag . entryAfter [ "installPackages" ] ( let 88iniFile = pkgs . writeText "hm-dconf.ini" ( toDconfIni cfg . settings ); 89 90statePath = "state/ ${ stateDconfKeys . name } " ; 91 92cleanup = pkgs . writeShellScript "dconf-cleanup" '' 93set -euo pipefail 94 95${ config . lib . bash . initHomeManagerLib } 96 97PATH = ${ makeBinPath [ pkgs . dconf pkgs . jq ]} '' $ { PATH:+:} $ PATH 98 99oldState =" $ 1 " 100newState =" $ 2 " 101 102# Can't do cleanup if we don't know the old state. 103if [[ ! -f $ oldState ]]; then 104exit 0 105fi 106 107# Reset all keys that are present in the old generation but not the new 108# one. 109jq -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 " 116done 117'' ; 118in '' 119if [[ -v DBUS_SESSION_BUS_ADDRESS ]]; then 120export DCONF_DBUS_RUN_SESSION="" 121else 122export DCONF_DBUS_RUN_SESSION=" ${ pkgs . dbus } /bin/dbus-run-session --dbus-daemon= ${ pkgs . dbus } /bin/dbus-daemon" 123fi 124 125if [[ -v oldGenPath ]]; then 126${ cleanup } \ 127"$oldGenPath/ ${ statePath } " \ 128"$newGenPath/ ${ statePath } " 129fi 130 131$DRY_RUN_CMD $DCONF_DBUS_RUN_SESSION ${ pkgs . dconf } /bin/dconf load / < ${ iniFile } 132 133unset DCONF_DBUS_RUN_SESSION 134'' ); 135}; 136}