nix/config/Powersave.nix

57 lines
1.3 KiB
Nix
Raw Normal View History

{
lib,
config,
pkgs,
...
}:
let
cfg = config.module.powersave;
2024-06-25 04:04:39 +03:00
script = pkgs.writeShellScriptBin "powersave" ''
function toggle() {
if status; then
echo ${cfg.cpu.boost.disableCmd} > ${cfg.cpu.boost.controlFile}
else
echo ${cfg.cpu.boost.enableCmd} > ${cfg.cpu.boost.controlFile}
fi
2024-06-25 04:04:39 +03:00
pkill -RTMIN+5 waybar
true
}
2024-06-25 04:04:39 +03:00
function widget() {
status && printf '' || printf '󰓅'
}
2024-06-25 04:04:39 +03:00
function status() {
local current=$(cat ${cfg.cpu.boost.controlFile})
local enabled="${cfg.cpu.boost.enableCmd}"
2024-06-25 04:04:39 +03:00
[[ "''${current}" = "''${enabled}" ]]
}
2024-06-25 04:04:39 +03:00
''${@}
'';
in
{
config = lib.mkIf cfg.enable {
environment.systemPackages = [ script ];
systemd = {
services.powersave-cpu = {
enable = true;
description = "disable CPU Boost";
wantedBy = [ "multi-user.target" ];
serviceConfig = {
Type = "simple";
RemainAfterExit = "yes";
ExecStart = "${lib.getExe pkgs.bash} -c 'echo ${cfg.cpu.boost.enableCmd} > ${cfg.cpu.boost.controlFile}'";
ExecStop = "${lib.getExe pkgs.bash} -c 'echo ${cfg.cpu.boost.disableCmd} > ${cfg.cpu.boost.controlFile}'";
};
};
2024-06-25 04:04:39 +03:00
# HACK: Allow user access.
tmpfiles.rules = [ "z ${cfg.cpu.boost.controlFile} 0777 - - - -" ];
};
};
2024-06-25 04:04:39 +03:00
}