fork/home-manager

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

Slabitykanshi: support adaptive sync (#4328)6e1eff9a

master
5.7 KiB231 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7  cfg = config.services.kanshi;
8
9  outputModule = types.submodule {
10    options = {
11
12      criteria = mkOption {
13        type = types.str;
14        description = ''
15          The criteria can either be an output name, an output description or "*".
16          The latter can be used to match any output.
17
18          On
19          {manpage}`sway(1)`,
20          output names and descriptions can be obtained via
21          `swaymsg -t get_outputs`.
22        '';
23      };
24
25      status = mkOption {
26        type = types.nullOr (types.enum [ "enable" "disable" ]);
27        default = null;
28        description = ''
29          Enables or disables the specified output.
30        '';
31      };
32
33      mode = mkOption {
34        type = types.nullOr types.str;
35        default = null;
36        example = "1920x1080@60Hz";
37        description = ''
38          <width>x<height>[@<rate>[Hz]]
39
40          Configures the specified output to use the specified mode.
41          Modes are a combination of width and height (in pixels) and
42          a refresh rate (in Hz) that your display can be configured to use.
43        '';
44      };
45
46      position = mkOption {
47        type = types.nullOr types.str;
48        default = null;
49        example = "1600,0";
50        description = ''
51          <x>,<y>
52
53          Places the output at the specified position in the global coordinates
54          space.
55        '';
56      };
57
58      scale = mkOption {
59        type = types.nullOr types.float;
60        default = null;
61        example = 2;
62        description = ''
63          Scales the output by the specified scale factor.
64        '';
65      };
66
67      transform = mkOption {
68        type = types.nullOr (types.enum [
69          "normal"
70          "90"
71          "180"
72          "270"
73          "flipped"
74          "flipped-90"
75          "flipped-180"
76          "flipped-270"
77        ]);
78        default = null;
79        description = ''
80          Sets the output transform.
81        '';
82      };
83
84      adaptiveSync = mkOption {
85        type = types.nullOr types.bool;
86        default = null;
87        example = true;
88        description = ''
89          Enables or disables adaptive synchronization
90          (aka. Variable Refresh Rate).
91        '';
92      };
93    };
94  };
95
96  outputStr =
97    { criteria, status, mode, position, scale, transform, adaptiveSync, ... }:
98    ''output "${criteria}"'' + optionalString (status != null) " ${status}"
99    + optionalString (mode != null) " mode ${mode}"
100    + optionalString (position != null) " position ${position}"
101    + optionalString (scale != null) " scale ${toString scale}"
102    + optionalString (transform != null) " transform ${transform}"
103    + optionalString (adaptiveSync != null)
104    " adaptive_sync ${if adaptiveSync then "on" else "off"}";
105
106  profileModule = types.submodule {
107    options = {
108      outputs = mkOption {
109        type = types.listOf outputModule;
110        default = [ ];
111        description = ''
112          Outputs configuration.
113        '';
114      };
115
116      exec = mkOption {
117        type = with types; coercedTo str singleton (listOf str);
118        default = [ ];
119        example =
120          "[ \${pkg.sway}/bin/swaymsg workspace 1, move workspace to eDP-1 ]";
121        description = ''
122          Commands executed after the profile is successfully applied.
123          Note that if you provide multiple commands, they will be
124          executed asynchronously with no guaranteed ordering.
125        '';
126      };
127    };
128  };
129
130  profileStr = name:
131    { outputs, exec, ... }: ''
132      profile ${name} {
133        ${
134          concatStringsSep "\n  "
135          (map outputStr outputs ++ map (cmd: "exec ${cmd}") exec)
136        }
137      }
138    '';
139in {
140
141  meta.maintainers = [ hm.maintainers.nurelin ];
142
143  options.services.kanshi = {
144    enable = mkEnableOption
145      "kanshi, a Wayland daemon that automatically configures outputs";
146
147    package = mkOption {
148      type = types.package;
149      default = pkgs.kanshi;
150      defaultText = literalExpression "pkgs.kanshi";
151      description = ''
152        kanshi derivation to use.
153      '';
154    };
155
156    profiles = mkOption {
157      type = types.attrsOf profileModule;
158      default = { };
159      description = ''
160        List of profiles.
161      '';
162      example = literalExpression ''
163        undocked = {
164          outputs = [
165            {
166              criteria = "eDP-1";
167            }
168          ];
169        };
170        docked = {
171          outputs = [
172            {
173              criteria = "eDP-1";
174            }
175            {
176              criteria = "Some Company ASDF 4242";
177              transform = "90";
178            }
179          ];
180        };
181      '';
182    };
183
184    extraConfig = mkOption {
185      type = types.lines;
186      default = "";
187      description = ''
188        Extra configuration lines to append to the kanshi
189        configuration file.
190      '';
191    };
192
193    systemdTarget = mkOption {
194      type = types.str;
195      default = "sway-session.target";
196      description = ''
197        Systemd target to bind to.
198      '';
199    };
200  };
201
202  config = mkIf cfg.enable {
203    assertions = [
204      (lib.hm.assertions.assertPlatform "services.kanshi" pkgs
205        lib.platforms.linux)
206    ];
207
208    xdg.configFile."kanshi/config".text = ''
209      ${concatStringsSep "\n" (mapAttrsToList profileStr cfg.profiles)}
210      ${cfg.extraConfig}
211    '';
212
213    systemd.user.services.kanshi = {
214      Unit = {
215        Description = "Dynamic output configuration";
216        Documentation = "man:kanshi(1)";
217        PartOf = cfg.systemdTarget;
218        Requires = cfg.systemdTarget;
219        After = cfg.systemdTarget;
220      };
221
222      Service = {
223        Type = "simple";
224        ExecStart = "${cfg.package}/bin/kanshi";
225        Restart = "always";
226      };
227
228      Install = { WantedBy = [ cfg.systemdTarget ]; };
229    };
230  };
231}