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 "the total uptime service";
14
15 timer = {
16 enable = mkOption {
17 type = types.bool;
18 default = true;
19 description = "Whether to regularly log uptime to detect bad shutdowns.";
20 };
21
22 period = mkOption {
23 type = types.str;
24 default = "*:0/5";
25 description = "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 description = "tuptime database owner";
40 };
41 };
42
43 systemd = {
44 services = {
45
46 tuptime = {
47 description = "the total uptime service";
48 documentation = [ "man:tuptime(1)" ];
49 after = [ "time-sync.target" ];
50 wantedBy = [ "multi-user.target" ];
51 serviceConfig = {
52 StateDirectory = "tuptime";
53 Type = "oneshot";
54 User = "_tuptime";
55 RemainAfterExit = true;
56 ExecStart = "${pkgs.tuptime}/bin/tuptime -x";
57 ExecStop = "${pkgs.tuptime}/bin/tuptime -xg";
58 };
59 };
60
61 tuptime-oneshot = mkIf cfg.timer.enable {
62 description = "the tuptime scheduled execution unit";
63 serviceConfig = {
64 StateDirectory = "tuptime";
65 Type = "oneshot";
66 User = "_tuptime";
67 ExecStart = "${pkgs.tuptime}/bin/tuptime -x";
68 };
69 };
70 };
71
72 timers.tuptime = mkIf cfg.timer.enable {
73 description = "the tuptime scheduled execution 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-oneshot.service";
83 };
84 };
85 };
86 };
87
88 meta.maintainers = [ maintainers.evils ];
89
90}