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

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

  config = let
    db = "gotify";
  in
    mkIf cfg.enable {
      nixfiles.modules = {
        nginx = {
          enable = true;
          upstreams.gotify.servers."127.0.0.1:${toString config.services.gotify.port}" = {};
          virtualHosts.${cfg.domain} = {
            locations."/" = {
              proxyPass = "http://gotify";
              proxyWebsockets = true;
            };
            extraConfig = nginxInternalOnly;
          };
        };
        postgresql = {
          enable = true;
          extraPostStart = [
            ''
              $PSQL "${db}" -tAc 'GRANT ALL ON SCHEMA "public" TO "${db}"'
            ''
          ];
        };
      };

      services = {
        gotify = {
          enable = true;
          port = 7665;
        };

        postgresql = {
          ensureDatabases = [db];
          ensureUsers = [
            {
              name = db;
              ensurePermissions."DATABASE \"${db}\"" = "ALL";
            }
          ];
        };
      };

      systemd.services.gotify-server = {
        after = ["network-online.target" "postgresql.service"];
        environment = {
          GOTIFY_DATABASE_DIALECT = "postgres";
          GOTIFY_DATABASE_CONNECTION = concatStringsSep " " [
            "host=/run/postgresql"
            "user=${db}"
            "dbname=${db}"
            "sslmode=disable"
          ];
        };
      };
    };
}

Consider giving Nix/NixOS a try! <3