about summary refs log tree commit diff
path: root/nixpkgs/nixos/modules/services/admin/pgadmin.nix
blob: 20b6b6670d9ccb6728d7e4b5fbb9871fb8678fca (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
{ 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);

  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 = {
    enable = mkEnableOption (lib.mdDoc "PostgreSQL Admin 4");

    port = mkOption {
      description = lib.mdDoc "Port for pgadmin4 to run on";
      type = types.port;
      default = 5050;
    };

    package = mkPackageOptionMD pkgs "pgadmin4" { };

    initialEmail = mkOption {
      description = lib.mdDoc "Initial email for the pgAdmin account";
      type = types.str;
    };

    initialPasswordFile = mkOption {
      description = lib.mdDoc ''
        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;
    };

    minimumPasswordLength = mkOption {
      description = lib.mdDoc "Minimum length of the password";
      type = types.int;
      default = 6;
    };

    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;
      };
    };

    openFirewall = mkEnableOption (lib.mdDoc "firewall passthrough for pgadmin4");

    settings = mkOption {
      description = lib.mdDoc ''
        Settings for pgadmin4.
        [Documentation](https://www.pgadmin.org/docs/pgadmin4/development/config_py.html)
      '';
      type = pyType;
      default = { };
    };
  };

  config = mkIf (cfg.enable) {
    networking.firewall.allowedTCPPorts = 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 {
      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;
    });

    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)
        # Check here for password length to prevent pgadmin from starting
        # and presenting a hard to find error message
        # see https://github.com/NixOS/nixpkgs/issues/270624
        PW_LENGTH=$(wc -m < ${escapeShellArg cfg.initialPasswordFile})
        if [ $PW_LENGTH -lt ${toString cfg.minimumPasswordLength} ]; then
            echo "Password must be at least ${toString cfg.minimumPasswordLength} characters long"
            exit 1
        fi
        (
          # 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"
        ) | ${cfg.package}/bin/pgadmin4-cli setup-db
      '';

      restartTriggers = [
        "/etc/pgadmin/config_system.py"
      ];

      serviceConfig = {
        User = "pgadmin";
        DynamicUser = true;
        LogsDirectory = "pgadmin";
        StateDirectory = "pgadmin";
        ExecStart = "${cfg.package}/bin/pgadmin4";
      };
    };

    users.users.pgadmin = {
      isSystemUser = true;
      group = "pgadmin";
    };

    users.groups.pgadmin = { };

    environment.etc."pgadmin/config_system.py" = {
      text = lib.optionalString cfg.emailServer.enable ''
        with open("${cfg.emailServer.passwordFile}") as f:
          pw = f.read()
        MAIL_PASSWORD = pw
      '' + formatPy cfg.settings;
      mode = "0600";
      user = "pgadmin";
      group = "pgadmin";
    };
  };
}