{ config, lib, ... }: let # Define a more flexible power management module cfg = config.pwrMgnt; in { # Define options for customizable power management options.pwrMgnt = { 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 Configuration 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-specific settings (for laptops) 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; }; services.powertop.enable = cfg.powertop.enable; 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 ''; }; }; }