summary refs log blame commit diff
path: root/modules/services/hardware/acpid.nix
blob: e9fd8e46dc926208ab8aaf8cad9a9e64c00d145e (plain) (tree)
1
2
3
4
5
6
7
8
9
                      
 
              
 




                                                










                                                                                    
       
  

                                         
                                            







                                
  
                                                





                                

           
  
                                                          





                                

           
 

  
 
 
                  
 

             
                      
 

                         
                                                           








                       
                                              
 
                
                                    
 
                                 



                                                                                
    
  
 
{ config, pkgs, ... }:

with pkgs.lib;

let

  acpiConfDir = pkgs.runCommand "acpi-events" {}
    ''
      ensureDir $out
      ${
        # Generate a .conf file for each event. (You can't have
        # multiple events in one config file...)
        let f = event:
          ''
            fn=$out/${event.name}.conf
            echo "event=${event.event}" > $fn
            echo "action=${pkgs.writeScript "${event.name}.sh" event.action}" >> $fn
          '';
        in pkgs.lib.concatMapStrings f events
      }
    '';
  
  events = [powerEvent lidEvent acEvent];
  
  # Called when the power button is pressed.
  powerEvent =
    { name = "power-button";
      event = "button/power.*";
      action = 
        ''
          #! ${pkgs.bash}/bin/sh
        '';
    };
  
  # Called when the laptop lid is opened/closed.
  lidEvent = 
    { name = "lid";
      event = "button/lid.*";
      action =
        ''
          #! ${pkgs.bash}/bin/sh
        '';
    };
  
  # Called when the AC power is connected or disconnected.
  acEvent =
    { name = "ac-power";
      event = "ac_adapter.*";
      action = 
        ''
          #! ${pkgs.bash}/bin/sh
        '';
    };

in

{

  ###### interface

  options = {
  
    services.acpid = {

      enable = mkOption {
        default = false;
        description = "Whether to enable the ACPI daemon.";
      };
      
    };
    
  };
  

  ###### implementation

  config = mkIf config.services.acpid.enable {

    jobs.acpid =
      { description = "ACPI daemon";

        startOn = "started udev";

        exec = "${pkgs.acpid}/sbin/acpid --foreground --confdir ${acpiConfDir}";
      };
      
  };
  
}