From aebb3d347467925e511a99928179df55d0154589 Mon Sep 17 00:00:00 2001 From: Payas Relekar Date: Mon, 15 Jul 2024 00:44:11 +0530 Subject: [PATCH] nixos/goatcounter: init --- .../manual/release-notes/rl-2411.section.md | 2 + nixos/modules/module-list.nix | 1 + .../modules/services/web-apps/goatcounter.nix | 80 +++++++++++++++++++ nixos/tests/all-tests.nix | 1 + nixos/tests/goatcounter.nix | 32 ++++++++ pkgs/by-name/go/goatcounter/package.nix | 23 +++--- 6 files changed, 130 insertions(+), 9 deletions(-) create mode 100644 nixos/modules/services/web-apps/goatcounter.nix create mode 100644 nixos/tests/goatcounter.nix diff --git a/nixos/doc/manual/release-notes/rl-2411.section.md b/nixos/doc/manual/release-notes/rl-2411.section.md index 1c3865b5f834..46cdb96880e1 100644 --- a/nixos/doc/manual/release-notes/rl-2411.section.md +++ b/nixos/doc/manual/release-notes/rl-2411.section.md @@ -20,6 +20,8 @@ - [FlareSolverr](https://github.com/FlareSolverr/FlareSolverr), proxy server to bypass Cloudflare protection. Available as [services.flaresolverr](#opt-services.flaresolverr.enable) service. +- [Goatcounter](https://www.goatcounter.com/), Easy web analytics. No tracking of personal data. Available as [services.goatcounter](options.html#opt-services.goatcocunter.enable). + - [Open-WebUI](https://github.com/open-webui/open-webui), a user-friendly WebUI for LLMs. Available as [services.open-webui](#opt-services.open-webui.enable) service. diff --git a/nixos/modules/module-list.nix b/nixos/modules/module-list.nix index 30a11980b7b6..96739c80862d 100644 --- a/nixos/modules/module-list.nix +++ b/nixos/modules/module-list.nix @@ -1397,6 +1397,7 @@ ./services/web-apps/gotosocial.nix ./services/web-apps/grocy.nix ./services/web-apps/pixelfed.nix + ./services/web-apps/goatcounter.nix ./services/web-apps/guacamole-client.nix ./services/web-apps/guacamole-server.nix ./services/web-apps/healthchecks.nix diff --git a/nixos/modules/services/web-apps/goatcounter.nix b/nixos/modules/services/web-apps/goatcounter.nix new file mode 100644 index 000000000000..318915fb88ff --- /dev/null +++ b/nixos/modules/services/web-apps/goatcounter.nix @@ -0,0 +1,80 @@ +{ + config, + lib, + pkgs, + ... +}: + +let + inherit (lib) types; + cfg = config.services.goatcounter; + stateDir = "goatcounter"; +in + +{ + options = { + services.goatcounter = { + enable = lib.mkEnableOption "goatcounter"; + + package = lib.mkPackageOption pkgs "goatcounter" { }; + + address = lib.mkOption { + type = types.str; + default = "127.0.0.1"; + description = "Web interface address."; + }; + + port = lib.mkOption { + type = types.port; + default = 8081; + description = "Web interface port."; + }; + + proxy = lib.mkOption { + type = types.bool; + default = false; + description = '' + Whether Goatcounter service is running behind a reverse proxy. Will listen for HTTPS if `false`. + Refer to [documentation](https://github.com/arp242/goatcounter?tab=readme-ov-file#running) for more details. + ''; + }; + + extraArgs = lib.mkOption { + type = with types; listOf str; + default = [ ]; + description = '' + List of extra arguments to be passed to goatcounter cli. + See {command}`goatcounter help serve` for more information. + ''; + }; + }; + }; + + config = lib.mkIf cfg.enable { + systemd.services.goatcounter = { + description = "Easy web analytics. No tracking of personal data."; + wantedBy = [ "multi-user.target" ]; + serviceConfig = { + ExecStart = lib.escapeShellArgs ( + [ + (lib.getExe cfg.package) + "serve" + "-listen" + "${cfg.address}:${toString cfg.port}" + ] + ++ lib.optionals cfg.proxy [ + "-tls" + "proxy" + ] + ++ cfg.extraArgs + ); + DynamicUser = true; + StateDirectory = stateDir; + WorkingDirectory = "%S/${stateDir}"; + Restart = "always"; + }; + }; + }; + + meta.maintainers = with lib.maintainers; [ bhankas ]; +} diff --git a/nixos/tests/all-tests.nix b/nixos/tests/all-tests.nix index b0bea263215b..cc31fbade123 100644 --- a/nixos/tests/all-tests.nix +++ b/nixos/tests/all-tests.nix @@ -375,6 +375,7 @@ in { gnome-xorg = handleTest ./gnome-xorg.nix {}; gns3-server = handleTest ./gns3-server.nix {}; gnupg = handleTest ./gnupg.nix {}; + goatcounter = handleTest ./goatcounter.nix {}; go-neb = handleTest ./go-neb.nix {}; gobgpd = handleTest ./gobgpd.nix {}; gocd-agent = handleTest ./gocd-agent.nix {}; diff --git a/nixos/tests/goatcounter.nix b/nixos/tests/goatcounter.nix new file mode 100644 index 000000000000..ee3b373383e2 --- /dev/null +++ b/nixos/tests/goatcounter.nix @@ -0,0 +1,32 @@ +import ./make-test-python.nix ( + { lib, pkgs, ... }: + + { + name = "goatcounter"; + + meta.maintainers = with lib.maintainers; [ bhankas ]; + + nodes.machine = + { config, ... }: + { + virtualisation.memorySize = 2048; + + services.goatcounter = { + enable = true; + proxy = true; + }; + }; + + testScript = '' + start_all() + machine.wait_for_unit("goatcounter.service") + # wait for goatcounter to fully come up + + with subtest("goatcounter service starts"): + machine.wait_until_succeeds( + "curl -sSfL http://localhost:8081/ > /dev/null", + timeout=30 + ) + ''; + } +) diff --git a/pkgs/by-name/go/goatcounter/package.nix b/pkgs/by-name/go/goatcounter/package.nix index 208581708837..83e05473ae60 100644 --- a/pkgs/by-name/go/goatcounter/package.nix +++ b/pkgs/by-name/go/goatcounter/package.nix @@ -1,8 +1,10 @@ -{ lib -, buildGoModule -, fetchFromGitHub -, testers -, goatcounter +{ + lib, + buildGoModule, + fetchFromGitHub, + testers, + goatcounter, + nixosTests, }: buildGoModule rec { @@ -31,10 +33,13 @@ buildGoModule rec { "-X zgo.at/goatcounter/v2.Version=${src.rev}" ]; - passthru.tests.version = testers.testVersion { - package = goatcounter; - command = "goatcounter version"; - version = "v${version}"; + passthru.tests = { + moduleTest = nixosTests.goatcounter; + version = testers.testVersion { + package = goatcounter; + command = "goatcounter version"; + version = "v${version}"; + }; }; meta = {