Skip to content

Commit

Permalink
nixos/librenms: use db socket when set and add package option (#359182)
Browse files Browse the repository at this point in the history
  • Loading branch information
NetaliDev authored Dec 14, 2024
2 parents 184ee25 + d512e07 commit d71a756
Showing 1 changed file with 33 additions and 8 deletions.
41 changes: 33 additions & 8 deletions nixos/modules/services/monitoring/librenms.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ let
settingsFormat = pkgs.formats.json { };
configJson = settingsFormat.generate "librenms-config.json" cfg.settings;

package = pkgs.librenms.override {
package = cfg.package.override {
logDir = cfg.logDir;
dataDir = cfg.dataDir;
};
Expand Down Expand Up @@ -60,6 +60,18 @@ in
options.services.librenms = with lib; {
enable = mkEnableOption "LibreNMS network monitoring system";

package = lib.mkPackageOption pkgs "librenms" { };

finalPackage = lib.mkOption {
type = lib.types.package;
readOnly = true;
default = package;
defaultText = lib.literalExpression "package";
description = ''
The final package used by the module. This is the package that has all overrides.
'';
};

user = mkOption {
type = types.str;
default = "librenms";
Expand Down Expand Up @@ -526,13 +538,25 @@ in
User = cfg.user;
Group = cfg.group;
ExecStartPre = lib.mkIf cfg.database.createLocally [
"!${pkgs.writeShellScript "librenms-db-init" ''
DB_PASSWORD=$(cat ${cfg.database.passwordFile} | tr -d '\n')
echo "ALTER USER '${cfg.database.username}'@'localhost' IDENTIFIED BY '$DB_PASSWORD';" | ${pkgs.mariadb}/bin/mysql
${lib.optionalString cfg.useDistributedPollers ''
echo "ALTER USER '${cfg.database.username}'@'%' IDENTIFIED BY '$DB_PASSWORD';" | ${pkgs.mariadb}/bin/mysql
''}
''}"
"!${
pkgs.writeShellScript "librenms-db-init" (
if !isNull cfg.database.socket then
''
echo "ALTER USER '${cfg.database.username}'@'localhost' IDENTIFIED VIA unix_socket;" | ${pkgs.mariadb}/bin/mysql --socket='${cfg.database.socket}'
${lib.optionalString cfg.useDistributedPollers ''
echo "ALTER USER '${cfg.database.username}'@'%' IDENTIFIED VIA unix_socket;" | ${pkgs.mariadb}/bin/mysql --socket='${cfg.database.socket}'
''}
''
else
''
DB_PASSWORD=$(cat ${cfg.database.passwordFile} | tr -d '\n')
echo "ALTER USER '${cfg.database.username}'@'localhost' IDENTIFIED BY '$DB_PASSWORD';" | ${pkgs.mariadb}/bin/mysql
${lib.optionalString cfg.useDistributedPollers ''
echo "ALTER USER '${cfg.database.username}'@'%' IDENTIFIED BY '$DB_PASSWORD';" | ${pkgs.mariadb}/bin/mysql
''}
''
)
}"
];
};
script = ''
Expand Down Expand Up @@ -567,6 +591,7 @@ in
then ''
# use socket connection
echo "DB_SOCKET=${cfg.database.socket}" >> ${cfg.dataDir}/.env
echo "DB_PASSWORD=null" >> ${cfg.dataDir}/.env
''
else ''
# use TCP connection
Expand Down

0 comments on commit d71a756

Please sign in to comment.