]> git.immae.eu Git - perso/Immae/Config/Nix.git/blob - nixops/modules/websites/commons/stats.nix
Use password store to store environment
[perso/Immae/Config/Nix.git] / nixops / modules / websites / commons / stats.nix
1 { lib, pkgs, config, mylibs, ... }:
2 let
3 cfg = config.services.myWebsites.commons.stats;
4 in {
5 options = {
6 services.myWebsites.commons.stats = {
7 enable = lib.mkEnableOption "enable statistics";
8 sites = lib.mkOption {
9 type = lib.types.listOf (lib.types.submodule {
10 options = {
11 conf = lib.mkOption { type = lib.types.path; };
12 name = lib.mkOption { type = lib.types.string; };
13 };
14 });
15 default = [];
16 description = "Sites to generate stats";
17 };
18 };
19 };
20
21 config = lib.mkIf cfg.enable {
22 environment.systemPackages = [
23 pkgs.goaccess
24 ];
25
26 nixpkgs.config.packageOverrides = oldpkgs: rec {
27 goaccess = oldpkgs.goaccess.overrideAttrs(old: rec {
28 name = "goaccess-${version}";
29 version = "1.3";
30 src = pkgs.fetchurl {
31 url = "https://tar.goaccess.io/${name}.tar.gz";
32 sha256 = "16vv3pj7pbraq173wlxa89jjsd279004j4kgzlrsk1dz4if5qxwc";
33 };
34 configureFlags = old.configureFlags ++ [ "--enable-tcb=btree" ];
35 buildInputs = old.buildInputs ++ [ pkgs.tokyocabinet pkgs.bzip2 ];
36 });
37 };
38
39 services.cron = {
40 enable = true;
41 systemCronJobs = let
42 stats = domain: conf: let
43 d = pkgs.writeScriptBin "stats-${domain}" ''
44 #!${pkgs.stdenv.shell}
45 set -e
46 shopt -s nullglob
47 date_regex=$(LC_ALL=C date -d yesterday +'%d\/%b\/%Y')
48 TMPFILE=$(mktemp)
49 trap "rm -f $TMPFILE" EXIT
50
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
54 done
55 goaccess $TMPFILE --no-progress -o /var/lib/goaccess/${domain}/index.html -p ${conf}
56 '';
57 in "${d}/bin/stats-${domain}";
58 in
59 pkgs.lib.lists.imap0 (i: v: "${toString (i+5)} 0 * * * root ${stats v.name v.conf}") cfg.sites;
60 };
61
62 system.activationScripts.goaccess = ''
63 mkdir -p /var/lib/goaccess
64 '' +
65 builtins.concatStringsSep "\n" (map (v: "mkdir -p /var/lib/goaccess/${v.name}") cfg.sites);
66 };
67 }