2022-02-23 14:37:29 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
pkg = pkgs.pgadmin4;
|
|
|
|
cfg = config.services.pgadmin;
|
|
|
|
|
|
|
|
_base = with types; [ int bool str ];
|
|
|
|
base = with types; oneOf ([ (listOf (oneOf _base)) (attrsOf (oneOf _base)) ] ++ _base);
|
|
|
|
|
|
|
|
formatAttrset = attr:
|
|
|
|
"{${concatStringsSep "\n" (mapAttrsToList (key: value: "${builtins.toJSON key}: ${formatPyValue value},") attr)}}";
|
|
|
|
|
|
|
|
formatPyValue = value:
|
|
|
|
if builtins.isString value then builtins.toJSON value
|
|
|
|
else if value ? _expr then value._expr
|
|
|
|
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 throw "Unrecognized type";
|
|
|
|
|
|
|
|
formatPy = attrs:
|
|
|
|
concatStringsSep "\n" (mapAttrsToList (key: value: "${key} = ${formatPyValue value}") attrs);
|
|
|
|
|
|
|
|
pyType = with types; attrsOf (oneOf [ (attrsOf base) (listOf base) base ]);
|
|
|
|
in
|
|
|
|
{
|
|
|
|
options.services.pgadmin = {
|
2022-08-28 21:18:44 +02:00
|
|
|
enable = mkEnableOption (lib.mdDoc "PostgreSQL Admin 4");
|
2022-02-23 14:37:29 +01:00
|
|
|
|
|
|
|
port = mkOption {
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc "Port for pgadmin4 to run on";
|
2022-02-23 14:37:29 +01:00
|
|
|
type = types.port;
|
|
|
|
default = 5050;
|
|
|
|
};
|
|
|
|
|
|
|
|
initialEmail = mkOption {
|
2022-08-27 23:37:28 +02:00
|
|
|
description = lib.mdDoc "Initial email for the pgAdmin account";
|
2022-02-23 14:37:29 +01:00
|
|
|
type = types.str;
|
|
|
|
};
|
|
|
|
|
|
|
|
initialPasswordFile = mkOption {
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc ''
|
2022-02-23 14:37:29 +01:00
|
|
|
Initial password file for the pgAdmin account.
|
2022-08-27 23:37:28 +02:00
|
|
|
NOTE: Should be string not a store path, to prevent the password from being world readable
|
2022-02-23 14:37:29 +01:00
|
|
|
'';
|
|
|
|
type = types.path;
|
|
|
|
};
|
|
|
|
|
2022-08-27 23:37:28 +02:00
|
|
|
emailServer = {
|
|
|
|
enable = mkOption {
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Enable SMTP email server. This is necessary, if you want to use password recovery or change your own password
|
|
|
|
'';
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
};
|
|
|
|
address = mkOption {
|
|
|
|
description = lib.mdDoc "SMTP server for email delivery";
|
|
|
|
type = types.str;
|
|
|
|
default = "localhost";
|
|
|
|
};
|
|
|
|
port = mkOption {
|
|
|
|
description = lib.mdDoc "SMTP server port for email delivery";
|
|
|
|
type = types.port;
|
|
|
|
default = 25;
|
|
|
|
};
|
|
|
|
useSSL = mkOption {
|
|
|
|
description = lib.mdDoc "SMTP server should use SSL";
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
};
|
|
|
|
useTLS = mkOption {
|
|
|
|
description = lib.mdDoc "SMTP server should use TLS";
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
};
|
|
|
|
username = mkOption {
|
|
|
|
description = lib.mdDoc "SMTP server username for email delivery";
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
|
|
|
};
|
|
|
|
sender = mkOption {
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
SMTP server sender email for email delivery. Some servers require this to be a valid email address from that server
|
|
|
|
'';
|
|
|
|
type = types.str;
|
|
|
|
example = "noreply@example.com";
|
|
|
|
};
|
|
|
|
passwordFile = mkOption {
|
|
|
|
description = lib.mdDoc ''
|
|
|
|
Password for SMTP email account.
|
|
|
|
NOTE: Should be string not a store path, to prevent the password from being world readable
|
|
|
|
'';
|
|
|
|
type = types.path;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-08-28 21:18:44 +02:00
|
|
|
openFirewall = mkEnableOption (lib.mdDoc "firewall passthrough for pgadmin4");
|
2022-02-23 14:37:29 +01:00
|
|
|
|
|
|
|
settings = mkOption {
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc ''
|
2022-02-23 14:37:29 +01:00
|
|
|
Settings for pgadmin4.
|
2022-08-27 23:37:28 +02:00
|
|
|
[Documentation](https://www.pgadmin.org/docs/pgadmin4/development/config_py.html)
|
2022-02-23 14:37:29 +01:00
|
|
|
'';
|
|
|
|
type = pyType;
|
2022-08-27 23:37:28 +02:00
|
|
|
default = { };
|
2022-02-23 14:37:29 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf (cfg.enable) {
|
|
|
|
networking.firewall.allowedTCPPorts = mkIf (cfg.openFirewall) [ cfg.port ];
|
|
|
|
|
|
|
|
services.pgadmin.settings = {
|
|
|
|
DEFAULT_SERVER_PORT = cfg.port;
|
|
|
|
SERVER_MODE = true;
|
|
|
|
} // (optionalAttrs cfg.openFirewall {
|
|
|
|
DEFAULT_SERVER = mkDefault "::";
|
2022-08-27 23:37:28 +02:00
|
|
|
}) // (optionalAttrs cfg.emailServer.enable {
|
|
|
|
MAIL_SERVER = cfg.emailServer.address;
|
|
|
|
MAIL_PORT = cfg.emailServer.port;
|
|
|
|
MAIL_USE_SSL = cfg.emailServer.useSSL;
|
|
|
|
MAIL_USE_TLS = cfg.emailServer.useTLS;
|
|
|
|
MAIL_USERNAME = cfg.emailServer.username;
|
|
|
|
SECURITY_EMAIL_SENDER = cfg.emailServer.sender;
|
2022-02-23 14:37:29 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
systemd.services.pgadmin = {
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
after = [ "network.target" ];
|
|
|
|
requires = [ "network.target" ];
|
|
|
|
# we're adding this optionally so just in case there's any race it'll be caught
|
|
|
|
# in case postgres doesn't start, pgadmin will just start normally
|
|
|
|
wants = [ "postgresql.service" ];
|
|
|
|
|
|
|
|
path = [ config.services.postgresql.package pkgs.coreutils pkgs.bash ];
|
|
|
|
|
|
|
|
preStart = ''
|
|
|
|
# NOTE: this is idempotent (aka running it twice has no effect)
|
|
|
|
(
|
|
|
|
# Email address:
|
|
|
|
echo ${escapeShellArg cfg.initialEmail}
|
|
|
|
|
|
|
|
# file might not contain newline. echo hack fixes that.
|
|
|
|
PW=$(cat ${escapeShellArg cfg.initialPasswordFile})
|
|
|
|
|
|
|
|
# Password:
|
|
|
|
echo "$PW"
|
|
|
|
# Retype password:
|
|
|
|
echo "$PW"
|
|
|
|
) | ${pkg}/bin/pgadmin4-setup
|
|
|
|
'';
|
|
|
|
|
|
|
|
restartTriggers = [
|
|
|
|
"/etc/pgadmin/config_system.py"
|
|
|
|
];
|
|
|
|
|
|
|
|
serviceConfig = {
|
|
|
|
User = "pgadmin";
|
|
|
|
DynamicUser = true;
|
|
|
|
LogsDirectory = "pgadmin";
|
|
|
|
StateDirectory = "pgadmin";
|
|
|
|
ExecStart = "${pkg}/bin/pgadmin4";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
users.users.pgadmin = {
|
|
|
|
isSystemUser = true;
|
|
|
|
group = "pgadmin";
|
|
|
|
};
|
|
|
|
|
2022-08-27 23:37:28 +02:00
|
|
|
users.groups.pgadmin = { };
|
2022-02-23 14:37:29 +01:00
|
|
|
|
|
|
|
environment.etc."pgadmin/config_system.py" = {
|
2022-08-27 23:37:28 +02:00
|
|
|
text = lib.optionalString cfg.emailServer.enable ''
|
|
|
|
with open("${cfg.emailServer.passwordFile}") as f:
|
|
|
|
pw = f.read()
|
|
|
|
MAIL_PASSWORD = pw
|
|
|
|
'' + formatPy cfg.settings;
|
2022-02-23 14:37:29 +01:00
|
|
|
mode = "0600";
|
|
|
|
user = "pgadmin";
|
|
|
|
group = "pgadmin";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|