aboutsummaryrefslogtreecommitdiff
path: root/modules/services/nixos/goaccess.nix
blob: 15a3768efbe4ceeba89406b1274472282f144b1f (plain)
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
{
  config,
  pkgs,
  inputs,
  lib,
  ...
}: let
  cfg = config.collinux.services.goaccess;

  modularService = {
    lib,
    config,
    options,
    ...
  }: let
    cfg = config.goaccess;

    settings =
      cfg.settings
      // {
        unix-socket = "/run/goaccess/goaccess.sock";
        output = "/run/goaccess/www/index.html";
      };
  in {
    options.goaccess = {
      package = lib.mkOption {
        description = "the `goaccess` package";
        type = lib.types.package;
      };
      settings = lib.mkOption {
        description = "goaccess.conf options";
        type = lib.types.attrsOf lib.types.str;
      };
    };

    config =
      {
        configData."goaccess.conf".text =
          settings
          |> lib.mapAttrsToList (k: v: "${k} ${v}")
          |> lib.concatStringsSep "\n";

        process.argv = [(lib.getExe cfg.package) "-p" config.configData."goaccess.conf".path];
      }
      // lib.optionalAttrs (options ? systemd) {
        systemd.service = {
          description = "GoAccess Real-Time Log Analyzer";
          restartIfChanged = true;
          wants = ["network-online.target" "caddy.service"];
          after = ["network-online.target" "caddy.service"];
          wantedBy = ["multi-user.target"];

          serviceConfig = {
            DynamicUser = true;
            Group = "caddy";

            RuntimeDirectory = ["goaccess" "goaccess/www"];
            RuntimeDirectoryMode = "0750";
            UMask = "0007"; # socket -> 0770, files -> 0660

            NoNewPrivileges = true;
            PrivateTmp = true;
            ProtectSystem = "strict";
            ProtectHome = true;
            ProtectKernelTunables = true;
            ProtectKernelModules = true;
            ProtectControlGroups = true;

            Restart = "on-failure";
          };
        };
      };
  };
in {
  config = lib.mkIf cfg.enable {
    system.services."goaccess" = {
      imports = [modularService];
      goaccess = {
        package = pkgs.goaccess;
        settings = {
          date-format = "%s";
          log-format = "CADDY";
          tz = config.time.timeZone;
          log-file = "/var/log/caddy/access-williamsfam.us.com.log";
          geoip-database = toString inputs.geolite-db;

          ws-url = "wss://stats.ganymede:443/ws"; # url that the frontend uses to fetch data

          real-time-html = "true";
          external-assets = "true";
          all-static-files = "false";
          html-report-title = "stats@ganymede";
          hl-header = "true";
          agent-list = "false";
          with-output-resolver = "false";
          http-method = "yes";
          http-protocol = "yes";
          "4xx-to-unique-count" = "false";
          ignore-crawlers = "false";
          crawlers-only = "false";
          unknowns-as-crawlers = "false";
          real-os = "true";
        };
      };
    };

    services.caddy.virtualHosts."stats.ganymede".extraConfig = ''
      tls internal

      root * /run/goaccess/www
      file_server

      reverse_proxy /ws unix//run/goaccess/goaccess.sock
    '';

    collinux.services.glance.homelabServices."stats" = {
      url = "https://stats.ganymede";
      icon = "mdi:poll";
    };
  };
}