aboutsummaryrefslogtreecommitdiff
path: root/modules/services/grobi.nix
blob: 4dfc5d6331f1b5d983e660f56ec6902d176a8cc8 (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
{ config, lib, pkgs, ... }:

with lib;

let

  cfg = config.services.grobi;

  eitherStrBoolIntList = with types;
    either str (either bool (either int (listOf str)));

in {
  meta.maintainers = [ maintainers.mbrgm ];

  options = {
    services.grobi = {
      enable = mkEnableOption "the grobi display setup daemon";

      executeAfter = mkOption {
        type = with types; listOf str;
        default = [ ];
        example = [ "setxkbmap dvorak" ];
        description = ''
          Commands to be run after an output configuration was
          changed. The Nix value declared here will be translated to
          JSON and written to the <option>execute_after</option> key
          in <filename>~/.config/grobi.conf</filename>.
        '';
      };

      rules = mkOption {
        type = with types; listOf (attrsOf eitherStrBoolIntList);
        default = [ ];
        example = literalExample ''
          [
            {
              name = "Home";
              outputs_connected = [ "DP-2" ];
              configure_single = "DP-2";
              primary = true;
              atomic = true;
              execute_after = [
                "${pkgs.xorg.xrandr}/bin/xrandr --dpi 96"
                "${pkgs.xmonad-with-packages}/bin/xmonad --restart";
              ];
            }
            {
              name = "Mobile";
              outputs_disconnected = [ "DP-2" ];
              configure_single = "eDP-1";
              primary = true;
              atomic = true;
              execute_after = [
                "${pkgs.xorg.xrandr}/bin/xrandr --dpi 120"
                "${pkgs.xmonad-with-packages}/bin/xmonad --restart";
              ];
            }
          ]
        '';
        description = ''
          These are the rules grobi tries to match to the current
          output configuration. The rules are evaluated top to bottom,
          the first matching rule is applied and processing stops. See
          <link xlink:href="https://github.com/fd0/grobi/blob/master/doc/grobi.conf"/>
          for more information. The Nix value declared here will be
          translated to JSON and written to the <option>rules</option>
          key in <filename>~/.config/grobi.conf</filename>.
        '';
      };
    };
  };

  config = mkIf cfg.enable {
    systemd.user.services.grobi = {
      Unit = {
        Description = "grobi display auto config daemon";
        After = [ "graphical-session-pre.target" ];
        PartOf = [ "graphical-session.target" ];
      };

      Service = {
        Type = "simple";
        ExecStart = "${pkgs.grobi}/bin/grobi watch -v";
        Restart = "always";
        RestartSec = "2s";
        Environment = "PATH=${pkgs.xorg.xrandr}/bin:${pkgs.bash}/bin";
      };

      Install = { WantedBy = [ "graphical-session.target" ]; };
    };

    xdg.configFile."grobi.conf".text = builtins.toJSON {
      execute_after = cfg.executeAfter;
      rules = cfg.rules;
    };
  };
}