fork/home-manager

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

Robert HelgessonMerge PR #421575790441

master
3.7 KiB137 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7  cfg = config.xfconf;
8
9  xfIntVariant = types.submodule {
10    options = {
11      type = mkOption {
12        type = types.enum [ "int" "uint" "uint64" ];
13        description = ''
14          To distinguish between int, uint and uint64 in xfconf,
15          you can specify the type in xfconf with this submodule.
16          For other types, you don't need to use this submodule,
17          just specify the value is enough.
18        '';
19      };
20      value = mkOption {
21        type = types.int;
22        description = "The value in xfconf.";
23      };
24    };
25  };
26
27  withType = v:
28    if builtins.isAttrs v then [
29      "-t"
30      v.type
31      "-s"
32      (toString v.value)
33    ] else if builtins.isBool v then [
34      "-t"
35      "bool"
36      "-s"
37      (if v then "true" else "false")
38    ] else if builtins.isInt v then [
39      "-t"
40      "int"
41      "-s"
42      (toString v)
43    ] else if builtins.isFloat v then [
44      "-t"
45      "double"
46      "-s"
47      (toString v)
48    ] else if builtins.isString v then [
49      "-t"
50      "string"
51      "-s"
52      v
53    ] else if builtins.isList v then
54      [ "-a" ] ++ concatMap withType v
55    else
56      throw "unexpected xfconf type: ${builtins.typeOf v}";
57
58in {
59  meta.maintainers = [ maintainers.chuangzhu ];
60
61  options.xfconf = {
62    enable = mkOption {
63      type = types.bool;
64      default = true;
65      visible = false;
66      description = ''
67        Whether to enable Xfconf settings.
68
69        Note, if you use NixOS then you must add
70        `programs.xfconf.enable = true`
71        to your system configuration. Otherwise you will see a systemd error
72        message when your configuration is activated.
73      '';
74    };
75
76    settings = mkOption {
77      type = with types;
78      # xfIntVariant must come AFTER str; otherwise strings are treated as submodule imports...
79        let value = nullOr (oneOf [ bool int float str xfIntVariant ]);
80        in attrsOf (attrsOf (either value (listOf value))) // {
81          description = "xfconf settings";
82        };
83      default = { };
84      example = literalExpression ''
85        {
86          xfce4-session = {
87            "startup/ssh-agent/enabled" = false;
88            "general/LockCommand" = "''${pkgs.lightdm}/bin/dm-tool lock";
89          };
90          xfce4-desktop = {
91            "backdrop/screen0/monitorLVDS-1/workspace0/last-image" =
92              "''${pkgs.nixos-artwork.wallpapers.stripes-logo.gnomeFilePath}";
93          };
94        }
95      '';
96      description = ''
97        Settings to write to the Xfconf configuration system.
98      '';
99    };
100  };
101
102  config = mkIf (cfg.enable && cfg.settings != { }) {
103    assertions =
104      [ (hm.assertions.assertPlatform "xfconf" pkgs platforms.linux) ];
105
106    home.activation.xfconfSettings = hm.dag.entryAfter [ "installPackages" ]
107      (let
108        mkCommand = channel: property: value: ''
109          $DRY_RUN_CMD ${pkgs.xfce.xfconf}/bin/xfconf-query \
110            ${
111              escapeShellArgs ([ "-c" channel "-p" "/${property}" ]
112                ++ (if value == null then
113                  [ "-r" ]
114                else
115                  [ "-n" ] ++ withType value))
116            }
117        '';
118
119        commands = mapAttrsToList
120          (channel: properties: mapAttrsToList (mkCommand channel) properties)
121          cfg.settings;
122
123        load = pkgs.writeShellScript "load-xfconf"
124          (concatMapStrings concatStrings commands);
125      in ''
126        if [[ -v DBUS_SESSION_BUS_ADDRESS ]]; then
127          export DBUS_RUN_SESSION_CMD=""
128        else
129          export DBUS_RUN_SESSION_CMD="${pkgs.dbus}/bin/dbus-run-session --dbus-daemon=${pkgs.dbus}/bin/dbus-daemon"
130        fi
131
132        $DRY_RUN_CMD $DBUS_RUN_SESSION_CMD ${load}
133
134        unset DBUS_RUN_SESSION_CMD
135      '');
136  };
137}