2022-03-08 01:42:46 +00:00
|
|
|
{
|
|
|
|
pkgs,
|
|
|
|
lib,
|
|
|
|
...
|
|
|
|
}: let
|
2021-11-01 20:51:17 +00:00
|
|
|
src = pkgs.fetchurl {
|
|
|
|
url = "https://github.com/zgoat/goatcounter/releases/download/v2.0.4/goatcounter-v2.0.4-linux-amd64.gz";
|
|
|
|
sha256 = "1h7hv5lk2gm3klbfbgwfa7xn31az9zmlryd8bqqq38lp5r56cpb4";
|
|
|
|
};
|
2022-03-08 01:42:46 +00:00
|
|
|
goatcounter-bin = pkgs.runCommand "goatcounter-bin" {} ''
|
2021-11-01 20:51:17 +00:00
|
|
|
mkdir -p $out/bin
|
|
|
|
gunzip -c ${src} > $out/bin/goatcounter
|
|
|
|
chmod +x $out/bin/goatcounter
|
|
|
|
'';
|
2021-11-02 13:17:38 +00:00
|
|
|
goatcounter-token = pkgs.privateValue "" "goatcounter-token";
|
2022-03-08 01:42:46 +00:00
|
|
|
in {
|
|
|
|
environment.systemPackages = [goatcounter-bin];
|
2021-12-03 01:37:58 +00:00
|
|
|
users = {
|
|
|
|
users.goatcounter = {
|
|
|
|
isSystemUser = true;
|
|
|
|
group = "goatcounter";
|
|
|
|
};
|
2022-03-08 01:42:46 +00:00
|
|
|
groups.goatcounter = {};
|
2021-12-03 01:37:58 +00:00
|
|
|
};
|
2021-11-01 20:51:17 +00:00
|
|
|
services.postgresql = {
|
|
|
|
enable = true;
|
2022-03-08 01:42:46 +00:00
|
|
|
ensureUsers = [
|
|
|
|
{
|
|
|
|
name = "goatcounter";
|
|
|
|
ensurePermissions = {
|
|
|
|
"DATABASE goatcounter" = "ALL PRIVILEGES";
|
|
|
|
};
|
|
|
|
}
|
|
|
|
];
|
|
|
|
ensureDatabases = ["goatcounter"];
|
2021-11-01 20:51:17 +00:00
|
|
|
};
|
2021-11-01 22:57:30 +00:00
|
|
|
services.nginx = {
|
|
|
|
appendHttpConfig = lib.mkAfter ''
|
|
|
|
log_format vcombined '$host $remote_addr - $remote_user [$time_local] '
|
|
|
|
'"$request" $status $body_bytes_sent '
|
|
|
|
'"$http_referer" "$http_user_agent"';
|
|
|
|
access_log /run/nginx/access.log vcombined;
|
|
|
|
'';
|
|
|
|
virtualHosts."analytics.maralorn.de" = {
|
|
|
|
enableACME = true;
|
|
|
|
forceSSL = true;
|
|
|
|
locations."/".proxyPass = "http://localhost:8081/";
|
|
|
|
};
|
|
|
|
};
|
2021-11-01 20:51:17 +00:00
|
|
|
systemd.services = {
|
|
|
|
goatcounter = {
|
2022-03-08 01:42:46 +00:00
|
|
|
requires = ["postgresql.service"];
|
|
|
|
after = ["postgresql.service"];
|
2021-11-01 20:51:17 +00:00
|
|
|
serviceConfig = {
|
|
|
|
User = "goatcounter";
|
|
|
|
ExecStart = "${goatcounter-bin}/bin/goatcounter serve -db 'postgresql://host=/run/postgresql dbname=goatcounter' -listen *:8081 -tls http -automigrate";
|
2021-11-26 04:45:58 +00:00
|
|
|
WatchdogSignal = "SIGTERM";
|
|
|
|
WatchdogSec = "20m";
|
|
|
|
Restart = "always";
|
2021-11-01 20:51:17 +00:00
|
|
|
};
|
2022-03-08 01:42:46 +00:00
|
|
|
wantedBy = ["multi-user.target"];
|
2021-11-01 20:51:17 +00:00
|
|
|
};
|
|
|
|
goatcounter-feeder = {
|
2022-03-08 01:42:46 +00:00
|
|
|
requires = ["goatcounter.service"];
|
|
|
|
after = ["goatcounter.service"];
|
2021-11-23 13:51:56 +00:00
|
|
|
serviceConfig = {
|
|
|
|
User = "nginx";
|
|
|
|
Type = "oneshot";
|
|
|
|
};
|
2021-11-01 20:51:17 +00:00
|
|
|
script = ''
|
2021-11-23 13:51:56 +00:00
|
|
|
(cat /run/nginx/access.log && truncate --size 0 /run/nginx/access.log) |\
|
|
|
|
sed 's/\([^ ]*\) \(.*"[^ ]* \/\)/\2\1\//; s/ \(\/.*\)?[^ ]* / \1 /' |\
|
|
|
|
GOATCOUNTER_API_KEY=${goatcounter-token} ${goatcounter-bin}/bin/goatcounter import -follow -site http://localhost:8081 - -format combined \
|
|
|
|
-exclude '!method:GET' \
|
|
|
|
-exclude 'remote_addr:2a02:c207:3002:7584:' \
|
|
|
|
-exclude 'remote_addr:glob:::1' \
|
|
|
|
-exclude 'remote_addr:127.0.0.1' \
|
|
|
|
-exclude redirect
|
2021-11-01 20:51:17 +00:00
|
|
|
'';
|
2021-11-23 13:51:56 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
systemd.timers = {
|
|
|
|
goatcounter-feeder = {
|
|
|
|
timerConfig.OnCalendar = "minutely";
|
2022-03-08 01:42:46 +00:00
|
|
|
wantedBy = ["timers.target"];
|
2021-11-01 20:51:17 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|