1
0
Fork 0
mirror of https://github.com/NixOS/nixpkgs.git synced 2025-07-08 19:45:42 +03:00

nixos/services.postgresqlWalReceiver: remove with lib;

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

View file

@ -1,24 +1,21 @@
{ config, lib, pkgs, ... }: { config, lib, pkgs, ... }:
with lib;
let let
receiverSubmodule = { receiverSubmodule = {
options = { options = {
postgresqlPackage = mkPackageOption pkgs "postgresql" { postgresqlPackage = lib.mkPackageOption pkgs "postgresql" {
example = "postgresql_15"; example = "postgresql_15";
}; };
directory = mkOption { directory = lib.mkOption {
type = types.path; type = lib.types.path;
example = literalExpression "/mnt/pg_wal/main/"; example = lib.literalExpression "/mnt/pg_wal/main/";
description = '' description = ''
Directory to write the output to. Directory to write the output to.
''; '';
}; };
statusInterval = mkOption { statusInterval = lib.mkOption {
type = types.int; type = lib.types.int;
default = 10; default = 10;
description = '' description = ''
Specifies the number of seconds between status packets sent back to the server. Specifies the number of seconds between status packets sent back to the server.
@ -28,8 +25,8 @@ let
''; '';
}; };
slot = mkOption { slot = lib.mkOption {
type = types.str; type = lib.types.str;
default = ""; default = "";
example = "some_slot_name"; example = "some_slot_name";
description = '' description = ''
@ -45,8 +42,8 @@ let
''; '';
}; };
synchronous = mkOption { synchronous = lib.mkOption {
type = types.bool; type = lib.types.bool;
default = false; default = false;
description = '' description = ''
Flush the WAL data to disk immediately after it has been received. Flush the WAL data to disk immediately after it has been received.
@ -57,8 +54,8 @@ let
''; '';
}; };
compress = mkOption { compress = lib.mkOption {
type = types.ints.between 0 9; type = lib.types.ints.between 0 9;
default = 0; default = 0;
description = '' description = ''
Enables gzip compression of write-ahead logs, and specifies the compression level Enables gzip compression of write-ahead logs, and specifies the compression level
@ -69,8 +66,8 @@ let
''; '';
}; };
connection = mkOption { connection = lib.mkOption {
type = types.str; type = lib.types.str;
example = "postgresql://user@somehost"; example = "postgresql://user@somehost";
description = '' description = ''
Specifies parameters used to connect to the server, as a connection string. Specifies parameters used to connect to the server, as a connection string.
@ -81,10 +78,10 @@ let
''; '';
}; };
extraArgs = mkOption { extraArgs = lib.mkOption {
type = with types; listOf str; type = with lib.types; listOf str;
default = [ ]; default = [ ];
example = literalExpression '' example = lib.literalExpression ''
[ [
"--no-sync" "--no-sync"
] ]
@ -94,10 +91,10 @@ let
''; '';
}; };
environment = mkOption { environment = lib.mkOption {
type = with types; attrsOf str; type = with lib.types; attrsOf str;
default = { }; default = { };
example = literalExpression '' example = lib.literalExpression ''
{ {
PGPASSFILE = "/private/passfile"; PGPASSFILE = "/private/passfile";
PGSSLMODE = "require"; PGSSLMODE = "require";
@ -114,10 +111,10 @@ let
in { in {
options = { options = {
services.postgresqlWalReceiver = { services.postgresqlWalReceiver = {
receivers = mkOption { receivers = lib.mkOption {
type = with types; attrsOf (submodule receiverSubmodule); type = with lib.types; attrsOf (submodule receiverSubmodule);
default = { }; default = { };
example = literalExpression '' example = lib.literalExpression ''
{ {
main = { main = {
postgresqlPackage = pkgs.postgresql_15; postgresqlPackage = pkgs.postgresql_15;
@ -138,7 +135,7 @@ in {
config = let config = let
receivers = config.services.postgresqlWalReceiver.receivers; receivers = config.services.postgresqlWalReceiver.receivers;
in mkIf (receivers != { }) { in lib.mkIf (receivers != { }) {
users = { users = {
users.postgres = { users.postgres = {
uid = config.ids.uids.postgres; uid = config.ids.uids.postgres;
@ -151,18 +148,18 @@ in {
}; };
}; };
assertions = concatLists (attrsets.mapAttrsToList (name: config: [ assertions = lib.concatLists (lib.attrsets.mapAttrsToList (name: config: [
{ {
assertion = config.compress > 0 -> versionAtLeast config.postgresqlPackage.version "10"; assertion = config.compress > 0 -> lib.versionAtLeast config.postgresqlPackage.version "10";
message = "Invalid configuration for WAL receiver \"${name}\": compress requires PostgreSQL version >= 10."; message = "Invalid configuration for WAL receiver \"${name}\": compress requires PostgreSQL version >= 10.";
} }
]) receivers); ]) receivers);
systemd.tmpfiles.rules = mapAttrsToList (name: config: '' systemd.tmpfiles.rules = lib.mapAttrsToList (name: config: ''
d ${escapeShellArg config.directory} 0750 postgres postgres - - d ${lib.escapeShellArg config.directory} 0750 postgres postgres - -
'') receivers; '') receivers;
systemd.services = with attrsets; mapAttrs' (name: config: nameValuePair "postgresql-wal-receiver-${name}" { systemd.services = lib.mapAttrs' (name: config: lib.nameValuePair "postgresql-wal-receiver-${name}" {
description = "PostgreSQL WAL receiver (${name})"; description = "PostgreSQL WAL receiver (${name})";
wantedBy = [ "multi-user.target" ]; wantedBy = [ "multi-user.target" ];
startLimitIntervalSec = 0; # retry forever, useful in case of network disruption startLimitIntervalSec = 0; # retry forever, useful in case of network disruption
@ -179,22 +176,22 @@ in {
script = let script = let
receiverCommand = postgresqlPackage: receiverCommand = postgresqlPackage:
if (versionAtLeast postgresqlPackage.version "10") if (lib.versionAtLeast postgresqlPackage.version "10")
then "${postgresqlPackage}/bin/pg_receivewal" then "${postgresqlPackage}/bin/pg_receivewal"
else "${postgresqlPackage}/bin/pg_receivexlog"; else "${postgresqlPackage}/bin/pg_receivexlog";
in '' in ''
${receiverCommand config.postgresqlPackage} \ ${receiverCommand config.postgresqlPackage} \
--no-password \ --no-password \
--directory=${escapeShellArg config.directory} \ --directory=${lib.escapeShellArg config.directory} \
--status-interval=${toString config.statusInterval} \ --status-interval=${toString config.statusInterval} \
--dbname=${escapeShellArg config.connection} \ --dbname=${lib.escapeShellArg config.connection} \
${optionalString (config.compress > 0) "--compress=${toString config.compress}"} \ ${lib.optionalString (config.compress > 0) "--compress=${toString config.compress}"} \
${optionalString (config.slot != "") "--slot=${escapeShellArg config.slot}"} \ ${lib.optionalString (config.slot != "") "--slot=${lib.escapeShellArg config.slot}"} \
${optionalString config.synchronous "--synchronous"} \ ${lib.optionalString config.synchronous "--synchronous"} \
${concatStringsSep " " config.extraArgs} ${lib.concatStringsSep " " config.extraArgs}
''; '';
}) receivers; }) receivers;
}; };
meta.maintainers = with maintainers; [ pacien ]; meta.maintainers = with lib.maintainers; [ pacien ];
} }