about summary refs log tree commit diff stats
path: root/modules/nixos/goatcounter.nix
blob: 2ba0e315ed08ac5ecd48d9e8601cc030e3d7855a (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
{ config
, lib
, pkgs
, ...
}:
let
  cfg = config.services.goatcounter;

  inherit (lib)
    optionalAttrs
    mkEnableOption
    mkPackageOption
    mkOption
    mkIf
    types;

  encodeParams = params: lib.concatStringsSep "&" (lib.mapAttrsToList (n: v: "${n}=${v}") params);
  encoder = {
    bool = n: v: lib.optionalString v "-${n}";
    int = n: v: "-${n} ${toString v}";
    list = n: v: "-${n} ${lib.concatStringsSep "," v}";
    string = n: v: "-${n} ${v}";
  };
  mkArgs = args: lib.concatStringsSep " " (lib.mapAttrsToList (n: v: (encoder.${builtins.typeOf v} n v)) args);
in
{
  options.services.goatcounter = {
    enable = mkEnableOption "Easy web analytics. No tracking of personal data.";

    user = mkOption {
      type = types.str;
      default = "goatcounter";
      description = "User account under which goatcounter runs.";
    };

    group = mkOption {
      type = types.str;
      default = "goatcounter";
      description = "Group under which goatcounter runs.";
    };

    package = mkPackageOption pkgs "goatcounter" { };

    homeDir = mkOption {
      type = types.path;
      default = "/var/lib/goatcounter";
      description = "Home directory for goatcounter user.";
    };

    database = mkOption {
      default = {
        type = "sqlite";
        file = "db/goatcounter.sqlite3";
      };

      type = types.submodule {
        options = {
          type = mkOption {
            type = types.enum [ "sqlite" "postgresql" ];
            default = "sqlite";
            description = "Database engine to use.";
          };

          file = mkOption {
            type = with types; nullOr str;
            default = null;
            description = "(sqlite) database file to use.";
          };

          params = mkOption {
            type = with types; attrsOf str;
            default = { };
            description = "Database connection parameters. See `goatcounter help db`";
          };
        };
      };
    };

    listenAddress = mkOption {
      type = types.str;
      default = "*";
      description = "Start the server on the specified address.";
    };

    port = mkOption {
      type = types.port;
      default = 8581;
      description = "Port for goatcounter to listen on.";
    };


    settings = mkOption {
      type = types.submodule rec {
        freeformType = types.anything;

        options = {
          tls =
            let
              values = (types.enum [ "http" "proxy" "tls" "rdr" "acme" ]);
            in
            mkOption {
              type = with types; either str (nonEmptyListOf values);
              default = [ "acme" "rdr" ];
              description = "Whether and how to handle TLS connections. See `goatcounter help listen`";
            };
        };
      };
    };
  };

  config = mkIf cfg.enable {
    services.goatcounter.settings = {
      listen = lib.mkDefault "${cfg.listenAddress}:${toString cfg.port}";
      db = lib.mkDefault "${cfg.database.type}+${cfg.database.file}?${encodeParams cfg.database.params}";
    };

    systemd.services.goatcounter = {
      description = "Goatcounter web analytics";
      wantedBy = [ "multi-user.target" ];
      serviceConfig = {
        ExecStart = "${cfg.package}/bin/goatcounter serve ${mkArgs cfg.settings}";
        User = cfg.user;
        Group = cfg.group;
        WorkingDirectory = cfg.homeDir;
        ReadWritePaths = [ cfg.homeDir ];
      } // optionalAttrs (cfg.port < 1024) {
        AmbientCapabilities = [ "CAP_NET_BIND_SERVICE" ];
        CapabilityBoundingSet = [ "CAP_NET_BIND_SERVICE" ];
      };
    };

    users.users = optionalAttrs (cfg.user == "goatcounter") {
      goatcounter = {
        inherit (cfg) group;
        home = cfg.homeDir;
        createHome = true;
        isSystemUser = true;
      };
    };

    users.groups = optionalAttrs (cfg.group == "goatcounter") {
      goatcounter = { };
    };

    environment.systemPackages = [ cfg.package ];
  };
}