nixpkgs/nixos/modules/services/hardware/tlp.nix

96 lines
3.2 KiB
Nix
Raw Normal View History

2015-01-02 15:12:11 +00:00
{ config, lib, pkgs, ... }:
with lib;
let
2020-02-26 04:19:27 +00:00
cfg = config.services.tlp;
enableRDW = config.networking.networkmanager.enable;
tlp = pkgs.tlp.override { inherit enableRDW; };
# TODO: Use this for having proper parameters in the future
mkTlpConfig = tlpConfig: generators.toKeyValue {
mkKeyValue = generators.mkKeyValueDefault {
mkValueString = val:
if isInt val then toString val
else if isString val then val
else if true == val then "1"
else if false == val then "0"
else if isList val then "\"" + (concatStringsSep " " val) + "\""
else err "invalid value provided to mkTlpConfig:" (toString val);
} "=";
} tlpConfig;
2015-01-02 15:12:11 +00:00
in
{
###### interface
options = {
services.tlp = {
enable = mkOption {
type = types.bool;
default = false;
2019-11-18 23:14:03 +00:00
description = "Whether to enable the TLP power management daemon.";
2015-01-02 15:12:11 +00:00
};
extraConfig = mkOption {
2016-10-23 17:33:41 +00:00
type = types.lines;
2015-01-02 15:12:11 +00:00
default = "";
description = "Additional configuration variables for TLP";
};
};
};
###### implementation
config = mkIf cfg.enable {
2020-02-26 04:19:27 +00:00
boot.kernelModules = [ "msr" ];
2015-01-02 15:12:11 +00:00
2020-02-26 04:19:27 +00:00
environment.etc = {
"tlp.conf".text = cfg.extraConfig;
} // optionalAttrs enableRDW {
"NetworkManager/dispatcher.d/99tlp-rdw-nm".source =
"${tlp}/etc/NetworkManager/dispatcher.d/99tlp-rdw-nm";
};
2020-02-26 04:19:27 +00:00
environment.systemPackages = [ tlp ];
2020-02-26 04:19:27 +00:00
# FIXME: When the config is parametrized we need to move these into a
# conditional on the relevant options being enabled.
powerManagement = {
scsiLinkPolicy = null;
cpuFreqGovernor = null;
cpufreq.max = null;
cpufreq.min = null;
};
2020-02-26 04:19:27 +00:00
services.udev.packages = [ tlp ];
2015-01-02 15:12:11 +00:00
2020-02-26 04:19:27 +00:00
systemd = {
packages = [ tlp ];
# XXX: These must always be disabled/masked according to [1].
#
# [1]: https://github.com/linrunner/TLP/blob/a9ada09e0821f275ce5f93dc80a4d81a7ff62ae4/tlp-stat.in#L319
sockets.systemd-rfkill.enable = false;
services.systemd-rfkill.enable = false;
services.tlp = {
# XXX: The service should reload whenever the configuration changes,
# otherwise newly set power options remain inactive until reboot (or
# manual unit restart.)
restartTriggers = [ config.environment.etc."tlp.conf".source ];
# XXX: When using systemd.packages (which we do above) the [Install]
# section of systemd units does not work (citation needed) so we manually
# enforce it here.
2015-01-02 15:12:11 +00:00
wantedBy = [ "multi-user.target" ];
};
2020-02-26 04:19:27 +00:00
services.tlp-sleep = {
# XXX: When using systemd.packages (which we do above) the [Install]
# section of systemd units does not work (citation needed) so we manually
# enforce it here.
2015-01-02 15:12:11 +00:00
before = [ "sleep.target" ];
2020-02-26 04:19:27 +00:00
wantedBy = [ "sleep.target" ];
# XXX: `tlp suspend` requires /var/lib/tlp to exist in order to save
# some stuff in there. There is no way, that I know of, to do this in
# the package itself, so we do it here instead making sure the unit
# won't fail due to the save dir not existing.
serviceConfig.StateDirectory = "tlp";
2015-01-02 15:12:11 +00:00
};
};
};
}