about summary refs log tree commit diff
path: root/modules/plausible.nix
blob: 94e0d9df2a23257708e9fe5d0ea3ab1ba91913c5 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
{
  config,
  inputs,
  lib,
  this,
  ...
}:
let
  cfg = config.nixfiles.modules.plausible;
in
{
  options.nixfiles.modules.plausible = {
    enable = lib.mkEnableOption "Plausible Analytics";

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

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

  config =
    let
      db = "plausible";
    in
    lib.mkIf cfg.enable {
      _module.args.libPlausible = {
        htmlPlausibleScript =
          {
            domain ? "$host",
            src ? "https://${cfg.domain}/js/script.js",
          }:
          ''<script defer data-domain="${domain}" src="${src}"></script>'';
      };

      secrets = {
        plausible-key.file = "${inputs.self}/secrets/plausible-key";
        plausible-admin-password.file = "${inputs.self}/secrets/plausible-admin-password";
        plausible-smtp-password.file = "${inputs.self}/secrets/smtp-password";
      };

      nixfiles.modules = {
        nginx = {
          enable = true;
          upstreams.plausible.servers."127.0.0.1:${toString cfg.port}" = { };
          virtualHosts.${cfg.domain}.locations."/" = {
            proxyPass = "http://plausible";
            proxyWebsockets = true;
          };
        };
        postgresql = {
          enable = true;
          extraPostStart = [
            ''
              $PSQL "${db}" -tAc 'GRANT ALL ON SCHEMA "public" TO "${db}"'
              $PSQL "${db}" -tAc 'CREATE EXTENSION IF NOT EXISTS citext'
            ''
          ];
        };
        clickhouse.enable = true;
      };

      services.postgresql = {
        ensureDatabases = [ db ];
        ensureUsers = [
          {
            name = db;
            ensureDBOwnership = true;
          }
        ];
      };

      services.plausible = {
        enable = true;

        # adminUser = {
        #   name = "admin";
        #   email = "admin@${my.domain.shire}";
        #   passwordFile = config.secrets.plausible-admin-password.path;
        #   activate = false;
        # };

        mail = {
          email = "webmaster@${lib.my.domain.shire}";
          smtp = {
            hostAddr = lib.my.domain.shire;
            hostPort = 465;
            enableSSL = true;
            user = "azahi@${lib.my.domain.shire}";
            passwordFile = config.secrets.plausible-smtp-password.path;
          };
        };

        database = {
          clickhouse = {
            setup = false;
            url = "http://127.0.0.1:8123/default";
          };

          postgres = {
            setup = true;
            dbname = db;
          };
        };

        server = {
          baseUrl = "https://${cfg.domain}";
          disableRegistration = true;
          listenAddress = "127.0.0.1";
          inherit (cfg) port;
          secretKeybaseFile = config.secrets.plausible-key.path;
        };
      };

      systemd.services.plausible = rec {
        after = [
          "postgresql.service"
          "clickhouse.service"
        ];
        requires = after;
      };

      topology.nodes.${this.hostname}.services.plausible = {
        name = "Plausible";
        icon = "${inputs.homelab-svg-assets}/assets/plausible.svg";
        info = cfg.domain;
        details.listen.text = lib.concatStringsSep ":" [
          config.services.plausible.server.listenAddress
          (toString cfg.port)
        ];
      };
    };
}

Consider giving Nix/NixOS a try! <3