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 environment.systemPackages = [
26 nixpkgs.config.packageOverrides = oldpkgs: rec {
27 goaccess = oldpkgs.goaccess.overrideAttrs(old: rec {
28 name = "goaccess-${version}";
31 url = "https://tar.goaccess.io/${name}.tar.gz";
32 sha256 = "16vv3pj7pbraq173wlxa89jjsd279004j4kgzlrsk1dz4if5qxwc";
34 configureFlags = old.configureFlags ++ [ "--enable-tcb=btree" ];
35 buildInputs = old.buildInputs ++ [ pkgs.tokyocabinet pkgs.bzip2 ];
42 stats = domain: conf: let
43 d = pkgs.writeScriptBin "stats-${domain}" ''
44 #!${pkgs.stdenv.shell}
47 date_regex=$(LC_ALL=C date -d yesterday +'%d\/%b\/%Y')
49 trap "rm -f $TMPFILE" EXIT
51 cat /var/log/httpd/access_log-${domain} | sed -n "/\\[$date_regex/ p" > $TMPFILE
52 for i in /var/log/httpd/access_log-${domain}*.gz; do
53 zcat "$i" | sed -n "/\\[$date_regex/ p" >> $TMPFILE
55 goaccess $TMPFILE --no-progress -o /var/lib/goaccess/${domain}/index.html -p ${conf}
57 in "${d}/bin/stats-${domain}";
59 pkgs.lib.lists.imap0 (i: v: "${toString (i+5)} 0 * * * root ${stats v.name v.conf}") cfg.sites;
62 system.activationScripts.goaccess = ''
63 mkdir -p /var/lib/goaccess
65 builtins.concatStringsSep "\n" (map (v: "mkdir -p /var/lib/goaccess/${v.name}") cfg.sites);