From 7e4e37fff4cd97cc7fed26c9e1060761ed1379a7 Mon Sep 17 00:00:00 2001 From: danbst Date: Tue, 23 Jul 2019 21:53:59 +0300 Subject: [PATCH] postgresql: allow changing initidb arguments via module system Closes https://github.com/NixOS/nixpkgs/issues/18829 + some cleanups --- .../modules/services/databases/postgresql.nix | 25 +++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/nixos/modules/services/databases/postgresql.nix b/nixos/modules/services/databases/postgresql.nix index 7dddebfc28dd..b89f4a57253a 100644 --- a/nixos/modules/services/databases/postgresql.nix +++ b/nixos/modules/services/databases/postgresql.nix @@ -119,6 +119,15 @@ in ''; }; + initdbFlags = mkOption { + type = with types; listOf str; + default = []; + description = '' + Additional flags passed to initdb during data dir + initialisation. + ''; + }; + initialScript = mkOption { type = types.nullOr types.path; default = null; @@ -257,10 +266,10 @@ in ###### implementation - config = mkIf config.services.postgresql.enable { + config = mkIf cfg.enable { assertions = [ - { assertion = cfg.groupAccess -> builtins.compareVersions cfg.package.version "11.0" >= 0; + { assertion = cfg.groupAccess -> versionAtLeast cfg.package.version "11.0"; message = '' 'groupAccess' is not available for PostgreSQL < 11. ''; @@ -276,8 +285,12 @@ in else pkgs.postgresql_9_4); services.postgresql.dataDir = - mkDefault (if versionAtLeast config.system.stateVersion "17.09" then "/var/lib/postgresql/${config.services.postgresql.package.psqlSchema}" - else "/var/db/postgresql"); + mkDefault (if versionAtLeast config.system.stateVersion "17.09" + 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 '' @@ -324,9 +337,7 @@ in '' # Initialise the database. if ! test -e ${cfg.dataDir}/PG_VERSION; then - initdb -U ${cfg.superUser} ${ - lib.optionalString cfg.groupAccess "--allow-group-access" - } + initdb -U ${cfg.superUser} ${lib.concatStringsSep " " cfg.initdbFlags} # See postStart! touch "${cfg.dataDir}/.first_startup" fi