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