mirror of
https://github.com/NixOS/nixpkgs.git
synced 2024-11-15 22:36:23 +01:00
48459567ae
Closes #216989 First of all, a bit of context: in PostgreSQL, newly created users don't have the CREATE privilege on the public schema of a database even with `ALL PRIVILEGES` granted via `ensurePermissions` which is how most of the DB users are currently set up "declaratively"[1]. This means e.g. a freshly deployed Nextcloud service will break early because Nextcloud itself cannot CREATE any tables in the public schema anymore. The other issue here is that `ensurePermissions` is a mere hack. It's effectively a mixture of SQL code (e.g. `DATABASE foo` is relying on how a value is substituted in a query. You'd have to parse a subset of SQL to actually know which object are permissions granted to for a user). After analyzing the existing modules I realized that in every case with a single exception[2] the UNIX system user is equal to the db user is equal to the db name and I don't see a compelling reason why people would change that in 99% of the cases. In fact, some modules would even break if you'd change that because the declarations of the system user & the db user are mixed up[3]. So I decided to go with something new which restricts the ways to use `ensure*` options rather than expanding those[4]. Effectively this means that * The DB user _must_ be equal to the DB name. * Permissions are granted via `ensureDBOwnerhip` for an attribute-set in `ensureUsers`. That way, the user is actually the owner and can perform `CREATE`. * For such a postgres user, a database must be declared in `ensureDatabases`. For anything else, a custom state management should be implemented. This can either be `initialScript`, doing it manual, outside of the module or by implementing proper state management for postgresql[5], but the current state of `ensure*` isn't even declarative, but a convergent tool which is what Nix actually claims to _not_ do. Regarding existing setups: there are effectively two options: * Leave everything as-is (assuming that system user == db user == db name): then the DB user will automatically become the DB owner and everything else stays the same. * Drop the `createDatabase = true;` declarations: nothing will change because a removal of `ensure*` statements is ignored, so it doesn't matter at all whether this option is kept after the first deploy (and later on you'd usually restore from backups anyways). The DB user isn't the owner of the DB then, but for an existing setup this is irrelevant because CREATE on the public schema isn't revoked from existing users (only not granted for new users). [1] not really declarative though because removals of these statements are simply ignored for instance: https://github.com/NixOS/nixpkgs/issues/206467 [2] `services.invidious`: I removed the `ensure*` part temporarily because it IMHO falls into the category "manage the state on your own" (see the commit message). See also https://github.com/NixOS/nixpkgs/pull/265857 [3] e.g. roundcube had `"DATABASE ${cfg.database.username}" = "ALL PRIVILEGES";` [4] As opposed to other changes that are considered a potential fix, but also add more things like collation for DBs or passwords that are _never_ touched again when changing those. [5] As suggested in e.g. https://github.com/NixOS/nixpkgs/issues/206467
323 lines
11 KiB
Nix
323 lines
11 KiB
Nix
{ config, lib, options, pkgs, ... }:
|
|
|
|
let
|
|
cfg = config.services.zabbixProxy;
|
|
opt = options.services.zabbixProxy;
|
|
pgsql = config.services.postgresql;
|
|
mysql = config.services.mysql;
|
|
|
|
inherit (lib) mkAfter mkDefault mkEnableOption mkIf mkMerge mkOption;
|
|
inherit (lib) attrValues concatMapStringsSep getName literalExpression optional optionalAttrs optionalString types;
|
|
inherit (lib.generators) toKeyValue;
|
|
|
|
user = "zabbix";
|
|
group = "zabbix";
|
|
runtimeDir = "/run/zabbix";
|
|
stateDir = "/var/lib/zabbix";
|
|
passwordFile = "${runtimeDir}/zabbix-dbpassword.conf";
|
|
|
|
moduleEnv = pkgs.symlinkJoin {
|
|
name = "zabbix-proxy-module-env";
|
|
paths = attrValues cfg.modules;
|
|
};
|
|
|
|
configFile = pkgs.writeText "zabbix_proxy.conf" (toKeyValue { listsAsDuplicateKeys = true; } cfg.settings);
|
|
|
|
mysqlLocal = cfg.database.createLocally && cfg.database.type == "mysql";
|
|
pgsqlLocal = cfg.database.createLocally && cfg.database.type == "pgsql";
|
|
|
|
in
|
|
|
|
{
|
|
imports = [
|
|
(lib.mkRemovedOptionModule [ "services" "zabbixProxy" "extraConfig" ] "Use services.zabbixProxy.settings instead.")
|
|
];
|
|
|
|
# interface
|
|
|
|
options = {
|
|
|
|
services.zabbixProxy = {
|
|
enable = mkEnableOption (lib.mdDoc "the Zabbix Proxy");
|
|
|
|
server = mkOption {
|
|
type = types.str;
|
|
description = lib.mdDoc ''
|
|
The IP address or hostname of the Zabbix server to connect to.
|
|
'';
|
|
};
|
|
|
|
package = mkOption {
|
|
type = types.package;
|
|
default =
|
|
if cfg.database.type == "mysql" then pkgs.zabbix.proxy-mysql
|
|
else if cfg.database.type == "pgsql" then pkgs.zabbix.proxy-pgsql
|
|
else pkgs.zabbix.proxy-sqlite;
|
|
defaultText = literalExpression "pkgs.zabbix.proxy-pgsql";
|
|
description = lib.mdDoc "The Zabbix package to use.";
|
|
};
|
|
|
|
extraPackages = mkOption {
|
|
type = types.listOf types.package;
|
|
default = with pkgs; [ nettools nmap traceroute ];
|
|
defaultText = literalExpression "[ nettools nmap traceroute ]";
|
|
description = lib.mdDoc ''
|
|
Packages to be added to the Zabbix {env}`PATH`.
|
|
Typically used to add executables for scripts, but can be anything.
|
|
'';
|
|
};
|
|
|
|
modules = mkOption {
|
|
type = types.attrsOf types.package;
|
|
description = lib.mdDoc "A set of modules to load.";
|
|
default = {};
|
|
example = literalExpression ''
|
|
{
|
|
"dummy.so" = pkgs.stdenv.mkDerivation {
|
|
name = "zabbix-dummy-module-''${cfg.package.version}";
|
|
src = cfg.package.src;
|
|
buildInputs = [ cfg.package ];
|
|
sourceRoot = "zabbix-''${cfg.package.version}/src/modules/dummy";
|
|
installPhase = '''
|
|
mkdir -p $out/lib
|
|
cp dummy.so $out/lib/
|
|
''';
|
|
};
|
|
}
|
|
'';
|
|
};
|
|
|
|
database = {
|
|
type = mkOption {
|
|
type = types.enum [ "mysql" "pgsql" "sqlite" ];
|
|
example = "mysql";
|
|
default = "pgsql";
|
|
description = lib.mdDoc "Database engine to use.";
|
|
};
|
|
|
|
host = mkOption {
|
|
type = types.str;
|
|
default = "localhost";
|
|
description = lib.mdDoc "Database host address.";
|
|
};
|
|
|
|
port = mkOption {
|
|
type = types.port;
|
|
default = if cfg.database.type == "mysql" then mysql.port else pgsql.port;
|
|
defaultText = literalExpression ''
|
|
if config.${opt.database.type} == "mysql"
|
|
then config.${options.services.mysql.port}
|
|
else config.${options.services.postgresql.port}
|
|
'';
|
|
description = lib.mdDoc "Database host port.";
|
|
};
|
|
|
|
name = mkOption {
|
|
type = types.str;
|
|
default = if cfg.database.type == "sqlite" then "${stateDir}/zabbix.db" else "zabbix";
|
|
defaultText = literalExpression "zabbix";
|
|
description = lib.mdDoc "Database name.";
|
|
};
|
|
|
|
user = mkOption {
|
|
type = types.str;
|
|
default = "zabbix";
|
|
description = lib.mdDoc "Database user.";
|
|
};
|
|
|
|
passwordFile = mkOption {
|
|
type = types.nullOr types.path;
|
|
default = null;
|
|
example = "/run/keys/zabbix-dbpassword";
|
|
description = lib.mdDoc ''
|
|
A file containing the password corresponding to
|
|
{option}`database.user`.
|
|
'';
|
|
};
|
|
|
|
socket = mkOption {
|
|
type = types.nullOr types.path;
|
|
default = null;
|
|
example = "/run/postgresql";
|
|
description = lib.mdDoc "Path to the unix socket file to use for authentication.";
|
|
};
|
|
|
|
createLocally = mkOption {
|
|
type = types.bool;
|
|
default = true;
|
|
description = lib.mdDoc "Whether to create a local database automatically.";
|
|
};
|
|
};
|
|
|
|
listen = {
|
|
ip = mkOption {
|
|
type = types.str;
|
|
default = "0.0.0.0";
|
|
description = lib.mdDoc ''
|
|
List of comma delimited IP addresses that the trapper should listen on.
|
|
Trapper will listen on all network interfaces if this parameter is missing.
|
|
'';
|
|
};
|
|
|
|
port = mkOption {
|
|
type = types.port;
|
|
default = 10051;
|
|
description = lib.mdDoc ''
|
|
Listen port for trapper.
|
|
'';
|
|
};
|
|
};
|
|
|
|
openFirewall = mkOption {
|
|
type = types.bool;
|
|
default = false;
|
|
description = lib.mdDoc ''
|
|
Open ports in the firewall for the Zabbix Proxy.
|
|
'';
|
|
};
|
|
|
|
settings = mkOption {
|
|
type = with types; attrsOf (oneOf [ int str (listOf str) ]);
|
|
default = {};
|
|
description = lib.mdDoc ''
|
|
Zabbix Proxy configuration. Refer to
|
|
<https://www.zabbix.com/documentation/current/manual/appendix/config/zabbix_proxy>
|
|
for details on supported values.
|
|
'';
|
|
example = {
|
|
CacheSize = "1G";
|
|
SSHKeyLocation = "/var/lib/zabbix/.ssh";
|
|
StartPingers = 32;
|
|
};
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
# implementation
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
assertions = [
|
|
{ assertion = !config.services.zabbixServer.enable;
|
|
message = "Please choose one of services.zabbixServer or services.zabbixProxy.";
|
|
}
|
|
{ assertion = cfg.database.createLocally -> cfg.database.user == user && cfg.database.name == cfg.database.user;
|
|
message = "services.zabbixProxy.database.user must be set to ${user} if services.zabbixProxy.database.createLocally is set true";
|
|
}
|
|
{ assertion = cfg.database.createLocally -> cfg.database.passwordFile == null;
|
|
message = "a password cannot be specified if services.zabbixProxy.database.createLocally is set to true";
|
|
}
|
|
];
|
|
|
|
services.zabbixProxy.settings = mkMerge [
|
|
{
|
|
LogType = "console";
|
|
ListenIP = cfg.listen.ip;
|
|
ListenPort = cfg.listen.port;
|
|
Server = cfg.server;
|
|
# TODO: set to cfg.database.socket if database type is pgsql?
|
|
DBHost = optionalString (cfg.database.createLocally != true) cfg.database.host;
|
|
DBName = cfg.database.name;
|
|
DBUser = cfg.database.user;
|
|
SocketDir = runtimeDir;
|
|
FpingLocation = "/run/wrappers/bin/fping";
|
|
LoadModule = builtins.attrNames cfg.modules;
|
|
}
|
|
(mkIf (cfg.database.createLocally != true) { DBPort = cfg.database.port; })
|
|
(mkIf (cfg.database.passwordFile != null) { Include = [ "${passwordFile}" ]; })
|
|
(mkIf (mysqlLocal && cfg.database.socket != null) { DBSocket = cfg.database.socket; })
|
|
(mkIf (cfg.modules != {}) { LoadModulePath = "${moduleEnv}/lib"; })
|
|
];
|
|
|
|
networking.firewall = mkIf cfg.openFirewall {
|
|
allowedTCPPorts = [ cfg.listen.port ];
|
|
};
|
|
|
|
services.mysql = optionalAttrs mysqlLocal {
|
|
enable = true;
|
|
package = mkDefault pkgs.mariadb;
|
|
};
|
|
|
|
systemd.services.mysql.postStart = mkAfter (optionalString mysqlLocal ''
|
|
( echo "CREATE DATABASE IF NOT EXISTS \`${cfg.database.name}\` CHARACTER SET utf8 COLLATE utf8_bin;"
|
|
echo "CREATE USER IF NOT EXISTS '${cfg.database.user}'@'localhost' IDENTIFIED WITH ${if (getName config.services.mysql.package == getName pkgs.mariadb) then "unix_socket" else "auth_socket"};"
|
|
echo "GRANT ALL PRIVILEGES ON \`${cfg.database.name}\`.* TO '${cfg.database.user}'@'localhost';"
|
|
) | ${config.services.mysql.package}/bin/mysql -N
|
|
'');
|
|
|
|
services.postgresql = optionalAttrs pgsqlLocal {
|
|
enable = true;
|
|
ensureDatabases = [ cfg.database.name ];
|
|
ensureUsers = [
|
|
{ name = cfg.database.user;
|
|
ensureDBOwnership = true;
|
|
}
|
|
];
|
|
};
|
|
|
|
users.users.${user} = {
|
|
description = "Zabbix daemon user";
|
|
uid = config.ids.uids.zabbix;
|
|
inherit group;
|
|
};
|
|
|
|
users.groups.${group} = {
|
|
gid = config.ids.gids.zabbix;
|
|
};
|
|
|
|
security.wrappers = {
|
|
fping =
|
|
{ setuid = true;
|
|
owner = "root";
|
|
group = "root";
|
|
source = "${pkgs.fping}/bin/fping";
|
|
};
|
|
};
|
|
|
|
systemd.services.zabbix-proxy = {
|
|
description = "Zabbix Proxy";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
after = optional mysqlLocal "mysql.service" ++ optional pgsqlLocal "postgresql.service";
|
|
|
|
path = [ "/run/wrappers" ] ++ cfg.extraPackages;
|
|
preStart = optionalString pgsqlLocal ''
|
|
if ! test -e "${stateDir}/db-created"; then
|
|
cat ${cfg.package}/share/zabbix/database/postgresql/schema.sql | ${pgsql.package}/bin/psql ${cfg.database.name}
|
|
touch "${stateDir}/db-created"
|
|
fi
|
|
'' + optionalString mysqlLocal ''
|
|
if ! test -e "${stateDir}/db-created"; then
|
|
cat ${cfg.package}/share/zabbix/database/mysql/schema.sql | ${mysql.package}/bin/mysql ${cfg.database.name}
|
|
touch "${stateDir}/db-created"
|
|
fi
|
|
'' + optionalString (cfg.database.type == "sqlite") ''
|
|
if ! test -e "${cfg.database.name}"; then
|
|
${pkgs.sqlite}/bin/sqlite3 "${cfg.database.name}" < ${cfg.package}/share/zabbix/database/sqlite3/schema.sql
|
|
fi
|
|
'' + optionalString (cfg.database.passwordFile != null) ''
|
|
# create a copy of the supplied password file in a format zabbix can consume
|
|
touch ${passwordFile}
|
|
chmod 0600 ${passwordFile}
|
|
echo -n "DBPassword = " > ${passwordFile}
|
|
cat ${cfg.database.passwordFile} >> ${passwordFile}
|
|
'';
|
|
|
|
serviceConfig = {
|
|
ExecStart = "@${cfg.package}/sbin/zabbix_proxy zabbix_proxy -f --config ${configFile}";
|
|
Restart = "always";
|
|
RestartSec = 2;
|
|
|
|
User = user;
|
|
Group = group;
|
|
RuntimeDirectory = "zabbix";
|
|
StateDirectory = "zabbix";
|
|
PrivateTmp = true;
|
|
};
|
|
};
|
|
|
|
};
|
|
|
|
}
|