{ config, lib, pkgs, ... }: let cfg = config.pwrMgmt; in { 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; }; 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 ''; }; }; }