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