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