about summary refs log tree commit diff
path: root/modules/nixos/ntfy.nix
blob: edbe7e5583e9ea7382604259f262a14feeff7fd5 (plain) (blame)
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
{
  config,
  lib,
  this,
  ...
}:
with lib; let
  cfg = config.nixfiles.modules.ntfy;
in {
  options.nixfiles.modules.ntfy = {
    enable = mkEnableOption "ntfy";

    port = mkOption {
      description = "Port.";
      type = types.port;
      default = 2586;
    };

    domain = mkOption {
      description = "Domain name sans protocol scheme.";
      type = with types; str;
      default = "ntfy.${config.networking.domain}";
    };

    prometheus = {
      enable = mkEnableOption "Prometheus exporter." // {default = true;};

      address = mkOption {
        description = "Address.";
        type = with types; str;
        default = this.wireguard.ipv4.address;
      };

      port = mkOption {
        description = "Port.";
        type = with types; port;
        default = 9289;
      };
    };
  };

  config = mkIf cfg.enable {
    ark.files = [config.services.ntfy-sh.settings.auth-file];

    nixfiles.modules.nginx = {
      enable = true;
      upstreams.ntfy.servers.${config.services.ntfy-sh.settings.listen-http} = {};
      virtualHosts.${cfg.domain} = {
        locations = {
          "/" = {
            proxyPass = "http://ntfy";
            proxyWebsockets = true;
          };
          "/metrics".extraConfig = ''
            deny all;
          '';
        };
        extraConfig = nginxInternalOnly;
      };
    };

    services.ntfy-sh = {
      enable = true;
      settings = {
        listen-http = "127.0.0.1:${toString cfg.port}";
        base-url = "https://${cfg.domain}";
        cache-file = "/var/cache/ntfy/cache.db";
        behind-proxy = true;
        attachment-cache-dir = "/var/cache/ntfy/attachments";
        auth-file = "/var/lib/ntfy/user.db";
        enable-metrics = cfg.prometheus.enable;
        metrics-listen-http = with cfg.prometheus;
          optionalString cfg.prometheus.enable "${address}:${toString port}";
      };
    };

    systemd.tmpfiles.rules = with config.services.ntfy-sh; [
      "d /var/lib/ntfy 0700 ${user} ${group} - -"
      "d /var/cache/ntfy 0700 ${user} ${group} - -"
      "d /var/cache/ntfy/attachments 0700 ${user} ${group} - -"
    ];
  };
}

Consider giving Nix/NixOS a try! <3