nix-config-v2/modules/pwrMgmt/default.nix

70 lines
1.9 KiB
Nix
Raw Normal View History

{
config,
lib,
2024-12-23 14:35:36 -06:00
pkgs,
...
}: let
2024-12-23 14:35:36 -06:00
cfg = config.pwrMgmt;
in {
2024-12-23 14:35:36 -06:00
options.pwrMgmt = {
enable = lib.mkEnableOption "Custom power management configuration";
cpuFreqGovernor = lib.mkOption {
type = lib.types.enum [
"performance"
"powersave"
"ondemand"
"conservative"
];
default = "performance";
description = "CPU frequency scaling governor to use";
};
powertop = {
enable = lib.mkEnableOption "PowerTop power management tool";
autotuneOnBoot = lib.mkOption {
type = lib.types.bool;
default = false;
description = "Automatically tune power settings on boot";
};
};
battery = {
enable = lib.mkEnableOption "Battery-specific power management";
startChargeThreshold = lib.mkOption {
type = lib.types.int;
default = 20;
description = "Percentage at which to start charging";
};
stopChargeThreshold = lib.mkOption {
type = lib.types.int;
default = 80;
description = "Percentage at which to stop charging";
};
};
};
config = lib.mkIf cfg.enable {
powerManagement = {
enable = true;
cpuFreqGovernor = cfg.cpuFreqGovernor;
};
2024-12-23 14:35:36 -06:00
environment.systemPackages = lib.mkIf cfg.powertop.enable [pkgs.powertop];
systemd.services.battery-charge-threshold = lib.mkIf cfg.battery.enable {
description = "Set battery charge thresholds";
wantedBy = ["multi-user.target"];
script = ''
if [ -f /sys/class/power_supply/BAT0/charge_start_threshold ]; then
echo ${toString cfg.battery.startChargeThreshold} > /sys/class/power_supply/BAT0/charge_start_threshold
fi
if [ -f /sys/class/power_supply/BAT0/charge_stop_threshold ]; then
echo ${toString cfg.battery.stopChargeThreshold} > /sys/class/power_supply/BAT0/charge_stop_threshold
fi
'';
};
};
}