summaryrefslogtreecommitdiff
path: root/nixos/modules/services/pushover.nix
blob: d5580227dac4904d598fc0bb3208bc719d7f47bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
{ config, lib, pkgs, ... }:
with lib;

let
  cfg = config.eth.services.pushover;

  escapeName = name: "pushover-${replaceStrings [ " " "'" ] [ "-" "" ] name}";

  mkService = name: opts: {
    name = escapeName name;
    value = {
      description = "Send ${name} notification";
      serviceConfig = {
        DynamicUser = true;
        ExecStart = pkgs.writeShellScript (escapeName name) ''
          ${pkgs.curl}/bin/curl \
            --verbose \
            --form-string user=${cfg.userKey} \
            --form-string token=${cfg.apiKey} \
            --form-string message=${escapeShellArg opts.message} \
            https://api.pushover.net/1/messages.json
        '';
        Environment = [ "HOME=/tmp" ];
        NoNewPrivileges = true;
        ProtectHome = true;
        ProtectKernelTunables = true;
        ProtectControlGroups = true;
        ProtectKernelModules = true;
        RestrictAddressFamilies = "AF_INET AF_INET6";
      };
    };
  };

  mkTimer = name: opts: {
    name = escapeName name;
    value = {
      description = "Periodically send ${name} notification";
      wants = [ "network.target" ];
      after = [ "network.target" ];
      wantedBy = [ "timers.target" ];
      timerConfig = {
        Unit = "${escapeName name}.service";
        OnCalendar = opts.schedule;
        RandomizedDelaySec = opts.delayUpTo;
        Persistent = true;
      };
    };
  };

in {
  options.eth.services.pushover = {
    enable = mkEnableOption "Send reminders with Pushover";

    userKey = mkOption {
      type = types.str;
      description = "Your user key (NB: this will go into the Nix store)";
    };
    apiKey = mkOption {
      type = types.str;
      description = "The application API key (NB: this will go into the Nix store)";
    };

    reminders = mkOption {
      type = types.attrsOf (types.submodule {
        options = {
          enable = mkEnableOption "Send a reminder with Pushover";

          message = mkOption {
            type = types.str;
            description = "The message to send.";
          };

          schedule = mkOption {
            type = types.str;
            description = "A systemd.time timespec.";
          };
          delayUpTo = mkOption {
            type = types.str;
            description = "A systemd.time duration.";
          };
        };
      });
    };
  };

  config = mkIf cfg.enable {
    systemd = {
      services = mapAttrs' mkService cfg.reminders;
      timers = mapAttrs' mkTimer cfg.reminders;
    };
  };
}