]> git.immae.eu Git - perso/Immae/Config/Nix.git/blame - nixops/modules/websites/commons/stats.nix
Make pkg an argument of libs.nix
[perso/Immae/Config/Nix.git] / nixops / modules / websites / commons / stats.nix
CommitLineData
e4a945cd
IB
1{ lib, pkgs, config, mylibs, ... }:
2let
3 cfg = config.services.myWebsites.commons.stats;
4in {
5 options = {
6 services.myWebsites.commons.stats = {
7 enable = lib.mkEnableOption "enable statistics";
8 sites = lib.mkOption {
a5365ec3
IB
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 });
e4a945cd
IB
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}";
7c059af8
IB
58 allStats = sites: pkgs.writeScript "stats" ''
59 #!${pkgs.stdenv.shell}
60
61 ${builtins.concatStringsSep "\n" (map (v: stats v.name v.conf) sites)}
62 '';
e4a945cd 63 in
7c059af8
IB
64 [
65 "5 0 * * * root ${allStats cfg.sites}"
66 ];
e4a945cd
IB
67 };
68
e4a945cd
IB
69 system.activationScripts.goaccess = ''
70 mkdir -p /var/lib/goaccess
71 '' +
72 builtins.concatStringsSep "\n" (map (v: "mkdir -p /var/lib/goaccess/${v.name}") cfg.sites);
73 };
74}