summaryrefslogtreecommitdiffstats
path: root/nixos/modules/services/hardware/watt.nix
blob: 56b3d6f5036dcee40caebdaceea4dde0135f5738 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
{
  config,
  pkgs,
  lib,
  ...
}:
let
  inherit (lib)
    mkIf
    mkOption
    mkEnableOption
    mkPackageOption
    getExe
    ;
  inherit (lib.types) submodule;

  cfg = config.services.watt;

  format = pkgs.formats.toml { };
  cfgFile = format.generate "watt-config.toml" cfg.settings;

  conflictingServices = [
    "power-profiles-daemon"
    "auto-cpufreq"
    "tlp"
    "cpupower-gui"
    "thermald"
  ];

in
{
  options.services.watt = {
    enable = mkEnableOption "automatic CPU speed & power optimizer for Linux";
    package = mkPackageOption pkgs "watt" { };

    settings = mkOption {
      default = { };
      type = submodule { freeformType = format.type; };
      description = "Configuration for Watt. Options at https://github.com/notaShelf/watt";
    };
  };

  config = mkIf cfg.enable {
    assertions = map (service: {
      assertion = !config.services.${service}.enable;
      message = "You have set services.${service}.enable = true; which conflicts with Watt.";
    }) conflictingServices;

    environment.systemPackages = [ cfg.package ];

    # This is necessary for the Watt CLI. The environment variable
    # passed to the systemd service will take priority in read order.
    environment.etc."watt.toml".source = cfgFile;

    services.dbus.packages = [ cfg.package ];

    systemd.services.watt = {
      wantedBy = [ "multi-user.target" ];
      conflicts = map (service: "${service}.service") conflictingServices;
      serviceConfig = {
        WorkingDirectory = "";
        ExecStart = getExe cfg.package;
        Restart = "on-failure";

        RuntimeDirectory = "watt";
        RuntimeDirectoryMode = "0755";
      };
    };

  };
}