Skip to content

Commit bb2e26c

Browse files
authored
Merge pull request #1809 from cachix/fix-pghost
postgres: improve how PGHOST is computed
2 parents 379980f + fb2cb01 commit bb2e26c

File tree

3 files changed

+73
-5
lines changed

3 files changed

+73
-5
lines changed

docs/reference/options.md

+13-1
Original file line numberDiff line numberDiff line change
@@ -84429,7 +84429,19 @@ CREATE ROLE bar;
8442984429

8443084430

8443184431

84432-
Listen address
84432+
A comma-separated list of TCP/IP address(es) on which the server should listen for connections.
84433+
84434+
By default, the server only accepts connections over unix sockets.
84435+
84436+
This option is parsed to set the ` PGHOST ` environment variable.
84437+
84438+
Special values:
84439+
84440+
- '\*' to listen on all available network interfaces.
84441+
- '0.0.0.0' to listen on all available IPv4 network interfaces.
84442+
- '::' to listen on all available IPv6 network interfaces.
84443+
- 'localhost' to listen only on the loopback interface.
84444+
- '' (empty string) disables TCP/IP connections and listens only on the unix socket.
8443384445

8443484446

8443584447

docs/supported-services/postgres.md

+13-1
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,19 @@ CREATE ROLE bar;
362362

363363

364364

365-
Listen address
365+
A comma-separated list of TCP/IP address(es) on which the server should listen for connections\.
366+
367+
By default, the server only accepts connections over unix sockets\.
368+
369+
This option is parsed to set the ` PGHOST ` environment variable\.
370+
371+
Special values:
372+
373+
- '\*' to listen on all available network interfaces\.
374+
- '0\.0\.0\.0' to listen on all available IPv4 network interfaces\.
375+
- '::' to listen on all available IPv6 network interfaces\.
376+
- 'localhost' to listen only on the loopback interface\.
377+
- '' (empty string) disables TCP/IP connections and listens only on the unix socket\.
366378

367379

368380

src/modules/services/postgres.nix

+47-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,26 @@ let
1111

1212
runtimeDir = "${config.env.DEVENV_RUNTIME}/postgres";
1313

14+
parseListenAddresses = input:
15+
let
16+
convertSpecialValue = value:
17+
if value == "*" || value == "0.0.0.0" then "127.0.0.1"
18+
else if value == "::" then "::1"
19+
else value;
20+
in
21+
lib.pipe input [
22+
(lib.splitString ",")
23+
(map lib.trim)
24+
(map convertSpecialValue)
25+
(builtins.filter (x: x != ""))
26+
];
27+
28+
# Fetch the first element of a list or return null if the list is empty.
29+
headWithDefault = default: input:
30+
if input == [ ]
31+
then default
32+
else builtins.head input;
33+
1434
postgresPkg =
1535
if cfg.extensions != null
1636
then
@@ -212,7 +232,20 @@ in
212232

213233
listen_addresses = lib.mkOption {
214234
type = types.str;
215-
description = "Listen address";
235+
description = ''
236+
A comma-separated list of TCP/IP address(es) on which the server should listen for connections.
237+
238+
By default, the server only accepts connections over unix sockets.
239+
240+
This option is parsed to set the `PGHOST` environment variable.
241+
242+
Special values:
243+
- \'*\' to listen on all available network interfaces.
244+
- \'0.0.0.0\' to listen on all available IPv4 network interfaces.
245+
- \'::\' to listen on all available IPv6 network interfaces.
246+
- \'localhost\' to listen only on the loopback interface.
247+
- \'\' (empty string) disables TCP/IP connections and listens only on the unix socket.
248+
'';
216249
default = "";
217250
example = "127.0.0.1";
218251
};
@@ -344,8 +377,19 @@ in
344377
packages = [ postgresPkg startScript ];
345378

346379
env.PGDATA = config.env.DEVENV_STATE + "/postgres";
347-
env.PGHOST = lib.mkDefault runtimeDir;
348-
env.PGPORT = cfg.port;
380+
env.PGHOST =
381+
let
382+
parsedAddress = headWithDefault null (parseListenAddresses cfg.listen_addresses);
383+
host =
384+
if cfg.listen_addresses != ""
385+
then parsedAddress
386+
else runtimeDir;
387+
in
388+
lib.mkDefault host;
389+
env.PGPORT =
390+
if cfg.listen_addresses != ""
391+
then cfg.port
392+
else null;
349393

350394
services.postgres.settings = {
351395
listen_addresses = cfg.listen_addresses;

0 commit comments

Comments
 (0)