fork/home-manager

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

Robert HelgessonMerge PR #421575790441

master
6.1 KiB234 linesraw
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6
7  cfg = config.services.gromit-mpx;
8
9  # Select the appropriate hot key:
10  hotkey = if isInt cfg.hotKey then
11    "--keycode ${toString cfg.hotKey}"
12  else if cfg.hotKey != null then
13    "--key ${cfg.hotKey}"
14  else
15    "--key none";
16
17  # Select the appropriate undo key:
18  undokey = if isInt cfg.undoKey then
19    "--undo-keycode ${toString cfg.undoKey}"
20  else if cfg.undoKey != null then
21    "--undo-key ${cfg.undoKey}"
22  else
23    "--undo-key none";
24
25  # The command line to send to gromit-mpx:
26  commandArgs = concatStringsSep " " [ hotkey undokey ];
27
28  # Gromit reads and writes from this file to store it's run time
29  # state.  That will break our config so we set it manually which,
30  # thanks to the read-only Nix store, prevents Gromit from writing to
31  # it.
32  keyFile = generators.toINI { } {
33    General.ShowIntroOnStartup = false;
34    Drawing.Opacity = cfg.opacity;
35  };
36
37  # Allowed modifiers:
38  modsAndButtons = [ "1" "2" "3" "4" "5" "SHIFT" "CONTROL" "ALT" "META" ];
39
40  # Create a string of tool attributes:
41  toolAttrs = tool:
42    concatStringsSep " " ([ "size=${toString tool.size}" ]
43      ++ optional (tool.type != "eraser") ''color="${tool.color}"''
44      ++ optional (tool.arrowSize != null)
45      "arrowsize=${toString tool.arrowSize}");
46
47  # Optional tool modifier string:
48  toolMod = tool:
49    if tool.modifiers != [ ] then
50      "[" + concatStringsSep ", " tool.modifiers + "]"
51    else
52      "";
53
54  # A single tool configuration:
55  toolToCfg = n: tool: ''
56    "tool-${toString n}" = ${toUpper tool.type} (${toolAttrs tool});
57    "${tool.device}"${toolMod tool} = "tool-${toString n}";
58  '';
59
60  # Per-tool options:
61  toolOptions = {
62    options = {
63      device = mkOption {
64        type = types.str;
65        example = "default";
66        description = ''
67          Use this tool with the given xinput device.  The device with
68          the name default works with any input.
69        '';
70      };
71
72      type = mkOption {
73        type = types.enum [ "pen" "eraser" "recolor" ];
74        default = "pen";
75        example = "eraser";
76        description = "Which type of tool this is.";
77      };
78
79      color = mkOption {
80        type = types.str;
81        default = "red";
82        example = "#ff00ff";
83        description = "The stroke (or recolor) color of the tool.";
84      };
85
86      size = mkOption {
87        type = types.ints.positive;
88        default = 5;
89        example = 3;
90        description = "The tool size.";
91      };
92
93      arrowSize = mkOption {
94        type = types.nullOr types.ints.positive;
95        default = null;
96        example = 2;
97        description = ''
98          If not `null`, automatically draw an arrow
99          at the end of a stroke with the given size.
100        '';
101      };
102
103      modifiers = mkOption {
104        type = types.listOf (types.enum modsAndButtons);
105        default = [ ];
106        example = [ "SHIFT" ];
107        description = ''
108          Only activate this tool if the given modifiers are also active.
109        '';
110      };
111    };
112  };
113
114in {
115  meta.maintainers = [ maintainers.pjones ];
116
117  options.services.gromit-mpx = {
118    enable = mkEnableOption "Gromit-MPX annotation tool";
119
120    package = mkOption {
121      type = types.package;
122      default = pkgs.gromit-mpx;
123      defaultText = "pkgs.gromit-mpx";
124      description = "The gromit-mpx package to use.";
125    };
126
127    hotKey = mkOption {
128      type = with types; nullOr (either str ints.positive);
129      default = "F9";
130      example = "Insert";
131      description = ''
132        A keysym or raw keycode that toggles the activation state of
133        gromit-mpx.  Set to `null` to disable the
134        hotkey in which case you'll have to activate gromit-mpx
135        manually using the command line.
136      '';
137    };
138
139    undoKey = mkOption {
140      type = with types; nullOr (either str ints.positive);
141      default = "F10";
142      description = ''
143        A keysym or raw keycode that causes gromit-mpx to undo the
144        last stroke.  Use this key along with the shift key to redo an
145        undone stoke.  Set to `null` to disable the
146        undo hotkey.
147      '';
148    };
149
150    opacity = mkOption {
151      type = types.addCheck types.float (f: f >= 0.0 && f <= 1.0) // {
152        description = "float between 0.0 and 1.0 (inclusive)";
153      };
154      default = 0.75;
155      example = 1.0;
156      description = "Opacity of the drawing overlay.";
157    };
158
159    tools = mkOption {
160      type = types.listOf (types.submodule toolOptions);
161      default = [
162        {
163          device = "default";
164          type = "pen";
165          size = 5;
166          color = "red";
167        }
168        {
169          device = "default";
170          type = "pen";
171          size = 5;
172          color = "blue";
173          modifiers = [ "SHIFT" ];
174        }
175        {
176          device = "default";
177          type = "pen";
178          size = 5;
179          color = "yellow";
180          modifiers = [ "CONTROL" ];
181        }
182        {
183          device = "default";
184          type = "pen";
185          size = 6;
186          color = "green";
187          arrowSize = 1;
188          modifiers = [ "2" ];
189        }
190        {
191          device = "default";
192          type = "eraser";
193          size = 75;
194          modifiers = [ "3" ];
195        }
196      ];
197      description = ''
198        Tool definitions for gromit-mpx to use.
199      '';
200    };
201  };
202
203  config = mkIf cfg.enable {
204    assertions = [
205      (lib.hm.assertions.assertPlatform "services.gromit-mpx" pkgs
206        lib.platforms.linux)
207    ];
208
209    xdg.configFile."gromit-mpx.ini".text = keyFile;
210    xdg.configFile."gromit-mpx.cfg".text =
211      concatStringsSep "\n" (imap1 toolToCfg cfg.tools);
212
213    home.packages = [ cfg.package ];
214
215    systemd.user.services.gromit-mpx = {
216      Unit = {
217        Description = "Gromit-MPX";
218        After = [ "graphical-session-pre.target" ];
219        PartOf = [ "graphical-session.target" ];
220        X-Restart-Triggers = [
221          "${config.xdg.configFile."gromit-mpx.cfg".source}"
222          "${config.xdg.configFile."gromit-mpx.ini".source}"
223        ];
224      };
225
226      Service = {
227        Type = "simple";
228        ExecStart = "${cfg.package}/bin/gromit-mpx ${commandArgs}";
229      };
230
231      Install = { WantedBy = [ "graphical-session.target" ]; };
232    };
233  };
234}