fork/home-manager

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

Robert HelgessonMerge PR #421575790441

master
8.9 KiB331 linesraw
1{ config, options, lib, pkgs, ... }:
2
3let
4  inherit (builtins) elemAt isAttrs isBool length mapAttrs toJSON;
5  inherit (lib)
6    boolToString concatMapStringsSep concatStringsSep escape literalExpression
7    mapAttrsToList mkEnableOption mkRenamedOptionModule mkRemovedOptionModule
8    mkDefault mkIf mkOption optional types warn;
9
10  cfg = config.services.picom;
11  opt = options.services.picom;
12
13  pairOf = x:
14    with types;
15    addCheck (listOf x) (y: length y == 2) // {
16      description = "pair of ${x.description}";
17    };
18
19  mkDefaultAttrs = mapAttrs (n: v: mkDefault v);
20
21  # Basically a tinkered lib.generators.mkKeyValueDefault
22  # It either serializes a top-level definition "key: { values };"
23  # or an expression "key = { values };"
24  mkAttrsString = top:
25    mapAttrsToList (k: v:
26      let sep = if (top && isAttrs v) then ": " else " = ";
27      in "${escape [ sep ] k}${sep}${mkValueString v};");
28
29  # This serializes a Nix expression to the libconfig format.
30  mkValueString = v:
31    if types.bool.check v then
32      boolToString v
33    else if types.int.check v then
34      toString v
35    else if types.float.check v then
36      toString v
37    else if types.str.check v then
38      ''"${escape [ ''"'' ] v}"''
39    else if builtins.isList v then
40      "[ ${concatMapStringsSep " , " mkValueString v} ]"
41    else if types.attrs.check v then
42      "{ ${concatStringsSep " " (mkAttrsString false v)} }"
43    else
44      throw ''
45        invalid expression used in option services.picom.settings:
46        ${v}
47      '';
48
49  toConf = attrs: concatStringsSep "\n" (mkAttrsString true cfg.settings);
50
51  configFile = toConf cfg.settings;
52
53in {
54  imports = [
55    (mkRemovedOptionModule [ "services" "picom" "refreshRate" ]
56      "The option `refresh-rate` has been deprecated by upstream.")
57    (mkRemovedOptionModule [ "services" "picom" "experimentalBackends" ]
58      "The option `--experimental-backends` has been removed by upstream.")
59    (mkRemovedOptionModule [ "services" "picom" "extraOptions" ]
60      "This option has been replaced by `services.picom.settings`.")
61    (mkRenamedOptionModule [ "services" "picom" "opacityRule" ] [
62      "services"
63      "picom"
64      "opacityRules"
65    ])
66  ];
67
68  options.services.picom = {
69    enable = mkEnableOption "Picom X11 compositor";
70
71    fade = mkOption {
72      type = types.bool;
73      default = false;
74      description = ''
75        Fade windows in and out.
76      '';
77    };
78
79    fadeDelta = mkOption {
80      type = types.ints.positive;
81      default = 10;
82      example = 5;
83      description = ''
84        Time between fade animation step (in ms).
85      '';
86    };
87
88    fadeSteps = mkOption {
89      type = pairOf (types.numbers.between 1.0e-2 1);
90      default = [ 2.8e-2 3.0e-2 ];
91      example = [ 4.0e-2 4.0e-2 ];
92      description = ''
93        Opacity change between fade steps (in and out).
94      '';
95    };
96
97    fadeExclude = mkOption {
98      type = types.listOf types.str;
99      default = [ ];
100      example = [ "window_type *= 'menu'" "name ~= 'Firefox$'" "focused = 1" ];
101      description = ''
102        List of conditions of windows that should not be faded.
103        See `picom(1)` man page for more examples.
104      '';
105    };
106
107    shadow = mkOption {
108      type = types.bool;
109      default = false;
110      description = ''
111        Draw window shadows.
112      '';
113    };
114
115    shadowOffsets = mkOption {
116      type = pairOf types.int;
117      default = [ (-15) (-15) ];
118      example = [ (-10) (-15) ];
119      description = ''
120        Left and right offset for shadows (in pixels).
121      '';
122    };
123
124    shadowOpacity = mkOption {
125      type = types.numbers.between 0 1;
126      default = 0.75;
127      example = 0.8;
128      description = ''
129        Window shadows opacity.
130      '';
131    };
132
133    shadowExclude = mkOption {
134      type = types.listOf types.str;
135      default = [ ];
136      example = [ "window_type *= 'menu'" "name ~= 'Firefox$'" "focused = 1" ];
137      description = ''
138        List of conditions of windows that should have no shadow.
139        See `picom(1)` man page for more examples.
140      '';
141    };
142
143    activeOpacity = mkOption {
144      type = types.numbers.between 0 1;
145      default = 1.0;
146      example = 0.8;
147      description = ''
148        Opacity of active windows.
149      '';
150    };
151
152    inactiveOpacity = mkOption {
153      type = types.numbers.between 0.1 1;
154      default = 1.0;
155      example = 0.8;
156      description = ''
157        Opacity of inactive windows.
158      '';
159    };
160
161    menuOpacity = mkOption {
162      type = types.numbers.between 0 1;
163      default = 1.0;
164      example = 0.8;
165      description = ''
166        Opacity of dropdown and popup menu.
167      '';
168    };
169
170    wintypes = mkOption {
171      type = types.attrs;
172      default = {
173        popup_menu = { opacity = cfg.menuOpacity; };
174        dropdown_menu = { opacity = cfg.menuOpacity; };
175      };
176      defaultText = literalExpression ''
177        {
178          popup_menu = { opacity = config.${opt.menuOpacity}; };
179          dropdown_menu = { opacity = config.${opt.menuOpacity}; };
180        }
181      '';
182      example = { };
183      description = ''
184        Rules for specific window types.
185      '';
186    };
187
188    opacityRules = mkOption {
189      type = types.listOf types.str;
190      default = [ ];
191      example = [
192        "95:class_g = 'URxvt' && !_NET_WM_STATE@:32a"
193        "0:_NET_WM_STATE@:32a *= '_NET_WM_STATE_HIDDEN'"
194      ];
195      description = ''
196        Rules that control the opacity of windows, in format PERCENT:PATTERN.
197      '';
198    };
199
200    backend = mkOption {
201      type = types.enum [ "egl" "glx" "xrender" "xr_glx_hybrid" ];
202      default = "xrender";
203      description = ''
204        Backend to use: `egl`, `glx`, `xrender` or `xr_glx_hybrid`.
205      '';
206    };
207
208    vSync = mkOption {
209      type = types.bool;
210      default = false;
211      description = ''
212        Enable vertical synchronization.
213      '';
214    };
215
216    extraArgs = mkOption {
217      type = with types; listOf str;
218      default = [ ];
219      example = literalExpression ''[ "--legacy-backends" ]'';
220      description = ''
221        Extra arguments to be passed to the picom executable.
222      '';
223    };
224
225    package = mkOption {
226      type = types.package;
227      default = pkgs.picom;
228      defaultText = literalExpression "pkgs.picom";
229      example = literalExpression "pkgs.picom";
230      description = ''
231        Picom derivation to use.
232      '';
233    };
234
235    settings = with types;
236      let
237        scalar = oneOf [ bool int float str ] // {
238          description = "scalar types";
239        };
240
241        libConfig = oneOf [ scalar (listOf libConfig) (attrsOf libConfig) ] // {
242          description = "libconfig type";
243        };
244
245        topLevel = attrsOf libConfig // {
246          description = ''
247            libconfig configuration. The format consists of an attributes
248            set (called a group) of settings. Each setting can be a scalar type
249            (boolean, integer, floating point number or string), a list of
250            scalars or a group itself
251          '';
252        };
253
254      in mkOption {
255        type = topLevel;
256        default = { };
257        example = literalExpression ''
258          blur =
259            { method = "gaussian";
260              size = 10;
261              deviation = 5.0;
262            };
263        '';
264        description = ''
265          Picom settings. Use this option to configure Picom settings not exposed
266          in a NixOS option or to bypass one. For the available options see the
267          CONFIGURATION FILES section at `picom(1)`.
268        '';
269      };
270  };
271
272  config = mkIf cfg.enable {
273    assertions = [
274      (lib.hm.assertions.assertPlatform "services.picom" pkgs
275        lib.platforms.linux)
276    ];
277
278    services.picom.settings = mkDefaultAttrs {
279      # fading
280      fading = cfg.fade;
281      fade-delta = cfg.fadeDelta;
282      fade-in-step = elemAt cfg.fadeSteps 0;
283      fade-out-step = elemAt cfg.fadeSteps 1;
284      fade-exclude = cfg.fadeExclude;
285
286      # shadows
287      shadow = cfg.shadow;
288      shadow-offset-x = elemAt cfg.shadowOffsets 0;
289      shadow-offset-y = elemAt cfg.shadowOffsets 1;
290      shadow-opacity = cfg.shadowOpacity;
291      shadow-exclude = cfg.shadowExclude;
292
293      # opacity
294      active-opacity = cfg.activeOpacity;
295      inactive-opacity = cfg.inactiveOpacity;
296
297      wintypes = cfg.wintypes;
298
299      opacity-rule = cfg.opacityRules;
300
301      # other options
302      backend = cfg.backend;
303      vsync = cfg.vSync;
304    };
305
306    home.packages = [ cfg.package ];
307
308    xdg.configFile."picom/picom.conf".text = configFile;
309
310    systemd.user.services.picom = {
311      Unit = {
312        Description = "Picom X11 compositor";
313        After = [ "graphical-session-pre.target" ];
314        PartOf = [ "graphical-session.target" ];
315      };
316
317      Install = { WantedBy = [ "graphical-session.target" ]; };
318
319      Service = {
320        ExecStart = concatStringsSep " " ([
321          "${cfg.package}/bin/picom"
322          "--config ${config.xdg.configFile."picom/picom.conf".source}"
323        ] ++ cfg.extraArgs);
324        Restart = "always";
325        RestartSec = 3;
326      };
327    };
328  };
329
330  meta.maintainers = with lib.maintainers; [ thiagokokada ];
331}