fork/home-manager

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

Robert HelgessonMerge PR #421575790441

master
10.5 KiB366 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7  cfg = config.programs.autorandr;
8
9  matrixOf = n: m: elemType:
10    mkOptionType rec {
11      name = "matrixOf";
12      description =
13        "${toString n}×${toString m} matrix of ${elemType.description}s";
14      check = xss:
15        let listOfSize = l: xs: isList xs && length xs == l;
16        in listOfSize n xss
17        && all (xs: listOfSize m xs && all elemType.check xs) xss;
18      merge = mergeOneOption;
19      getSubOptions = prefix: elemType.getSubOptions (prefix ++ [ "*" "*" ]);
20      getSubModules = elemType.getSubModules;
21      substSubModules = mod: matrixOf n m (elemType.substSubModules mod);
22      functor = (defaultFunctor name) // { wrapped = elemType; };
23    };
24
25  profileModule = types.submodule {
26    options = {
27      fingerprint = mkOption {
28        type = types.attrsOf types.str;
29        description = ''
30          Output name to EDID mapping.
31          Use `autorandr --fingerprint` to get current setup values.
32        '';
33        default = { };
34      };
35
36      config = mkOption {
37        type = types.attrsOf configModule;
38        description = "Per output profile configuration.";
39        default = { };
40      };
41
42      hooks = mkOption {
43        type = profileHooksModule;
44        description = "Profile hook scripts.";
45        default = { };
46      };
47    };
48  };
49
50  configModule = types.submodule {
51    options = {
52      enable = mkOption {
53        type = types.bool;
54        description = "Whether to enable the output.";
55        default = true;
56      };
57
58      crtc = mkOption {
59        type = types.nullOr types.ints.unsigned;
60        description = "Output video display controller.";
61        default = null;
62        example = 0;
63      };
64
65      primary = mkOption {
66        type = types.bool;
67        description = "Whether output should be marked as primary";
68        default = false;
69      };
70
71      position = mkOption {
72        type = types.str;
73        description = "Output position";
74        default = "";
75        example = "5760x0";
76      };
77
78      mode = mkOption {
79        type = types.str;
80        description = "Output resolution.";
81        default = "";
82        example = "3840x2160";
83      };
84
85      rate = mkOption {
86        type = types.str;
87        description = "Output framerate.";
88        default = "";
89        example = "60.00";
90      };
91
92      gamma = mkOption {
93        type = types.str;
94        description = "Output gamma configuration.";
95        default = "";
96        example = "1.0:0.909:0.833";
97      };
98
99      rotate = mkOption {
100        type = types.nullOr (types.enum [ "normal" "left" "right" "inverted" ]);
101        description = "Output rotate configuration.";
102        default = null;
103        example = "left";
104      };
105
106      transform = mkOption {
107        type = types.nullOr (matrixOf 3 3 types.float);
108        default = null;
109        example = literalExpression ''
110          [
111            [ 0.6 0.0 0.0 ]
112            [ 0.0 0.6 0.0 ]
113            [ 0.0 0.0 1.0 ]
114          ]
115        '';
116        description = ''
117          Refer to
118          {manpage}`xrandr(1)`
119          for the documentation of the transform matrix.
120        '';
121      };
122
123      dpi = mkOption {
124        type = types.nullOr types.ints.positive;
125        description = "Output DPI configuration.";
126        default = null;
127        example = 96;
128      };
129
130      scale = mkOption {
131        type = types.nullOr (types.submodule {
132          options = {
133            method = mkOption {
134              type = types.enum [ "factor" "pixel" ];
135              description = "Output scaling method.";
136              default = "factor";
137              example = "pixel";
138            };
139
140            x = mkOption {
141              type = types.either types.float types.ints.positive;
142              description = "Horizontal scaling factor/pixels.";
143            };
144
145            y = mkOption {
146              type = types.either types.float types.ints.positive;
147              description = "Vertical scaling factor/pixels.";
148            };
149          };
150        });
151        description = ''
152          Output scale configuration.
153
154          Either configure by pixels or a scaling factor. When using pixel method the
155          {manpage}`xrandr(1)`
156          option
157          `--scale-from`
158          will be used; when using factor method the option
159          `--scale`
160          will be used.
161
162          This option is a shortcut version of the transform option and they are mutually
163          exclusive.
164        '';
165        default = null;
166        example = literalExpression ''
167          {
168            x = 1.25;
169            y = 1.25;
170          }
171        '';
172      };
173
174      filter = mkOption {
175        type = types.nullOr (types.enum [ "bilinear" "nearest" ]);
176        description = "Interpolation method to be used for scaling the output.";
177        default = null;
178        example = "nearest";
179      };
180    };
181  };
182
183  hookType = types.lines;
184
185  globalHooksModule = types.submodule {
186    options = {
187      postswitch = mkOption {
188        type = types.attrsOf hookType;
189        description = "Postswitch hook executed after mode switch.";
190        default = { };
191      };
192
193      preswitch = mkOption {
194        type = types.attrsOf hookType;
195        description = "Preswitch hook executed before mode switch.";
196        default = { };
197      };
198
199      predetect = mkOption {
200        type = types.attrsOf hookType;
201        description = ''
202          Predetect hook executed before autorandr attempts to run xrandr.
203        '';
204        default = { };
205      };
206    };
207  };
208
209  profileHooksModule = types.submodule {
210    options = {
211      postswitch = mkOption {
212        type = hookType;
213        description = "Postswitch hook executed after mode switch.";
214        default = "";
215      };
216
217      preswitch = mkOption {
218        type = hookType;
219        description = "Preswitch hook executed before mode switch.";
220        default = "";
221      };
222
223      predetect = mkOption {
224        type = hookType;
225        description = ''
226          Predetect hook executed before autorandr attempts to run xrandr.
227        '';
228        default = "";
229      };
230    };
231  };
232
233  hookToFile = folder: name: hook:
234    nameValuePair "autorandr/${folder}/${name}" {
235      source = "${pkgs.writeShellScriptBin "hook" hook}/bin/hook";
236    };
237  profileToFiles = name: profile:
238    with profile;
239    mkMerge ([
240      {
241        "autorandr/${name}/setup".text = concatStringsSep "\n"
242          (mapAttrsToList fingerprintToString fingerprint);
243        "autorandr/${name}/config".text =
244          concatStringsSep "\n" (mapAttrsToList configToString profile.config);
245      }
246      (mkIf (hooks.postswitch != "")
247        (listToAttrs [ (hookToFile name "postswitch" hooks.postswitch) ]))
248      (mkIf (hooks.preswitch != "")
249        (listToAttrs [ (hookToFile name "preswitch" hooks.preswitch) ]))
250      (mkIf (hooks.predetect != "")
251        (listToAttrs [ (hookToFile name "predetect" hooks.predetect) ]))
252    ]);
253  fingerprintToString = name: edid: "${name} ${edid}";
254  configToString = name: config:
255    if config.enable then
256      concatStringsSep "\n" ([ "output ${name}" ]
257        ++ optional (config.position != "") "pos ${config.position}"
258        ++ optional (config.crtc != null) "crtc ${toString config.crtc}"
259        ++ optional config.primary "primary"
260        ++ optional (config.dpi != null) "dpi ${toString config.dpi}"
261        ++ optional (config.gamma != "") "gamma ${config.gamma}"
262        ++ optional (config.mode != "") "mode ${config.mode}"
263        ++ optional (config.rate != "") "rate ${config.rate}"
264        ++ optional (config.rotate != null) "rotate ${config.rotate}"
265        ++ optional (config.filter != null) "filter ${config.filter}"
266        ++ optional (config.transform != null) ("transform "
267          + concatMapStringsSep "," toString (flatten config.transform))
268        ++ optional (config.scale != null)
269        ((if config.scale.method == "factor" then "scale" else "scale-from")
270          + " ${toString config.scale.x}x${toString config.scale.y}"))
271    else ''
272      output ${name}
273      off
274    '';
275
276in {
277  options = {
278    programs.autorandr = {
279      enable = mkEnableOption "Autorandr";
280
281      hooks = mkOption {
282        type = globalHooksModule;
283        description = "Global hook scripts";
284        default = { };
285        example = literalExpression ''
286          {
287            postswitch = {
288              "notify-i3" = "''${pkgs.i3}/bin/i3-msg restart";
289              "change-background" = readFile ./change-background.sh;
290              "change-dpi" = '''
291                case "$AUTORANDR_CURRENT_PROFILE" in
292                  default)
293                    DPI=120
294                    ;;
295                  home)
296                    DPI=192
297                    ;;
298                  work)
299                    DPI=144
300                    ;;
301                  *)
302                    echo "Unknown profile: $AUTORANDR_CURRENT_PROFILE"
303                    exit 1
304                esac
305
306                echo "Xft.dpi: $DPI" | ''${pkgs.xorg.xrdb}/bin/xrdb -merge
307              '''
308            };
309          }
310        '';
311      };
312
313      profiles = mkOption {
314        type = types.attrsOf profileModule;
315        description = "Autorandr profiles specification.";
316        default = { };
317        example = literalExpression ''
318          {
319            "work" = {
320              fingerprint = {
321                eDP1 = "<EDID>";
322                DP1 = "<EDID>";
323              };
324              config = {
325                eDP1.enable = false;
326                DP1 = {
327                  enable = true;
328                  crtc = 0;
329                  primary = true;
330                  position = "0x0";
331                  mode = "3840x2160";
332                  gamma = "1.0:0.909:0.833";
333                  rate = "60.00";
334                  rotate = "left";
335                };
336              };
337              hooks.postswitch = readFile ./work-postswitch.sh;
338            };
339          }
340        '';
341      };
342    };
343  };
344
345  config = mkIf cfg.enable {
346    assertions = flatten (mapAttrsToList (profile:
347      { config, ... }:
348      mapAttrsToList (output: opts: {
349        assertion = opts.scale == null || opts.transform == null;
350        message = ''
351          Cannot use the profile output options 'scale' and 'transform' simultaneously.
352          Check configuration for: programs.autorandr.profiles.${profile}.config.${output}
353        '';
354      }) config) cfg.profiles);
355
356    home.packages = [ pkgs.autorandr ];
357    xdg.configFile = mkMerge ([
358      (mapAttrs' (hookToFile "postswitch.d") cfg.hooks.postswitch)
359      (mapAttrs' (hookToFile "preswitch.d") cfg.hooks.preswitch)
360      (mapAttrs' (hookToFile "predetect.d") cfg.hooks.predetect)
361      (mkMerge (mapAttrsToList profileToFiles cfg.profiles))
362    ]);
363  };
364
365  meta.maintainers = [ maintainers.uvnikita ];
366}