nixos/services.awstats: remove with lib;

This commit is contained in:
Felix Buehler 2024-08-27 20:43:28 +02:00
parent d9a644373a
commit e2a2735d4d

View File

@ -1,29 +1,26 @@
{ config, lib, pkgs, ... }: { config, lib, pkgs, ... }:
with lib;
let let
cfg = config.services.awstats; cfg = config.services.awstats;
package = pkgs.awstats; package = pkgs.awstats;
configOpts = {name, config, ...}: { configOpts = {name, config, ...}: {
options = { options = {
type = mkOption{ type = lib.mkOption{
type = types.enum [ "mail" "web" ]; type = lib.types.enum [ "mail" "web" ];
default = "web"; default = "web";
example = "mail"; example = "mail";
description = '' description = ''
The type of log being collected. The type of log being collected.
''; '';
}; };
domain = mkOption { domain = lib.mkOption {
type = types.str; type = lib.types.str;
default = name; default = name;
description = "The domain name to collect stats for."; description = "The domain name to collect stats for.";
example = "example.com"; example = "example.com";
}; };
logFile = mkOption { logFile = lib.mkOption {
type = types.str; type = lib.types.str;
example = "/var/log/nginx/access.log"; example = "/var/log/nginx/access.log";
description = '' description = ''
The log file to be scanned. The log file to be scanned.
@ -35,8 +32,8 @@ let
''; '';
}; };
logFormat = mkOption { logFormat = lib.mkOption {
type = types.str; type = lib.types.str;
default = "1"; default = "1";
description = '' description = ''
The log format being used. The log format being used.
@ -48,8 +45,8 @@ let
''; '';
}; };
hostAliases = mkOption { hostAliases = lib.mkOption {
type = types.listOf types.str; type = lib.types.listOf lib.types.str;
default = []; default = [];
example = [ "www.example.org" ]; example = [ "www.example.org" ];
description = '' description = ''
@ -57,10 +54,10 @@ let
''; '';
}; };
extraConfig = mkOption { extraConfig = lib.mkOption {
type = types.attrsOf types.str; type = lib.types.attrsOf lib.types.str;
default = {}; default = {};
example = literalExpression '' example = lib.literalExpression ''
{ {
"ValidHTTPCodes" = "404"; "ValidHTTPCodes" = "404";
} }
@ -69,44 +66,44 @@ let
}; };
webService = { webService = {
enable = mkEnableOption "awstats web service"; enable = lib.mkEnableOption "awstats web service";
hostname = mkOption { hostname = lib.mkOption {
type = types.str; type = lib.types.str;
default = config.domain; default = config.domain;
description = "The hostname the web service appears under."; description = "The hostname the web service appears under.";
}; };
urlPrefix = mkOption { urlPrefix = lib.mkOption {
type = types.str; type = lib.types.str;
default = "/awstats"; default = "/awstats";
description = "The URL prefix under which the awstats pages appear."; description = "The URL prefix under which the awstats pages appear.";
}; };
}; };
}; };
}; };
webServices = filterAttrs (name: value: value.webService.enable) cfg.configs; webServices = lib.filterAttrs (name: value: value.webService.enable) cfg.configs;
in in
{ {
imports = [ imports = [
(mkRemovedOptionModule [ "services" "awstats" "service" "enable" ] "Please enable per domain with `services.awstats.configs.<name>.webService.enable`") (lib.mkRemovedOptionModule [ "services" "awstats" "service" "enable" ] "Please enable per domain with `services.awstats.configs.<name>.webService.enable`")
(mkRemovedOptionModule [ "services" "awstats" "service" "urlPrefix" ] "Please set per domain with `services.awstats.configs.<name>.webService.urlPrefix`") (lib.mkRemovedOptionModule [ "services" "awstats" "service" "urlPrefix" ] "Please set per domain with `services.awstats.configs.<name>.webService.urlPrefix`")
(mkRenamedOptionModule [ "services" "awstats" "vardir" ] [ "services" "awstats" "dataDir" ]) (lib.mkRenamedOptionModule [ "services" "awstats" "vardir" ] [ "services" "awstats" "dataDir" ])
]; ];
options.services.awstats = { options.services.awstats = {
enable = mkEnableOption "awstats, a real-time logfile analyzer"; enable = lib.mkEnableOption "awstats, a real-time logfile analyzer";
dataDir = mkOption { dataDir = lib.mkOption {
type = types.path; type = lib.types.path;
default = "/var/lib/awstats"; default = "/var/lib/awstats";
description = "The directory where awstats data will be stored."; description = "The directory where awstats data will be stored.";
}; };
configs = mkOption { configs = lib.mkOption {
type = types.attrsOf (types.submodule configOpts); type = lib.types.attrsOf (lib.types.submodule configOpts);
default = {}; default = {};
example = literalExpression '' example = lib.literalExpression ''
{ {
"mysite" = { "mysite" = {
domain = "example.com"; domain = "example.com";
@ -117,8 +114,8 @@ in
description = "Attribute set of domains to collect stats for."; description = "Attribute set of domains to collect stats for.";
}; };
updateAt = mkOption { updateAt = lib.mkOption {
type = types.nullOr types.str; type = lib.types.nullOr lib.types.str;
default = null; default = null;
example = "hourly"; example = "hourly";
description = '' description = ''
@ -129,18 +126,18 @@ in
}; };
config = mkIf cfg.enable { config = lib.mkIf cfg.enable {
environment.systemPackages = [ package.bin ]; environment.systemPackages = [ package.bin ];
environment.etc = mapAttrs' (name: opts: environment.etc = lib.mapAttrs' (name: opts:
nameValuePair "awstats/awstats.${name}.conf" { lib.nameValuePair "awstats/awstats.${name}.conf" {
source = pkgs.runCommand "awstats.${name}.conf" source = pkgs.runCommand "awstats.${name}.conf"
{ preferLocalBuild = true; } { preferLocalBuild = true; }
('' (''
sed \ sed \
'' ''
# set up mail stats # set up mail stats
+ optionalString (opts.type == "mail") + lib.optionalString (opts.type == "mail")
'' ''
-e 's|^\(LogType\)=.*$|\1=M|' \ -e 's|^\(LogType\)=.*$|\1=M|' \
-e 's|^\(LevelForBrowsersDetection\)=.*$|\1=0|' \ -e 's|^\(LevelForBrowsersDetection\)=.*$|\1=0|' \
@ -187,7 +184,7 @@ in
'' ''
+ +
# extra config # extra config
concatStringsSep "\n" (mapAttrsToList (n: v: '' lib.concatStringsSep "\n" (lib.mapAttrsToList (n: v: ''
-e 's|^\(${n}\)=.*$|\1="${v}"|' \ -e 's|^\(${n}\)=.*$|\1="${v}"|' \
'') opts.extraConfig) '') opts.extraConfig)
+ +
@ -199,11 +196,11 @@ in
# create data directory with the correct permissions # create data directory with the correct permissions
systemd.tmpfiles.rules = systemd.tmpfiles.rules =
[ "d '${cfg.dataDir}' 755 root root - -" ] ++ [ "d '${cfg.dataDir}' 755 root root - -" ] ++
mapAttrsToList (name: opts: "d '${cfg.dataDir}/${name}' 755 root root - -") cfg.configs ++ lib.mapAttrsToList (name: opts: "d '${cfg.dataDir}/${name}' 755 root root - -") cfg.configs ++
[ "Z '${cfg.dataDir}' 755 root root - -" ]; [ "Z '${cfg.dataDir}' 755 root root - -" ];
# nginx options # nginx options
services.nginx.virtualHosts = mapAttrs'(name: opts: { services.nginx.virtualHosts = lib.mapAttrs'(name: opts: {
name = opts.webService.hostname; name = opts.webService.hostname;
value = { value = {
locations = { locations = {
@ -224,10 +221,10 @@ in
}) webServices; }) webServices;
# update awstats # update awstats
systemd.services = mkIf (cfg.updateAt != null) (mapAttrs' (name: opts: systemd.services = lib.mkIf (cfg.updateAt != null) (lib.mapAttrs' (name: opts:
nameValuePair "awstats-${name}-update" { lib.nameValuePair "awstats-${name}-update" {
description = "update awstats for ${name}"; description = "update awstats for ${name}";
script = optionalString (opts.type == "mail") script = lib.optionalString (opts.type == "mail")
'' ''
if [[ -f "${cfg.dataDir}/${name}-cursor" ]]; then if [[ -f "${cfg.dataDir}/${name}-cursor" ]]; then
CURSOR="$(cat "${cfg.dataDir}/${name}-cursor" | tr -d '\n')" CURSOR="$(cat "${cfg.dataDir}/${name}-cursor" | tr -d '\n')"