postgresql: allow changing initidb arguments via module system

Closes https://github.com/NixOS/nixpkgs/issues/18829

+ some cleanups
This commit is contained in:
danbst 2019-07-23 21:53:59 +03:00
parent 92a015d35d
commit 7e4e37fff4

View file

@ -119,6 +119,15 @@ in
''; '';
}; };
initdbFlags = mkOption {
type = with types; listOf str;
default = [];
description = ''
Additional flags passed to <literal>initdb<literal> during data dir
initialisation.
'';
};
initialScript = mkOption { initialScript = mkOption {
type = types.nullOr types.path; type = types.nullOr types.path;
default = null; default = null;
@ -257,10 +266,10 @@ in
###### implementation ###### implementation
config = mkIf config.services.postgresql.enable { config = mkIf cfg.enable {
assertions = [ assertions = [
{ assertion = cfg.groupAccess -> builtins.compareVersions cfg.package.version "11.0" >= 0; { assertion = cfg.groupAccess -> versionAtLeast cfg.package.version "11.0";
message = '' message = ''
'groupAccess' is not available for PostgreSQL < 11. 'groupAccess' is not available for PostgreSQL < 11.
''; '';
@ -276,8 +285,12 @@ in
else pkgs.postgresql_9_4); else pkgs.postgresql_9_4);
services.postgresql.dataDir = services.postgresql.dataDir =
mkDefault (if versionAtLeast config.system.stateVersion "17.09" then "/var/lib/postgresql/${config.services.postgresql.package.psqlSchema}" mkDefault (if versionAtLeast config.system.stateVersion "17.09"
else "/var/db/postgresql"); then "/var/lib/postgresql/${cfg.package.psqlSchema}"
else "/var/db/postgresql");
services.postgresql.initdbFlags =
mkDefault (lib.optional cfg.groupAccess "--allow-group-access");
services.postgresql.authentication = mkAfter services.postgresql.authentication = mkAfter
'' ''
@ -324,9 +337,7 @@ in
'' ''
# Initialise the database. # Initialise the database.
if ! test -e ${cfg.dataDir}/PG_VERSION; then if ! test -e ${cfg.dataDir}/PG_VERSION; then
initdb -U ${cfg.superUser} ${ initdb -U ${cfg.superUser} ${lib.concatStringsSep " " cfg.initdbFlags}
lib.optionalString cfg.groupAccess "--allow-group-access"
}
# See postStart! # See postStart!
touch "${cfg.dataDir}/.first_startup" touch "${cfg.dataDir}/.first_startup"
fi fi