nixos/services.pgadmin: remove with lib;

This commit is contained in:
Felix Buehler 2024-08-28 21:18:53 +02:00 committed by Jörg Thalheim
parent b0d554537c
commit 5e67f3e4c8

View File

@ -1,15 +1,12 @@
{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.services.pgadmin;
_base = with types; [ int bool str ];
base = with types; oneOf ([ (listOf (oneOf _base)) (attrsOf (oneOf _base)) ] ++ _base);
_base = with lib.types; [ int bool str ];
base = with lib.types; oneOf ([ (listOf (oneOf _base)) (attrsOf (oneOf _base)) ] ++ _base);
formatAttrset = attr:
"{${concatStringsSep "\n" (mapAttrsToList (key: value: "${builtins.toJSON key}: ${formatPyValue value},") attr)}}";
"{${lib.concatStringsSep "\n" (lib.mapAttrsToList (key: value: "${builtins.toJSON key}: ${formatPyValue value},") attr)}}";
formatPyValue = value:
if builtins.isString value then builtins.toJSON value
@ -17,98 +14,98 @@ let
else if builtins.isInt value then toString value
else if builtins.isBool value then (if value then "True" else "False")
else if builtins.isAttrs value then (formatAttrset value)
else if builtins.isList value then "[${concatStringsSep "\n" (map (v: "${formatPyValue v},") value)}]"
else if builtins.isList value then "[${lib.concatStringsSep "\n" (map (v: "${formatPyValue v},") value)}]"
else throw "Unrecognized type";
formatPy = attrs:
concatStringsSep "\n" (mapAttrsToList (key: value: "${key} = ${formatPyValue value}") attrs);
lib.concatStringsSep "\n" (lib.mapAttrsToList (key: value: "${key} = ${formatPyValue value}") attrs);
pyType = with types; attrsOf (oneOf [ (attrsOf base) (listOf base) base ]);
pyType = with lib.types; attrsOf (oneOf [ (attrsOf base) (listOf base) base ]);
in
{
options.services.pgadmin = {
enable = mkEnableOption "PostgreSQL Admin 4";
enable = lib.mkEnableOption "PostgreSQL Admin 4";
port = mkOption {
port = lib.mkOption {
description = "Port for pgadmin4 to run on";
type = types.port;
type = lib.types.port;
default = 5050;
};
package = mkPackageOption pkgs "pgadmin4" { };
package = lib.mkPackageOption pkgs "pgadmin4" { };
initialEmail = mkOption {
initialEmail = lib.mkOption {
description = "Initial email for the pgAdmin account";
type = types.str;
type = lib.types.str;
};
initialPasswordFile = mkOption {
initialPasswordFile = lib.mkOption {
description = ''
Initial password file for the pgAdmin account. Minimum length by default is 6.
Please see `services.pgadmin.minimumPasswordLength`.
NOTE: Should be string not a store path, to prevent the password from being world readable
'';
type = types.path;
type = lib.types.path;
};
minimumPasswordLength = mkOption {
minimumPasswordLength = lib.mkOption {
description = "Minimum length of the password";
type = types.int;
type = lib.types.int;
default = 6;
};
emailServer = {
enable = mkOption {
enable = lib.mkOption {
description = ''
Enable SMTP email server. This is necessary, if you want to use password recovery or change your own password
'';
type = types.bool;
type = lib.types.bool;
default = false;
};
address = mkOption {
address = lib.mkOption {
description = "SMTP server for email delivery";
type = types.str;
type = lib.types.str;
default = "localhost";
};
port = mkOption {
port = lib.mkOption {
description = "SMTP server port for email delivery";
type = types.port;
type = lib.types.port;
default = 25;
};
useSSL = mkOption {
useSSL = lib.mkOption {
description = "SMTP server should use SSL";
type = types.bool;
type = lib.types.bool;
default = false;
};
useTLS = mkOption {
useTLS = lib.mkOption {
description = "SMTP server should use TLS";
type = types.bool;
type = lib.types.bool;
default = false;
};
username = mkOption {
username = lib.mkOption {
description = "SMTP server username for email delivery";
type = types.nullOr types.str;
type = lib.types.nullOr lib.types.str;
default = null;
};
sender = mkOption {
sender = lib.mkOption {
description = ''
SMTP server sender email for email delivery. Some servers require this to be a valid email address from that server
'';
type = types.str;
type = lib.types.str;
example = "noreply@example.com";
};
passwordFile = mkOption {
passwordFile = lib.mkOption {
description = ''
Password for SMTP email account.
NOTE: Should be string not a store path, to prevent the password from being world readable
'';
type = types.path;
type = lib.types.path;
};
};
openFirewall = mkEnableOption "firewall passthrough for pgadmin4";
openFirewall = lib.mkEnableOption "firewall passthrough for pgadmin4";
settings = mkOption {
settings = lib.mkOption {
description = ''
Settings for pgadmin4.
[Documentation](https://www.pgadmin.org/docs/pgadmin4/development/config_py.html)
@ -118,17 +115,17 @@ in
};
};
config = mkIf (cfg.enable) {
networking.firewall.allowedTCPPorts = mkIf (cfg.openFirewall) [ cfg.port ];
config = lib.mkIf (cfg.enable) {
networking.firewall.allowedTCPPorts = lib.mkIf (cfg.openFirewall) [ cfg.port ];
services.pgadmin.settings = {
DEFAULT_SERVER_PORT = cfg.port;
PASSWORD_LENGTH_MIN = cfg.minimumPasswordLength;
SERVER_MODE = true;
UPGRADE_CHECK_ENABLED = false;
} // (optionalAttrs cfg.openFirewall {
DEFAULT_SERVER = mkDefault "::";
}) // (optionalAttrs cfg.emailServer.enable {
} // (lib.optionalAttrs cfg.openFirewall {
DEFAULT_SERVER = lib.mkDefault "::";
}) // (lib.optionalAttrs cfg.emailServer.enable {
MAIL_SERVER = cfg.emailServer.address;
MAIL_PORT = cfg.emailServer.port;
MAIL_USE_SSL = cfg.emailServer.useSSL;
@ -160,7 +157,7 @@ in
fi
(
# Email address:
echo ${escapeShellArg cfg.initialEmail}
echo ${lib.escapeShellArg cfg.initialEmail}
# file might not contain newline. echo hack fixes that.
PW=$(cat "$PW_FILE")
@ -183,7 +180,7 @@ in
StateDirectory = "pgadmin";
ExecStart = "${cfg.package}/bin/pgadmin4";
LoadCredential = [ "initial_password:${cfg.initialPasswordFile}" ]
++ optional cfg.emailServer.enable "email_password:${cfg.emailServer.passwordFile}";
++ lib.optional cfg.emailServer.enable "email_password:${cfg.emailServer.passwordFile}";
};
};