nixos/zabbixWeb: format

This commit is contained in:
2024-06-08 15:39:06 +02:00
parent c27fdd8ebe
commit cd6038fbba

View File

@@ -1,9 +1,31 @@
{ config, lib, options, pkgs, ... }:
{
config,
lib,
options,
pkgs,
...
}:
let
inherit (lib) mkDefault mkEnableOption mkPackageOption mkRenamedOptionModule mkForce mkIf mkMerge mkOption types;
inherit (lib) literalExpression mapAttrs optionalString optionals versionAtLeast;
inherit (lib)
mkDefault
mkEnableOption
mkPackageOption
mkRenamedOptionModule
mkForce
mkIf
mkMerge
mkOption
types
;
inherit (lib)
literalExpression
mapAttrs
optionalString
optionals
versionAtLeast
;
cfg = config.services.zabbixWeb;
opt = options.services.zabbixWeb;
@@ -17,13 +39,25 @@ let
<?php
// Zabbix GUI configuration file.
global $DB;
$DB['TYPE'] = '${ { mysql = "MYSQL"; pgsql = "POSTGRESQL"; oracle = "ORACLE"; }.${cfg.database.type} }';
$DB['TYPE'] = '${
{
mysql = "MYSQL";
pgsql = "POSTGRESQL";
oracle = "ORACLE";
}
.${cfg.database.type}
}';
$DB['SERVER'] = '${cfg.database.host}';
$DB['PORT'] = '${toString cfg.database.port}';
$DB['DATABASE'] = '${cfg.database.name}';
$DB['USER'] = '${cfg.database.user}';
# NOTE: file_get_contents adds newline at the end of returned string
$DB['PASSWORD'] = ${if cfg.database.passwordFile != null then "trim(file_get_contents('${cfg.database.passwordFile}'), \"\\r\\n\")" else "''"};
$DB['PASSWORD'] = ${
if cfg.database.passwordFile != null then
"trim(file_get_contents('${cfg.database.passwordFile}'), \"\\r\\n\")"
else
"''"
};
// Schema name. Used for IBM DB2 and PostgreSQL.
$DB['SCHEMA'] = ''';
$ZBX_SERVER = '${cfg.server.address}';
@@ -33,12 +67,21 @@ let
${cfg.extraConfig}
'';
in
{
imports = [
(mkRenamedOptionModule
[ "services" "zabbixWeb" "virtualHost" ] [ "services" "zabbixWeb" "HttpdVirtualHost" ])
[
"services"
"zabbixWeb"
"virtualHost"
]
[
"services"
"zabbixWeb"
"HttpdVirtualHost"
]
)
];
# interface
@@ -46,7 +89,10 @@ in
zabbixWeb = {
enable = mkEnableOption "the Zabbix web interface";
package = mkPackageOption pkgs [ "zabbix" "web" ] { };
package = mkPackageOption pkgs [
"zabbix"
"web"
] { };
server = {
port = mkOption {
@@ -64,7 +110,11 @@ in
database = {
type = mkOption {
type = types.enum [ "mysql" "pgsql" "oracle" ];
type = types.enum [
"mysql"
"pgsql"
"oracle"
];
example = "mysql";
default = "pgsql";
description = "Database engine to use.";
@@ -79,9 +129,12 @@ in
port = mkOption {
type = types.port;
default =
if cfg.database.type == "mysql" then config.services.mysql.port
else if cfg.database.type == "pgsql" then config.services.postgresql.settings.port
else 1521;
if cfg.database.type == "mysql" then
config.services.mysql.port
else if cfg.database.type == "pgsql" then
config.services.postgresql.settings.port
else
1521;
defaultText = literalExpression ''
if config.${opt.database.type} == "mysql" then config.${options.services.mysql.port}
else if config.${opt.database.type} == "pgsql" then config.services.postgresql.settings.port
@@ -170,7 +223,13 @@ in
};
poolConfig = mkOption {
type = with types; attrsOf (oneOf [ str int bool ]);
type =
with types;
attrsOf (oneOf [
str
int
bool
]);
default = {
"pm" = "dynamic";
"pm.max_children" = 32;
@@ -191,7 +250,6 @@ in
Additional configuration to be copied verbatim into {file}`zabbix.conf.php`.
'';
};
};
};
@@ -199,21 +257,30 @@ in
config = mkIf cfg.enable {
services.zabbixWeb.extraConfig = optionalString ((versionAtLeast config.system.stateVersion "20.09") && (versionAtLeast cfg.package.version "5.0.0")) ''
services.zabbixWeb.extraConfig =
optionalString
(
(versionAtLeast config.system.stateVersion "20.09") && (versionAtLeast cfg.package.version "5.0.0")
)
''
$DB['DOUBLE_IEEE754'] = 'true';
'';
systemd.tmpfiles.rules = [
"d '${stateDir}' 0750 ${user} ${group} - -" ] ++ optionals (cfg.frontend == "httpd") [
systemd.tmpfiles.rules =
[ "d '${stateDir}' 0750 ${user} ${group} - -" ]
++ optionals (cfg.frontend == "httpd") [
"d '${stateDir}/session' 0750 ${user} ${config.services.httpd.group} - -"
] ++ optionals (cfg.frontend == "nginx") [
]
++ optionals (cfg.frontend == "nginx") [
"d '${stateDir}/session' 0750 ${user} ${config.services.nginx.group} - -"
];
services.phpfpm.pools.zabbix = {
inherit user;
group = if cfg.frontend == "httpd" then config.services.httpd.group else config.services.nginx.group;
phpOptions = ''
group =
if cfg.frontend == "httpd" then config.services.httpd.group else config.services.nginx.group;
phpOptions =
''
# https://www.zabbix.com/documentation/current/manual/installation/install
memory_limit = 128M
post_max_size = 16M
@@ -225,15 +292,19 @@ in
always_populate_raw_post_data = -1
# https://bbs.archlinux.org/viewtopic.php?pid=1745214#p1745214
session.save_path = ${stateDir}/session
'' + optionalString (config.time.timeZone != null) ''
''
+ optionalString (config.time.timeZone != null) ''
date.timezone = "${config.time.timeZone}"
'' + optionalString (cfg.database.type == "oracle") ''
''
+ optionalString (cfg.database.type == "oracle") ''
extension=${pkgs.phpPackages.oci8}/lib/php/extensions/oci8.so
'';
phpEnv.ZABBIX_CONFIG = "${zabbixConfig}";
settings = {
"listen.owner" = if cfg.frontend == "httpd" then config.services.httpd.user else config.services.nginx.user;
"listen.group" = if cfg.frontend == "httpd" then config.services.httpd.group else config.services.nginx.group;
"listen.owner" =
if cfg.frontend == "httpd" then config.services.httpd.user else config.services.nginx.user;
"listen.group" =
if cfg.frontend == "httpd" then config.services.httpd.group else config.services.nginx.group;
} // cfg.poolConfig;
};
@@ -241,7 +312,9 @@ in
enable = true;
adminAddr = mkDefault cfg.HttpdVirtualHost.adminAddr;
extraModules = [ "proxy_fcgi" ];
virtualHosts.${cfg.hostname} = mkMerge [ cfg.HttpdVirtualHost {
virtualHosts.${cfg.hostname} = mkMerge [
cfg.HttpdVirtualHost
{
documentRoot = mkForce "${cfg.package}/share/zabbix";
extraConfig = ''
<Directory "${cfg.package}/share/zabbix">
@@ -255,7 +328,8 @@ in
DirectoryIndex index.php
</Directory>
'';
} ];
}
];
};
services.nginx = mkIf (cfg.frontend == "nginx") {
@@ -282,9 +356,6 @@ in
inherit group;
};
users.groups.${group} = mapAttrs (name: mkDefault) {
gid = config.ids.gids.zabbix;
};
users.groups.${group} = mapAttrs (name: mkDefault) { gid = config.ids.gids.zabbix; };
};
}