{ lib, config, pkgs, ... }: {
  config = lib.mkIf (config.syscfg.make.power) {
    services.tlp = {
      enable = true;
      settings = {
        START_CHARGE_THRESH_BAT0 = 75;
        STOP_CHARGE_THRESH_BAT0 = 90;
        CPU_SCALING_GOVERNOR_ON_AC = "performance";
        CPU_SCALING_GOVERNOR_ON_BAT = "powersave";
        MEM_SLEEP_ON_BAT = "deep";
      };
    };
    
    powerManagement.enable = true;
    # suspend to RAM (deep) rather than `s2idle`
    boot.kernelParams = [ "mem_sleep_default=deep" ];
    # suspend-then-hibernate
    systemd.sleep.settings.Sleep = {
      HibernateDelaySec = "30m";
      SuspendState = "mem";
    };

    services.logind.settings.Login.HandleLidSwitch = "suspend-then-hibernate";
    # Hibernate on power button pressed
    services.logind.settings.Login.HandlePowerKey = "hibernate";
    services.logind.settings.Login.HandlePowerKeyLongPress = "poweroff";

    systemd.user.services.battery_monitor = {
      wants = [ "display-manager.service" ];
      wantedBy = [ "graphical-session.target" ];
      script = ''
        prev_val=100
        check () { [[ $1 -ge $val ]] && [[ $1 -lt $prev_val ]]; }
        notify () {
        ${pkgs.libnotify}/bin/notify-send -a Battery "$@" \
            -h "int:value:$val" "Discharging" "$val%, $remaining"
        }
        while true; do
        IFS=: read _ bat0 < <(${pkgs.acpi}/bin/acpi -b)
        IFS=\ , read status val remaining <<<"$bat0"
        val=''${val%\%}
        if [[ $status = Discharging ]]; then
            echo "$val%, $remaining"
            if check 30 || check 25 || check 20; then notify
            elif check 15 || [[ $val -le 10 ]]; then notify -u critical
            fi
        fi
        prev_val=$val
        # Sleep longer when battery is high to save CPU
        if [[ $val -gt 30 ]]; then sleep 10m; elif [[ $val -ge 20 ]]; then sleep 5m; else sleep 1m; fi
        done
      '';
    };
  };
}
