1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6 cfg = config.services.mbpfan;
7 verbose = if cfg.verbose then "v" else "";
8
9in {
10 options.services.mbpfan = {
11 enable = mkOption {
12 default = false;
13 type = types.bool;
14 description = ''
15 Whether to enable the mbpfan daemon.
16 '';
17 };
18
19 package = mkOption {
20 default = pkgs.mbpfan;
21 description = ''
22 The package used for the mbpfan daemon.
23 '';
24 };
25
26 minFanSpeed = mkOption {
27 type = types.int;
28 default = 2000;
29 description = ''
30 The minimum fan speed.
31 '';
32 };
33
34 maxFanSpeed = mkOption {
35 type = types.int;
36 default = 6200;
37 description = ''
38 The maximum fan speed.
39 '';
40 };
41
42 lowTemp = mkOption {
43 type = types.int;
44 default = 63;
45 description = ''
46 The low temperature.
47 '';
48 };
49
50 highTemp = mkOption {
51 type = types.int;
52 default = 66;
53 description = ''
54 The high temperature.
55 '';
56 };
57
58 maxTemp = mkOption {
59 type = types.int;
60 default = 86;
61 description = ''
62 The maximum temperature.
63 '';
64 };
65
66 pollingInterval = mkOption {
67 type = types.int;
68 default = 7;
69 description = ''
70 The polling interval.
71 '';
72 };
73
74 verbose = mkOption {
75 type = types.bool;
76 default = false;
77 description = ''
78 If true, sets the log level to verbose.
79 '';
80 };
81 };
82
83 config = mkIf cfg.enable {
84 boot.kernelModules = [ "coretemp" "applesmc" ];
85
86 environment = {
87 etc."mbpfan.conf".text = ''
88 [general]
89 min_fan_speed = ${toString cfg.minFanSpeed}
90 max_fan_speed = ${toString cfg.maxFanSpeed}
91 low_temp = ${toString cfg.lowTemp}
92 high_temp = ${toString cfg.highTemp}
93 max_temp = ${toString cfg.maxTemp}
94 polling_interval = ${toString cfg.pollingInterval}
95 '';
96 systemPackages = [ cfg.package ];
97 };
98
99 systemd.services.mbpfan = {
100 description = "A fan manager daemon for MacBook Pro";
101 wantedBy = [ "sysinit.target" ];
102 after = [ "syslog.target" "sysinit.target" ];
103 restartTriggers = [ config.environment.etc."mbpfan.conf".source ];
104 serviceConfig = {
105 Type = "simple";
106 ExecStart = "${cfg.package}/bin/mbpfan -f${verbose}";
107 ExecReload = "${pkgs.coreutils}/bin/kill -HUP $MAINPID";
108 PIDFile = "/var/run/mbpfan.pid";
109 Restart = "always";
110 };
111 };
112 };
113}