]> git.immae.eu Git - perso/Immae/Config/Nix.git/blobdiff - modules/webapps/webstats/default.nix
Move webstats outside of nixops
[perso/Immae/Config/Nix.git] / modules / webapps / webstats / default.nix
similarity index 53%
rename from nixops/modules/websites/commons/stats.nix
rename to modules/webapps/webstats/default.nix
index 73595f1ff7e71838c5e0068ac68939cba239e04a..f4916bd011fc03ceb61e4b037a586937cb00e5cb 100644 (file)
@@ -1,27 +1,42 @@
 { lib, pkgs, config, mylibs, ... }:
 let
-    cfg = config.services.myWebsites.commons.stats;
+  name = "goaccess";
+  cfg = config.services.webstats;
 in {
-  options = {
-    services.myWebsites.commons.stats = {
-      enable = lib.mkEnableOption "enable statistics";
-      sites = lib.mkOption {
-        type = lib.types.listOf (lib.types.submodule {
-          options = {
-            conf = lib.mkOption {
-              type = lib.types.nullOr lib.types.path;
-              default = null;
-            };
-            name = lib.mkOption { type = lib.types.string; };
+  options.services.webstats = {
+    dataDir = lib.mkOption {
+      type = lib.types.path;
+      default = "/var/lib/${name}";
+      description = ''
+        The directory where Goaccess stores its data.
+      '';
+    };
+    sites = lib.mkOption {
+      type = lib.types.listOf (lib.types.submodule {
+        options = {
+          conf = lib.mkOption {
+            type = lib.types.nullOr lib.types.path;
+            default = null;
+            description = ''
+              use custom goaccess configuration file instead of the
+              default one.
+              '';
+          };
+          name = lib.mkOption {
+            type = lib.types.string;
+            description  = ''
+              Domain name. Corresponds to the Apache file name and the
+              folder name in which the state will be saved.
+              '';
           };
-        });
-        default = [];
-        description = "Sites to generate stats";
-      };
+        };
+      });
+      default = [];
+      description = "Sites to generate stats";
     };
   };
 
-  config = lib.mkIf cfg.enable {
+  config = lib.mkIf (builtins.length cfg.sites > 0) {
     users.users.root.packages = [
       pkgs.goaccess
     ];
@@ -32,7 +47,7 @@ in {
         stats = domain: conf: let
           config = if builtins.isNull conf
             then pkgs.runCommand "goaccess.conf" {
-                dbPath = "/var/lib/goaccess/${domain}";
+                dbPath = "${cfg.dataDir}/${domain}";
               } "substituteAll ${./goaccess.conf} $out"
             else conf;
           d = pkgs.writeScriptBin "stats-${domain}" ''
@@ -47,7 +62,7 @@ in {
             for i in /var/log/httpd/access_log-${domain}*.gz; do
               zcat "$i" | sed -n "/\\[$date_regex/ p" >> $TMPFILE
             done
-            ${pkgs.goaccess}/bin/goaccess $TMPFILE --no-progress -o /var/lib/goaccess/${domain}/index.html -p ${config}
+            ${pkgs.goaccess}/bin/goaccess $TMPFILE --no-progress -o ${cfg.dataDir}/${domain}/index.html -p ${config}
             '';
           in "${d}/bin/stats-${domain}";
         allStats = sites: pkgs.writeScript "stats" ''
@@ -64,6 +79,6 @@ in {
     system.activationScripts.goaccess = ''
       mkdir -p /var/lib/goaccess
     '' +
-    builtins.concatStringsSep "\n" (map (v: "mkdir -p /var/lib/goaccess/${v.name}") cfg.sites);
+    builtins.concatStringsSep "\n" (map (v: "mkdir -p ${cfg.dataDir}/${v.name}") cfg.sites);
   };
 }