1{
2 config,
3 lib,
4 pkgs,
5 ...
6}:
7let
8
9 cfg = config.services.tuptime;
10
11in
12{
13
14 options.services.tuptime = {
15
16 enable = lib.mkEnableOption "the total uptime service";
17
18 timer = {
19 enable = lib.mkOption {
20 type = lib.types.bool;
21 default = true;
22 description = "Whether to regularly log uptime to detect bad shutdowns.";
23 };
24
25 period = lib.mkOption {
26 type = lib.types.str;
27 default = "*:0/5";
28 description = "systemd calendar event";
29 };
30 };
31 };
32
33 config = lib.mkIf cfg.enable {
34
35 environment.systemPackages = [ pkgs.tuptime ];
36
37 users = {
38 groups._tuptime.members = [ "_tuptime" ];
39 users._tuptime = {
40 isSystemUser = true;
41 group = "_tuptime";
42 description = "tuptime database owner";
43 };
44 };
45
46 systemd = {
47 services = {
48
49 tuptime = {
50 description = "The total uptime service";
51 documentation = [ "man:tuptime(1)" ];
52 after = [ "time-sync.target" ];
53 wantedBy = [ "multi-user.target" ];
54 serviceConfig = {
55 StateDirectory = "tuptime";
56 Type = "oneshot";
57 User = "_tuptime";
58 RemainAfterExit = true;
59 ExecStart = "${pkgs.tuptime}/bin/tuptime -q";
60 ExecStop = "${pkgs.tuptime}/bin/tuptime -qg";
61 };
62 };
63
64 tuptime-sync = lib.mkIf cfg.timer.enable {
65 description = "Tuptime scheduled sync service";
66 serviceConfig = {
67 Type = "oneshot";
68 User = "_tuptime";
69 ExecStart = "${pkgs.tuptime}/bin/tuptime -q";
70 };
71 };
72 };
73
74 timers.tuptime-sync = lib.mkIf cfg.timer.enable {
75 description = "Tuptime scheduled sync timer";
76 # this timer should be started if the service is started
77 # even if the timer was previously stopped
78 wantedBy = [
79 "tuptime.service"
80 "timers.target"
81 ];
82 # this timer should be stopped if the service is stopped
83 partOf = [ "tuptime.service" ];
84 timerConfig = {
85 OnBootSec = "1min";
86 OnCalendar = cfg.timer.period;
87 Unit = "tuptime-sync.service";
88 };
89 };
90 };
91 };
92
93 meta.maintainers = [ lib.maintainers.evils ];
94
95}