nix/module/Powersave.nix

80 lines
1.6 KiB
Nix
Raw Normal View History

{
2024-11-04 04:37:29 +03:00
lib,
config,
pkgs,
util,
...
}: let
cfg = config.module.powersave;
2024-06-25 04:04:39 +03:00
2024-11-04 04:37:29 +03:00
script = pkgs.writeShellScriptBin "powersave" (util.trimTabs ''
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
2024-11-04 04:37:29 +03:00
pkill -RTMIN+5 waybar
true
}
2024-06-25 04:04:39 +03:00
2024-11-04 04:37:29 +03:00
function widget() {
status && printf '' || printf '󰓅'
}
2024-06-25 04:04:39 +03:00
2024-11-04 04:37:29 +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
2024-11-04 04:37:29 +03:00
[[ "''${current}" = "''${enabled}" ]]
}
2024-06-25 04:04:39 +03:00
2024-11-04 04:37:29 +03:00
''${@}
'');
in {
options.module.powersave = {
enable = lib.mkEnableOption "the powersave";
cpu.boost = {
disableCmd = lib.mkOption {
default = null;
type = lib.types.str;
};
enableCmd = lib.mkOption {
default = null;
type = lib.types.str;
};
controlFile = lib.mkOption {
default = null;
type = lib.types.str;
};
};
};
2024-06-25 04:04:39 +03:00
2024-11-04 04:37:29 +03:00
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
2024-11-04 04:37:29 +03:00
# HACK: Allow user access.
tmpfiles.rules = [
"z ${cfg.cpu.boost.controlFile} 0777 - - - -"
];
};
};
2024-06-25 04:04:39 +03:00
}