From e130424c2fd01f2c2bac23832e2e88c9604cc78a Mon Sep 17 00:00:00 2001 From: Ethel Morgan Date: Mon, 27 Jul 2020 20:48:09 +0100 Subject: add eth.services.pushover, for random reminders --- nixos/modules/module-list.nix | 1 + nixos/modules/services/pushover.nix | 92 +++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 nixos/modules/services/pushover.nix (limited to 'nixos') diff --git a/nixos/modules/module-list.nix b/nixos/modules/module-list.nix index 247d9a6..e7d7494 100644 --- a/nixos/modules/module-list.nix +++ b/nixos/modules/module-list.nix @@ -20,6 +20,7 @@ ./services/helix-directory.nix ./services/helix-player.nix ./services/mosquitto.nix + ./services/pushover.nix ./services/snapclient.nix ./services/ssh.nix ./services/upmpdcli.nix 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; + }; + }; +} -- cgit v1.2.3