@@ -0,0 +1,123 @@
1+{ config, lib, pkgs, ... }:
2+
3+with lib;
4+
5+let
6+ cfg = config.services.awstats;
7+ package = pkgs.awstats;
8+in
9+
10+{
11+ options.services.awstats = {
12+ enable = mkOption {
13+ type = types.bool;
14+ default = cfg.service.enable;
15+ description = ''
16+ Enable the awstats program (but not service).
17+ Currently only simple httpd (Apache) configs are supported,
18+ and awstats plugins may not work correctly.
19+ '';
20+ };
21+ vardir = mkOption {
22+ type = types.path;
23+ default = "/var/lib/awstats";
24+ description = "The directory where variable awstats data will be stored.";
25+ };
26+
27+ extraConfig = mkOption {
28+ type = types.lines;
29+ default = "";
30+ description = "Extra configuration to be appendend to awstats.conf.";
31+ };
32+
33+ updateAt = mkOption {
34+ type = types.nullOr types.string;
35+ default = null;
36+ example = "hourly";
37+ description = ''
38+ Specification of the time at which awstats will get updated.
39+ (in the format described by <citerefentry>
40+ <refentrytitle>systemd.time</refentrytitle>
41+ <manvolnum>5</manvolnum></citerefentry>)
42+ '';
43+ };
44+
45+ service = {
46+ enable = mkOption {
47+ type = types.bool;
48+ default = false;
49+ description = ''Enable the awstats web service. This switches on httpd.'';
50+ };
51+ urlPrefix = mkOption {
52+ type = types.string;
53+ default = "/awstats";
54+ description = "The URL prefix under which the awstats service appears.";
55+ };
56+ };
57+ };
58+
59+
60+ config = mkIf cfg.enable {
61+ environment.systemPackages = [ package.bin ];
62+ /* TODO:
63+ - heed config.services.httpd.logPerVirtualHost, etc.
64+ - Can't AllowToUpdateStatsFromBrowser, as CGI scripts don't have permission
65+ to read the logs, and our httpd config apparently doesn't an option for that.
66+ */
67+ environment.etc."awstats/awstats.conf".source = pkgs.runCommand "awstats.conf"
68+ { preferLocalBuild = true; }
69+ ( let
70+ cfg-httpd = config.services.httpd;
71+ logFormat =
72+ if cfg-httpd.logFormat == "combined" then "1" else
73+ if cfg-httpd.logFormat == "common" then "4" else
74+ throw "awstats service doesn't support Apache log format `${cfg-httpd.logFormat}`";
75+ in
76+ ''
77+ sed \
78+ -e 's|^\(DirData\)=.*$|\1="${cfg.vardir}"|' \
79+ -e 's|^\(DirIcons\)=.*$|\1="icons"|' \
80+ -e 's|^\(CreateDirDataIfNotExists\)=.*$|\1=1|' \
81+ -e 's|^\(SiteDomain\)=.*$|\1="${cfg-httpd.hostName}"|' \
82+ -e 's|^\(LogFile\)=.*$|\1="${cfg-httpd.logDir}/access_log"|' \
83+ -e 's|^\(LogFormat\)=.*$|\1=${logFormat}|' \
84+ < '${package.out}/wwwroot/cgi-bin/awstats.model.conf' > "$out"
85+ echo '${cfg.extraConfig}' >> "$out"
86+ '');
87+
88+ # The httpd sub-service showing awstats.
89+ services.httpd.enable = mkIf cfg.service.enable true;
90+ services.httpd.extraSubservices = mkIf cfg.service.enable [ { function = { serverInfo, ... }: {
91+ extraConfig =
92+ ''
93+ Alias ${cfg.service.urlPrefix}/classes "${package.out}/wwwroot/classes/"
94+ Alias ${cfg.service.urlPrefix}/css "${package.out}/wwwroot/css/"
95+ Alias ${cfg.service.urlPrefix}/icons "${package.out}/wwwroot/icon/"
96+ ScriptAlias ${cfg.service.urlPrefix}/ "${package.out}/wwwroot/cgi-bin/"
97+
98+ <Directory "${package.out}/wwwroot">
99+ Options None
100+ AllowOverride None
101+ Order allow,deny
102+ Allow from all
103+ </Directory>
104+ '';
105+ startupScript =
106+ let
107+ inherit (serverInfo.serverConfig) user group;
108+ in pkgs.writeScript "awstats_startup.sh"
109+ ''
110+ mkdir -p '${cfg.vardir}'
111+ chown '${user}:${group}' '${cfg.vardir}'
112+ '';
113+ };}];
114+
115+ systemd.services.awstats-update = mkIf (cfg.updateAt != null) {
116+ description = "awstats log collector";
117+ script = "exec '${package.bin}/bin/awstats' -update -config=awstats.conf";
118+ startAt = cfg.updateAt;
119+ };
120+ };
121+
122+}
123+