<commit_before>{ config, pkgs, ... }:

with pkgs.lib;

let

  cfg = config.powerManagement;

  sleepHook = pkgs.writeScript "sleep-hook.sh"
    ''
      #! ${pkgs.stdenv.shell}
      action="$1"
      case "$action" in
          hibernate|suspend)
              ${cfg.powerDownCommands}
              ;;
          thaw|resume)
              ${cfg.resumeCommands}
              ${cfg.powerUpCommands}
              ;;
      esac
    '';

in

{

  ###### interface

  options = {

    powerManagement = {

      enable = mkOption {
        default = false;
        description =
          ''
            Whether to enable power management.  This includes support
            for suspend-to-RAM and powersave features on laptops.
          '';
      };

      resumeCommands = mkOption {
        default = "";
        description = "Commands executed after the system resumes from suspend-to-RAM.";
      };

      powerUpCommands = mkOption {
        default = "";
        example = "${pkgs.hdparm}/sbin/hdparm -B 255 /dev/sda";
        description =
          ''
            Commands executed when the machine powers up.  That is,
            they're executed both when the system first boots and when
            it resumes from suspend or hibernation.
          '';
      };

      powerDownCommands = mkOption {
        default = "";
        example = "${pkgs.hdparm}/sbin/hdparm -B 255 /dev/sda";
        description =
          ''
            Commands executed when the machine powers down.  That is,
            they're executed both when the system shuts down and when
            it goes to suspend or hibernation.
          '';
      };

    };

  };


  ###### implementation

  config = mkIf cfg.enable {

    # Enable the ACPI daemon.  Not sure whether this is essential.
    services.acpid.enable = true;

    environment.systemPackages = [ pkgs.pmutils ];

    environment.etc = singleton
      { source = sleepHook;
        target = "pm/sleep.d/00sleep-hook";
      };

    boot.kernelModules =
      [ "acpi_cpufreq" "cpufreq_performance" "cpufreq_powersave" "cpufreq_ondemand"
        "cpufreq_conservative"
      ];

    powerManagement.cpuFreqGovernor = mkDefault "ondemand";
    powerManagement.scsiLinkPolicy = mkDefault "min_power";
    
  };

}<commit_msg>systemd: Run the powerManagement.* hooks on suspend/resume<commit_after>{ config, pkgs, ... }:

with pkgs.lib;

let

  cfg = config.powerManagement;

in

{

  ###### interface

  options = {

    powerManagement = {

      enable = mkOption {
        default = false;
        description =
          ''
            Whether to enable power management.  This includes support
            for suspend-to-RAM and powersave features on laptops.
          '';
      };

      resumeCommands = mkOption {
        default = "";
        description = "Commands executed after the system resumes from suspend-to-RAM.";
      };

      powerUpCommands = mkOption {
        default = "";
        example = "${pkgs.hdparm}/sbin/hdparm -B 255 /dev/sda";
        description =
          ''
            Commands executed when the machine powers up.  That is,
            they're executed both when the system first boots and when
            it resumes from suspend or hibernation.
          '';
      };

      powerDownCommands = mkOption {
        default = "";
        example = "${pkgs.hdparm}/sbin/hdparm -B 255 /dev/sda";
        description =
          ''
            Commands executed when the machine powers down.  That is,
            they're executed both when the system shuts down and when
            it goes to suspend or hibernation.
          '';
      };

    };

  };


  ###### implementation

  config = mkIf cfg.enable {

    # Enable the ACPI daemon.  Not sure whether this is essential.
    services.acpid.enable = true;

    boot.kernelModules =
      [ "acpi_cpufreq" "cpufreq_performance" "cpufreq_powersave" "cpufreq_ondemand"
        "cpufreq_conservative"
      ];

    powerManagement.cpuFreqGovernor = mkDefault "ondemand";
    powerManagement.scsiLinkPolicy = mkDefault "min_power";

    # Service executed before suspending/hibernating.
    boot.systemd.services."pre-sleep" =
      { description = "Pre-Sleep Actions";
        wantedBy = [ "sleep.target" ];
        before = [ "sleep.target" ];
        script =
          ''
            ${cfg.powerDownCommands}
          '';
        serviceConfig.Type = "oneshot";
      };

    # Service executed before suspending/hibernating.  There doesn't
    # seem to be a good way to hook in a service to be executed after
    # both suspend *and* hibernate, so have a separate one for each.
    boot.systemd.services."post-suspend" =
      { description = "Post-Suspend Actions";
        wantedBy = [ "suspend.target" ];
        after = [ "systemd-suspend.service" ];
        script =
          ''
            ${cfg.resumeCommands}
            ${cfg.powerUpCommands}
          '';
        serviceConfig.Type = "oneshot";
      };

    boot.systemd.services."post-hibernate" =
      { description = "Post-Hibernate Actions";
        wantedBy = [ "hibernate.target" ];
        after = [ "systemd-hibernate.service" ];
        script =
          ''
            ${cfg.resumeCommands}
            ${cfg.powerUpCommands}
          '';
        serviceConfig.Type = "oneshot";
      };

  };

}
{'fraction_non_alphanumeric': 0.1053125586634128, 'fraction_numerical': 0.002815843814529754, 'mean_word_length': 1.8629768941429339, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6120258', 'n_tokens_mistral': 1552, 'n_tokens_neox': 1495, 'n_words': 382}