Skip to content

postgres: improve how PGHOST is computed #1809

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion docs/reference/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -84429,7 +84429,19 @@ CREATE ROLE bar;



Listen address
A comma-separated list of TCP/IP address(es) on which the server should listen for connections.

By default, the server only accepts connections over unix sockets.

This option is parsed to set the ` PGHOST ` environment variable.

Special values:

- '\*' to listen on all available network interfaces.
- '0.0.0.0' to listen on all available IPv4 network interfaces.
- '::' to listen on all available IPv6 network interfaces.
- 'localhost' to listen only on the loopback interface.
- '' (empty string) disables TCP/IP connections and listens only on the unix socket.



Expand Down
14 changes: 13 additions & 1 deletion docs/supported-services/postgres.md
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,19 @@ CREATE ROLE bar;



Listen address
A comma-separated list of TCP/IP address(es) on which the server should listen for connections\.

By default, the server only accepts connections over unix sockets\.

This option is parsed to set the ` PGHOST ` environment variable\.

Special values:

- '\*' to listen on all available network interfaces\.
- '0\.0\.0\.0' to listen on all available IPv4 network interfaces\.
- '::' to listen on all available IPv6 network interfaces\.
- 'localhost' to listen only on the loopback interface\.
- '' (empty string) disables TCP/IP connections and listens only on the unix socket\.



Expand Down
50 changes: 47 additions & 3 deletions src/modules/services/postgres.nix
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,26 @@ let

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

parseListenAddresses = input:
let
convertSpecialValue = value:
if value == "*" || value == "0.0.0.0" then "127.0.0.1"
else if value == "::" then "::1"
else value;
in
lib.pipe input [
(lib.splitString ",")
(map lib.trim)
(map convertSpecialValue)
(builtins.filter (x: x != ""))
];

# Fetch the first element of a list or return null if the list is empty.
headWithDefault = default: input:
if input == [ ]
then default
else builtins.head input;

postgresPkg =
if cfg.extensions != null
then
Expand Down Expand Up @@ -212,7 +232,20 @@ in

listen_addresses = lib.mkOption {
type = types.str;
description = "Listen address";
description = ''
A comma-separated list of TCP/IP address(es) on which the server should listen for connections.

By default, the server only accepts connections over unix sockets.

This option is parsed to set the `PGHOST` environment variable.

Special values:
- \'*\' to listen on all available network interfaces.
- \'0.0.0.0\' to listen on all available IPv4 network interfaces.
- \'::\' to listen on all available IPv6 network interfaces.
- \'localhost\' to listen only on the loopback interface.
- \'\' (empty string) disables TCP/IP connections and listens only on the unix socket.
'';
default = "";
example = "127.0.0.1";
};
Expand Down Expand Up @@ -344,8 +377,19 @@ in
packages = [ postgresPkg startScript ];

env.PGDATA = config.env.DEVENV_STATE + "/postgres";
env.PGHOST = lib.mkDefault runtimeDir;
env.PGPORT = cfg.port;
env.PGHOST =
let
parsedAddress = headWithDefault null (parseListenAddresses cfg.listen_addresses);
host =
if cfg.listen_addresses != ""
then parsedAddress
else runtimeDir;
in
lib.mkDefault host;
env.PGPORT =
if cfg.listen_addresses != ""
then cfg.port
else null;

services.postgres.settings = {
listen_addresses = cfg.listen_addresses;
Expand Down
Loading