From ef34dbb44f5d8e6803676e2f2fc0f40268fdcc70 Mon Sep 17 00:00:00 2001 From: Tigor Hutasuhut Date: Fri, 11 Oct 2024 09:45:12 +0700 Subject: [PATCH] ntfy: update client config to auto subscribe --- options/services.nix | 14 ++++++++++---- system/podman/ytptube.nix | 17 +++++++++++++---- system/services/ntfy-sh.nix | 38 ++++++++++++++++++++++++++++--------- 3 files changed, 52 insertions(+), 17 deletions(-) diff --git a/options/services.nix b/options/services.nix index 68e8f8b..44d91a8 100644 --- a/options/services.nix +++ b/options/services.nix @@ -1,4 +1,4 @@ -{ config, lib, ... }: +{ config, lib, pkgs, ... }: let inherit (lib) mkEnableOption mkOption types; in @@ -24,9 +24,15 @@ in navidrome.enable = mkEnableOption "navidrome"; ntfy-sh.enable = mkEnableOption "ntfy-sh"; - ntfy-sh.client.enable = mkOption { - type = types.bool; - default = config.profile.services.ntfy-sh.enable; + ntfy-sh.client = { + settings = lib.mkOption { + type = (pkgs.formats.yaml { }).type; + default = { }; + }; + enable = mkOption { + type = types.bool; + default = config.profile.services.ntfy-sh.enable; + }; }; couchdb.enable = mkEnableOption "couchdb"; diff --git a/system/podman/ytptube.nix b/system/podman/ytptube.nix index 28f67ee..af3e792 100644 --- a/system/podman/ytptube.nix +++ b/system/podman/ytptube.nix @@ -59,8 +59,8 @@ let } ]); in -{ - config = mkIf (podman.enable && podman.${name}.enable) { +lib.mkMerge [ + (mkIf podman.${name}.enable { sops = { secrets = let @@ -142,6 +142,7 @@ in mode = "0444"; }; + virtualisation.oci-containers.containers.${name} = { inherit image; hostname = name; @@ -163,5 +164,13 @@ in "io.containers.autoupdate" = "registry"; }; }; - }; -} + }) + { + profile.services.ntfy-sh.client.settings.subscribe = [ + { + topic = "ytptube"; + command = ''${pkgs.libnotify}/bin/notify-send --category=im.received --urgency=normal "$title" "$message"''; + } + ]; + } +] diff --git a/system/services/ntfy-sh.nix b/system/services/ntfy-sh.nix index 1e1f207..009595e 100644 --- a/system/services/ntfy-sh.nix +++ b/system/services/ntfy-sh.nix @@ -5,6 +5,7 @@ let inherit (lib) mkIf; domain = "ntfy.tigor.web.id"; listenAddress = "0.0.0.0:15150"; + configPath = "/etc/ntfy/client.yml"; in lib.mkMerge [ (mkIf cfg.enable { @@ -46,7 +47,26 @@ lib.mkMerge [ ]; environment.sessionVariables = { - NTFY_CONFIG = "/etc/ntfy/client.yml"; + NTFY_CONFIG = configPath; + }; + + systemd.services.ntfy-client = { + enable = true; + wantedBy = [ "multi-user.target" ]; + description = "ntfy client"; + after = [ "network-online.target" ]; + restartTriggers = [ (builtins.toJSON cfg.client.settings) ]; + environment = { + DISPLAY = ":0"; + DBUS_SESSION_BUS_ADDRESS = "unix:path=/run/user/${toString config.profile.user.uid}/bus"; + }; + path = [ pkgs.bash ]; + serviceConfig = { + User = toString config.profile.user.uid; + Group = toString config.profile.user.gid; + ExecStart = "${pkgs.ntfy-sh}/bin/ntfy subscribe --config ${configPath} --from-config"; + Restart = "on-failure"; + }; }; sops = { @@ -55,20 +75,20 @@ lib.mkMerge [ opts = { sopsFile = ../../secrets/ntfy.yaml; }; in { - "ntfy/default/user" = opts; - "ntfy/default/password" = opts; + "ntfy/tokens/tigor" = opts; }; templates = let filename = "ntfy-client.yaml"; in { ${filename} = { - content = builtins.readFile ((pkgs.formats.yaml { }).generate filename { - default-host = "https://${domain}"; - default-user = config.sops.placeholder."ntfy/default/user"; - default-password = config.sops.placeholder."ntfy/default/password"; - }); - path = "/etc/ntfy/client.yml"; + content = builtins.readFile ((pkgs.formats.yaml { }).generate filename ( + { + default-host = "https://${domain}"; + detault-token = config.sops.placeholder."ntfy/tokens/tigor"; + } // cfg.client.settings + )); + path = configPath; owner = config.profile.user.name; }; };