Rewrite acpid module in a more generic way

koral 93e17506 148396c0

+44 -130
+44 -130
nixos/modules/services/hardware/acpid.nix
··· 4 4 5 5 let 6 6 7 - acpiConfDir = pkgs.runCommand "acpi-events" {} 8 - '' 9 - mkdir -p $out 10 - ${ 11 - # Generate a configuration file for each event. (You can't have 12 - # multiple events in one config file...) 13 - let f = event: 14 - '' 15 - fn=$out/${event.name} 16 - echo "event=${event.event}" > $fn 17 - echo "action=${pkgs.writeScript "${event.name}.sh" event.action}" >> $fn 18 - ''; 19 - in lib.concatMapStrings f events 20 - } 21 - ''; 22 - 23 - events = [powerEvent lidEvent acEvent muteEvent volumeDownEvent volumeUpEvent cdPlayEvent cdNextEvent cdPrevEvent]; 24 - 25 - # Called when the power button is pressed. 26 - powerEvent = 27 - { name = "power-button"; 7 + canonicalHandlers = { 8 + powerEvent = { 28 9 event = "button/power.*"; 29 - action = 30 - '' 31 - #! ${pkgs.bash}/bin/sh 32 - ${config.services.acpid.powerEventCommands} 33 - ''; 10 + action = config.services.acpid.powerEventCommands; 34 11 }; 35 12 36 - # Called when the laptop lid is opened/closed. 37 - lidEvent = 38 - { name = "lid"; 13 + lidEvent = { 39 14 event = "button/lid.*"; 40 - action = 41 - '' 42 - #! ${pkgs.bash}/bin/sh 43 - ${config.services.acpid.lidEventCommands} 44 - ''; 15 + action = config.services.acpid.lidEventCommands; 45 16 }; 46 17 47 - # Called when the AC power is connected or disconnected. 48 - acEvent = 49 - { name = "ac-power"; 18 + acEvent = { 50 19 event = "ac_adapter.*"; 51 - action = 52 - '' 53 - #! ${pkgs.bash}/bin/sh 54 - ${config.services.acpid.acEventCommands} 55 - ''; 20 + action = config.services.acpid.acEventCommands; 56 21 }; 57 - 58 - muteEvent = { 59 - name = "mute"; 60 - event = "button/mute.*"; 61 - action = '' 62 - #! ${pkgs.bash}/bin/sh 63 - ${config.services.acpid.muteCommands} 64 - ''; 65 - }; 66 - 67 - volumeDownEvent = { 68 - name = "volume-down"; 69 - event = "button/volumedown.*"; 70 - action = '' 71 - #! ${pkgs.bash}/bin/sh 72 - ${config.services.acpid.volumeDownEventCommands} 73 - ''; 74 22 }; 75 23 76 - volumeUpEvent = { 77 - name = "volume-up"; 78 - event = "button/volumeup.*"; 79 - action = '' 80 - #! ${pkgs.bash}/bin/sh 81 - ${config.services.acpid.volumeUpEventCommands} 82 - ''; 83 - }; 84 - 85 - cdPlayEvent = { 86 - name = "cd-play"; 87 - event = "cd/play.*"; 88 - action = '' 89 - #! ${pkgs.bash}/bin/sh 90 - ${config.services.acpid.cdPlayEventCommands} 91 - ''; 92 - }; 93 - 94 - cdNextEvent = { 95 - name = "cd-next"; 96 - event = "cd/next.*"; 97 - action = '' 98 - #! ${pkgs.bash}/bin/sh 99 - ${config.services.acpid.cdNextEventCommands} 100 - ''; 101 - }; 102 - 103 - cdPrevEvent = { 104 - name = "cd-prev"; 105 - event = "cd/prev.*"; 106 - action = '' 107 - #! ${pkgs.bash}/bin/sh 108 - ${config.services.acpid.cdPrevEventCommands} 24 + acpiConfDir = pkgs.runCommand "acpi-events" {} 25 + '' 26 + mkdir -p $out 27 + ${ 28 + # Generate a configuration file for each event. (You can't have 29 + # multiple events in one config file...) 30 + let f = name: handler: 31 + '' 32 + fn=$out/${name} 33 + echo "event=${handler.event}" > $fn 34 + echo "action=${pkgs.writeScript "${name}.sh" (concatStringsSep "\n" [ "#! ${pkgs.bash}/bin/sh" handler.action ])}" >> $fn 35 + ''; 36 + in concatStringsSep "\n" (mapAttrsToList f (canonicalHandlers // config.services.acpid.handlers)) 37 + } 109 38 ''; 110 - }; 111 - 112 39 113 40 in 114 41 ··· 126 53 description = "Whether to enable the ACPI daemon."; 127 54 }; 128 55 56 + handlers = mkOption { 57 + type = types.attrsOf (types.submodule { 58 + options = { 59 + event = mkOption { 60 + type = types.str; 61 + example = [ "button/power.*" "button/lid.*" "ac_adapter.*" "button/mute.*" "button/volumedown.*" "cd/play.*" "cd/next.*" ]; 62 + description = "Event type."; 63 + }; 64 + 65 + action = mkOption { 66 + type = types.lines; 67 + description = "Shell commands to execute when the event is triggered."; 68 + }; 69 + }; 70 + }); 71 + 72 + description = "Event handlers."; 73 + default = {}; 74 + example = { mute = { event = "button/mute.*"; action = "amixer set Master toggle"; }; }; 75 + 76 + 77 + }; 78 + 129 79 powerEventCommands = mkOption { 130 80 type = types.lines; 131 81 default = ""; ··· 142 92 type = types.lines; 143 93 default = ""; 144 94 description = "Shell commands to execute on an ac_adapter.* event."; 145 - }; 146 - 147 - muteCommands = mkOption { 148 - type = types.lines; 149 - default = ""; 150 - description = "Shell commands to execute on an button/mute.* event."; 151 - }; 152 - 153 - volumeDownEventCommands = mkOption { 154 - type = types.lines; 155 - default = ""; 156 - description = "Shell commands to execute on an button/volumedown.* event."; 157 - }; 158 - 159 - volumeUpEventCommands = mkOption { 160 - type = types.lines; 161 - default = ""; 162 - description = "Shell commands to execute on an button/volumeup.* event."; 163 - }; 164 - 165 - cdPlayEventCommands = mkOption { 166 - type = types.lines; 167 - default = ""; 168 - description = "Shell commands to execute on an cd/play.* event."; 169 - }; 170 - 171 - cdNextEventCommands = mkOption { 172 - type = types.lines; 173 - default = ""; 174 - description = "Shell commands to execute on an cd/next.* event."; 175 - }; 176 - 177 - cdPrevEventCommands = mkOption { 178 - type = types.lines; 179 - default = ""; 180 - description = "Shell commands to execute on an cd/prev.* event."; 181 95 }; 182 96 183 97 };