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