diff options
author | Ethel Morgan <eth@ethulhu.co.uk> | 2020-07-27 20:48:09 +0100 |
---|---|---|
committer | Ethel Morgan <eth@ethulhu.co.uk> | 2020-07-27 20:48:09 +0100 |
commit | e130424c2fd01f2c2bac23832e2e88c9604cc78a (patch) | |
tree | b078826b04228bb83cf4d92160358627d21afe01 /nixos/modules/services | |
parent | b6e49129d2a3a01dd1ad436e4144bacbe89d1c30 (diff) |
add eth.services.pushover, for random reminders
Diffstat (limited to '')
-rw-r--r-- | nixos/modules/services/pushover.nix | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/nixos/modules/services/pushover.nix b/nixos/modules/services/pushover.nix new file mode 100644 index 0000000..d558022 --- /dev/null +++ b/nixos/modules/services/pushover.nix @@ -0,0 +1,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; + }; + }; +} |