1 { lib, pkgs, config, mylibs, ... }:
3 cfg = config.services.myWebsites.commons.stats;
6 services.myWebsites.commons.stats = {
7 enable = lib.mkEnableOption "enable statistics";
9 type = lib.types.listOf (lib.types.submodule {
11 conf = lib.mkOption { type = lib.types.path; };
12 name = lib.mkOption { type = lib.types.string; };
16 description = "Sites to generate stats";
21 config = lib.mkIf cfg.enable {
22 users.users.root.packages = [
29 stats = domain: conf: let
30 d = pkgs.writeScriptBin "stats-${domain}" ''
31 #!${pkgs.stdenv.shell}
34 date_regex=$(LC_ALL=C date -d yesterday +'%d\/%b\/%Y')
36 trap "rm -f $TMPFILE" EXIT
38 cat /var/log/httpd/access_log-${domain} | sed -n "/\\[$date_regex/ p" > $TMPFILE
39 for i in /var/log/httpd/access_log-${domain}*.gz; do
40 zcat "$i" | sed -n "/\\[$date_regex/ p" >> $TMPFILE
42 ${pkgs.goaccess}/bin/goaccess $TMPFILE --no-progress -o /var/lib/goaccess/${domain}/index.html -p ${conf}
44 in "${d}/bin/stats-${domain}";
45 allStats = sites: pkgs.writeScript "stats" ''
46 #!${pkgs.stdenv.shell}
48 ${builtins.concatStringsSep "\n" (map (v: stats v.name v.conf) sites)}
52 "5 0 * * * root ${allStats cfg.sites}"
56 system.activationScripts.goaccess = ''
57 mkdir -p /var/lib/goaccess
59 builtins.concatStringsSep "\n" (map (v: "mkdir -p /var/lib/goaccess/${v.name}") cfg.sites);