aboutsummaryrefslogtreecommitdiff
path: root/home-manager/modules/services/kanshi.nix
blob: 4e5e5f104e65b2f9e2e20d669401f3a75da207f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
{ config, lib, pkgs, ... }:

with lib;

let

  cfg = config.services.kanshi;

  outputModule = types.submodule {
    options = {

      criteria = mkOption {
        type = types.str;
        description = ''
          The criteria can either be an output name, an output description or "*".
          The latter can be used to match any output.

          On
          <citerefentry>
            <refentrytitle>sway</refentrytitle>
            <manvolnum>1</manvolnum>
          </citerefentry>,
          output names and descriptions can be obtained via
          <literal>swaymsg -t get_outputs</literal>.
        '';
      };

      status = mkOption {
        type = types.nullOr (types.enum [ "enable" "disable" ]);
        default = null;
        description = ''
          Enables or disables the specified output.
        '';
      };

      mode = mkOption {
        type = types.nullOr types.str;
        default = null;
        example = "1920x1080@60Hz";
        description = ''
          &lt;width&gt;x&lt;height&gt;[@&lt;rate&gt;[Hz]]
          </para><para>
          Configures the specified output to use the specified mode.
          Modes are a combination of width and height (in pixels) and
          a refresh rate (in Hz) that your display can be configured to use.
        '';
      };

      position = mkOption {
        type = types.nullOr types.str;
        default = null;
        example = "1600,0";
        description = ''
          &lt;x&gt;,&lt;y&gt;
          </para><para>
          Places the output at the specified position in the global coordinates
          space.
        '';
      };

      scale = mkOption {
        type = types.nullOr types.float;
        default = null;
        example = 2;
        description = ''
          Scales the output by the specified scale factor.
        '';
      };

      transform = mkOption {
        type = types.nullOr (types.enum [
          "normal"
          "90"
          "180"
          "270"
          "flipped"
          "flipped-90"
          "flipped-180"
          "flipped-270"
        ]);
        default = null;
        description = ''
          Sets the output transform.
        '';
      };
    };
  };

  outputStr = { criteria, status, mode, position, scale, transform, ... }:
    ''output "${criteria}"'' + optionalString (status != null) " ${status}"
    + optionalString (mode != null) " mode ${mode}"
    + optionalString (position != null) " position ${position}"
    + optionalString (scale != null) " scale ${toString scale}"
    + optionalString (transform != null) " transform ${transform}";

  profileModule = types.submodule {
    options = {
      outputs = mkOption {
        type = types.listOf outputModule;
        default = [ ];
        description = ''
          Outputs configuration.
        '';
      };

      exec = mkOption {
        type = types.nullOr types.str;
        default = null;
        example =
          "\${pkg.sway}/bin/swaymsg workspace 1, move workspace to eDP-1";
        description = ''
          Command executed after the profile is succesfully applied.
        '';
      };
    };
  };

  profileStr = name:
    { outputs, exec, ... }:
    ''
      profile ${name} {
        ${concatStringsSep "\n  " (map outputStr outputs)}
    '' + optionalString (exec != null) "  exec ${exec}\n" + ''
      }
    '';
in {

  meta.maintainers = [ maintainers.nurelin ];

  options.services.kanshi = {
    enable = mkEnableOption
      "kanshi, a Wayland daemon that automatically configures outputs";

    package = mkOption {
      type = types.package;
      default = pkgs.kanshi;
      defaultText = literalExample "pkgs.kanshi";
      description = ''
        kanshi derivation to use.
      '';
    };

    profiles = mkOption {
      type = types.attrsOf profileModule;
      default = { };
      description = ''
        List of profiles.
      '';
    };

    extraConfig = mkOption {
      type = types.lines;
      default = "";
      description = ''
        Extra configuration lines to append to the kanshi
        configuration file.
      '';
    };

    systemdTarget = mkOption {
      type = types.str;
      default = "sway-session.target";
      description = ''
        Systemd target to bind to.
      '';
    };
  };

  config = mkIf cfg.enable {

    xdg.configFile."kanshi/config".text = ''
      ${concatStringsSep "\n" (mapAttrsToList profileStr cfg.profiles)}
      ${cfg.extraConfig}
    '';

    systemd.user.services.kanshi = {
      Unit = {
        Description = "Dynamic output configuration";
        Documentation = "man:kanshi(1)";
        PartOf = cfg.systemdTarget;
        Requires = cfg.systemdTarget;
        After = cfg.systemdTarget;
      };

      Service = {
        Type = "simple";
        ExecStart = "${cfg.package}/bin/kanshi";
        Restart = "always";
      };

      Install = { WantedBy = [ cfg.systemdTarget ]; };
    };
  };
}