HACMRPLPDJIRWINVHRKYM5N7RLT5273WQQYRJLA3URNXJBJ4FSVAC 2BGCCK64WA7SVJ2KLNVT6OHSW7MGRPUF4ZVRTSQWJ55RC652D2BQC GO2MEAYW3PLH5HTW2VVI7AT3FDOHFFJRC7TCEHE27D4XMA4LXCIQC DQKCPBYIW34BPR3BNMCBID7HL3MHGKR47LGBJ6B7VSIKMCOV3EFAC O5U2RS6S4RUGVSOZPTJGD6HJOOO5VM7SPZUDUHU4G3BHZOTJH47QC BDDKGGL7QNTFAX3ARMV4VK2XQCLOVJNFLXFCZB7IFCGEATCMVBIAC RTBMBSBABSGTRICJ4AWBKWO3JJHBRKV6FGOMYPDD7X6SS6X35ZIQC VWR3XCGQZU4GXXBVWEFGPQXHUOXWPOZBNQHL67GAX3QGP7LNKG3AC 4GGQX4QFLTB4FMCYJOIBPOAMZVC5FXUNEZGF3A7E7GUNNVM5WQHAC SUJ7TKWXHFZCEEQFGLRT4PZ4TYAXFLNDZVE57SN2AHXTLEPX7WXAC S6ZN4FXJQKJM6BQKDSAKZTAILRKYNFFEUDJKFQTDBWENQTJCPJZAC 7AJ4T2F665FWRW47PCIZYIGQFSBEPRXBLUMBZO6FQBMOMKZ5HYXQC UIRZ4SMZZC2QZ7D4QFQJZZKFA3QOA5SOQBOAGOG3RLXTCSLS3QJAC 3C3SXK3RCUTNAF5BQCPP5FXJPCHJMP4BSKJLPI55XK56GBKSB2AAC HYBPXVO2BD6SK7IVFLS6YZFEN2HMSZDALXIUCJGSKVB3LYMLCWJAC 4R56PJDQPYZ5FLFAMZSNY7UI5TKF6N5YCHCJU3ZBV5FHB46GYM5QC XPKDQGFUFF4BT32ODPEJRZGBEOGZVDRAEN2EOWU7DBIN2QQX6KUAC { config, lib, pkgs, ... }: letinherit (lib) enabled mkForce mkOverride mkValue flip map;in {options.services.postgresql.ensure = mkValue [];config.services.postgresql = enabled {package = pkgs.postgresql_17;enableJIT = true;enableTCPIP = true;settings.listen_addresses = mkForce "::";authentication = mkOverride 10 ''# DATABASE USER AUTHENTICATIONlocal all all peer# DATABASE USER ADDRESS AUTHENTICATIONhost all all ::/0 md5'';ensure = [ "postgres" "root" ];initdbArgs = [ "--locale=C" "--encoding=UTF8" ];ensureDatabases = config.services.postgresql.ensure;ensureUsers = flip map config.services.postgresql.ensure (name: {inherit name;ensureDBOwnership = true;ensureClauses = {login = true;superuser = name == "postgres" || name == "root";};});};config.environment.systemPackages = [config.services.postgresql.package];}
extraConfig = lib.optionalString (config.services ? plausible)(config.services.plausible.extraNginxConfigFor domain);
extraConfig = ''proxy_set_header Accept-Encoding "";sub_filter "</head>" '<script data-goatcounter="https://analytics.${domain}/count" async src="https://analytics.${domain}/count.js"></script></head>';sub_filter_last_modified on;sub_filter_once on;'';
extraConfig = lib.optionalString (config.services ? plausible)(config.services.plausible.extraNginxConfigFor fqdn);
extraConfig = ''proxy_set_header Accept-Encoding "";sub_filter "</head>" '<script data-goatcounter="https://analytics.${domain}/count" async src="https://analytics.${domain}/count.js"></script></head>';sub_filter_last_modified on;sub_filter_once on;'';
extraConfig = lib.optionalString (config.services ? plausible)(config.services.plausible.extraNginxConfigFor fqdn);
extraConfig = ''proxy_set_header Accept-Encoding "";sub_filter "</head>" '<script data-goatcounter="https://analytics.${domain}/count" async src="https://analytics.${domain}/count.js"></script></head>';sub_filter_last_modified on;sub_filter_once on;'';
sub_filter "</head>" '<script defer data-domain="${domain}" data-api="https://analytics.plumj.am/api/event" src="https://analytics.plumj.am/js/script.file-downloads.hash.outbound-links.js"></script><script>window.plausible = window.plausible || function() { (window.plausible.q = window.plausible.q || []).push(arguments) }</script></head>';
sub_filter "</head>" '<script data-goatcounter="https://analytics.plumj.am/count" async src="https://analytics.plumj.am/count.js"></script></head>';
add_header Content-Security-Policy "script-src 'self' 'unsafe-inline' 'unsafe-eval' ${domain} *.${domain} cdn.jsdelivr.net unpkg.com *.posthog.com *.sanity.io *.googletagmanager.com *.google-analytics.com analytics.plumj.am; object-src 'self' ${domain} *.${domain}; base-uri 'self'; frame-ancestors 'self' dr-radka.sanity.studio *.sanity.io; form-action 'self' ${domain} *.${domain}; font-src 'self' ${domain} *.${domain} cdn.jsdelivr.net; connect-src 'self' ${domain} *.${domain} unpkg.com *.posthog.com *.sanity.io *.googletagmanager.com *.google-analytics.com plausible.io analytics.plumj.am; img-src 'self' ${domain} *.${domain} unpkg.com *.tile.openstreetmap.org *.sanity.io cdn.sanity.io www.googletagmanager.com data:;" always;
add_header Content-Security-Policy "script-src 'self' 'unsafe-inline' 'unsafe-eval' ${domain} *.${domain} cdn.jsdelivr.net unpkg.com *.posthog.com *.sanity.io *.googletagmanager.com *.google-analytics.com analytics.plumj.am; object-src 'self' ${domain} *.${domain}; base-uri 'self'; frame-ancestors 'self' dr-radka.sanity.studio *.sanity.io; form-action 'self' ${domain} *.${domain}; font-src 'self' ${domain} *.${domain} cdn.jsdelivr.net; connect-src 'self' ${domain} *.${domain} unpkg.com *.posthog.com *.sanity.io *.googletagmanager.com *.google-analytics.com analytics.plumj.am; img-src 'self' ${domain} *.${domain} unpkg.com *.tile.openstreetmap.org *.sanity.io cdn.sanity.io www.googletagmanager.com data:;" always;
{ self, config, lib, ... }: letinherit (config.networking) domain;inherit (lib) enabled mkOption;fqdn = "analytics.${domain}";port = 8007;in {imports = [ (self + /modules/postgresql.nix) ];options.services.plausible.extraNginxConfigFor = mkOption {type = lib.types.functionTo lib.types.str;default = domain: ''proxy_set_header Accept-Encoding "";sub_filter "</head>" '<script defer data-domain="${domain}" data-api="https://${fqdn}/api/event" src="https://${fqdn}/js/script.file-downloads.hash.outbound-links.js"></script><script>window.plausible = window.plausible || function() { (window.plausible.q = window.plausible.q || []).push(arguments) }</script></head>';sub_filter_last_modified on;sub_filter_once on;'';};config = {services.postgresql.ensure = [ "plausible" ];age.secrets.plausibleKey = {file = ./key.age;owner = "plausible";};services.plausible = enabled {database = {clickhouse.setup = true;postgres.setup = true;};server = {inherit port;disableRegistration = true;secretKeybaseFile = config.age.secrets.plausibleKey.path;baseUrl = "https://${fqdn}";listenAddress = "::1";};};services.nginx.virtualHosts.${fqdn} = lib.merge config.services.nginx.sslTemplate {extraConfig = config.services.plausible.extraNginxConfigFor fqdn;locations."/" = {proxyPass = "http://[::1]:${toString port}";proxyWebsockets = true;};};};}
extraConfig = lib.optionalString (config.services ? plausible)(config.services.plausible.extraNginxConfigFor fqdn);
extraConfig = ''proxy_set_header Accept-Encoding "";sub_filter "</head>" '<script data-goatcounter="https://analytics.${domain}/count" async src="https://analytics.${domain}/count.js"></script></head>';sub_filter_last_modified on;sub_filter_once on;'';
{ self, config, lib, ... }: letinherit (config.networking) domain;inherit (lib) enabled;fqdn = "analytics.${domain}";port = 8007;in {config = {services.goatcounter = enabled {inherit port;proxy = true;address = "127.0.0.1";};services.nginx.virtualHosts.${fqdn} = lib.merge config.services.nginx.sslTemplate {locations."/" = {proxyPass = "http://127.0.0.1:${toString port}";proxyWebsockets = true;extraConfig = ''proxy_hide_header X-Content-Type-Options;'';};};};}