mirror of
https://github.com/NixOS/nixpkgs.git
synced 2024-11-22 06:53:01 +00:00
nixos/postgresql: turn settings
into a submodule
The main idea behind that was to be able to do more sophisticated merging for stuff that goes into `postgresql.conf`: `shared_preload_libraries` is a comma-separated list in a `types.str` and thus not mergeable. With this change, the option accepts both a comma-separated string xor a list of strings. This can be implemented rather quick using `coercedTo` + freeform modules. The interface still behaves equally, but it allows to merge declarations for this option together. One side-effect was that I had to change the `attrsOf (oneOf ...)` part into a submodule to allow declaring options for certain things. While at it, I decided to move `log_line_prefix` and `port` into this structure as well.
This commit is contained in:
parent
8b152a2242
commit
5142b7afa8
@ -118,7 +118,7 @@ are already created.
|
|||||||
before = "service1.service";
|
before = "service1.service";
|
||||||
after = "postgresql.service";
|
after = "postgresql.service";
|
||||||
serviceConfig.User = "postgres";
|
serviceConfig.User = "postgres";
|
||||||
environment.PSQL = "psql --port=${toString services.postgresql.port}";
|
environment.PSQL = "psql --port=${toString services.postgresql.settings.port}";
|
||||||
path = [ postgresql ];
|
path = [ postgresql ];
|
||||||
script = ''
|
script = ''
|
||||||
$PSQL service1 -c 'GRANT SELECT ON ALL TABLES IN SCHEMA public TO "extraUser1"'
|
$PSQL service1 -c 'GRANT SELECT ON ALL TABLES IN SCHEMA public TO "extraUser1"'
|
||||||
@ -139,7 +139,7 @@ are already created.
|
|||||||
|
|
||||||
```nix
|
```nix
|
||||||
{
|
{
|
||||||
environment.PSQL = "psql --port=${toString services.postgresql.port}";
|
environment.PSQL = "psql --port=${toString services.postgresql.settings.port}";
|
||||||
path = [ postgresql ];
|
path = [ postgresql ];
|
||||||
systemd.services."service1".preStart = ''
|
systemd.services."service1".preStart = ''
|
||||||
$PSQL -c 'GRANT SELECT ON ALL TABLES IN SCHEMA public TO "extraUser1"'
|
$PSQL -c 'GRANT SELECT ON ALL TABLES IN SCHEMA public TO "extraUser1"'
|
||||||
@ -159,7 +159,7 @@ are already created.
|
|||||||
before = "service1.service";
|
before = "service1.service";
|
||||||
after = "postgresql.service";
|
after = "postgresql.service";
|
||||||
serviceConfig.User = "service1";
|
serviceConfig.User = "service1";
|
||||||
environment.PSQL = "psql --port=${toString services.postgresql.port}";
|
environment.PSQL = "psql --port=${toString services.postgresql.settings.port}";
|
||||||
path = [ postgresql ];
|
path = [ postgresql ];
|
||||||
script = ''
|
script = ''
|
||||||
$PSQL -c 'GRANT SELECT ON ALL TABLES IN SCHEMA public TO "extraUser1"'
|
$PSQL -c 'GRANT SELECT ON ALL TABLES IN SCHEMA public TO "extraUser1"'
|
||||||
|
@ -27,7 +27,7 @@ let
|
|||||||
else toString value;
|
else toString value;
|
||||||
|
|
||||||
# The main PostgreSQL configuration file.
|
# The main PostgreSQL configuration file.
|
||||||
configFile = pkgs.writeTextDir "postgresql.conf" (concatStringsSep "\n" (mapAttrsToList (n: v: "${n} = ${toStr v}") cfg.settings));
|
configFile = pkgs.writeTextDir "postgresql.conf" (concatStringsSep "\n" (mapAttrsToList (n: v: "${n} = ${toStr v}") (filterAttrs (const (x: x != null)) cfg.settings)));
|
||||||
|
|
||||||
configFileCheck = pkgs.runCommand "postgresql-configfile-check" {} ''
|
configFileCheck = pkgs.runCommand "postgresql-configfile-check" {} ''
|
||||||
${cfg.package}/bin/postgres -D${configFile} -C config_file >/dev/null
|
${cfg.package}/bin/postgres -D${configFile} -C config_file >/dev/null
|
||||||
@ -41,6 +41,9 @@ in
|
|||||||
{
|
{
|
||||||
imports = [
|
imports = [
|
||||||
(mkRemovedOptionModule [ "services" "postgresql" "extraConfig" ] "Use services.postgresql.settings instead.")
|
(mkRemovedOptionModule [ "services" "postgresql" "extraConfig" ] "Use services.postgresql.settings instead.")
|
||||||
|
|
||||||
|
(mkRenamedOptionModule [ "services" "postgresql" "logLinePrefix" ] [ "services" "postgresql" "settings" "log_line_prefix" ])
|
||||||
|
(mkRenamedOptionModule [ "services" "postgresql" "port" ] [ "services" "postgresql" "settings" "port" ])
|
||||||
];
|
];
|
||||||
|
|
||||||
###### interface
|
###### interface
|
||||||
@ -57,14 +60,6 @@ in
|
|||||||
example = "postgresql_15";
|
example = "postgresql_15";
|
||||||
};
|
};
|
||||||
|
|
||||||
port = mkOption {
|
|
||||||
type = types.port;
|
|
||||||
default = 5432;
|
|
||||||
description = lib.mdDoc ''
|
|
||||||
The port on which PostgreSQL listens.
|
|
||||||
'';
|
|
||||||
};
|
|
||||||
|
|
||||||
checkConfig = mkOption {
|
checkConfig = mkOption {
|
||||||
type = types.bool;
|
type = types.bool;
|
||||||
default = true;
|
default = true;
|
||||||
@ -352,17 +347,6 @@ in
|
|||||||
'';
|
'';
|
||||||
};
|
};
|
||||||
|
|
||||||
logLinePrefix = mkOption {
|
|
||||||
type = types.str;
|
|
||||||
default = "[%p] ";
|
|
||||||
example = "%m [%p] ";
|
|
||||||
description = lib.mdDoc ''
|
|
||||||
A printf-style string that is output at the beginning of each log line.
|
|
||||||
Upstream default is `'%m [%p] '`, i.e. it includes the timestamp. We do
|
|
||||||
not include the timestamp, because journal has it anyway.
|
|
||||||
'';
|
|
||||||
};
|
|
||||||
|
|
||||||
extraPlugins = mkOption {
|
extraPlugins = mkOption {
|
||||||
type = with types; coercedTo (listOf path) (path: _ignorePg: path) (functionTo (listOf path));
|
type = with types; coercedTo (listOf path) (path: _ignorePg: path) (functionTo (listOf path));
|
||||||
default = _: [];
|
default = _: [];
|
||||||
@ -373,7 +357,38 @@ in
|
|||||||
};
|
};
|
||||||
|
|
||||||
settings = mkOption {
|
settings = mkOption {
|
||||||
type = with types; attrsOf (oneOf [ bool float int str ]);
|
type = with types; submodule {
|
||||||
|
freeformType = attrsOf (oneOf [ bool float int str ]);
|
||||||
|
options = {
|
||||||
|
shared_preload_libraries = mkOption {
|
||||||
|
type = nullOr (coercedTo (listOf str) (concatStringsSep ", ") str);
|
||||||
|
default = null;
|
||||||
|
example = literalExpression ''[ "auto_explain" "anon" ]'';
|
||||||
|
description = mdDoc ''
|
||||||
|
List of libraries to be preloaded.
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
|
||||||
|
log_line_prefix = mkOption {
|
||||||
|
type = types.str;
|
||||||
|
default = "[%p] ";
|
||||||
|
example = "%m [%p] ";
|
||||||
|
description = lib.mdDoc ''
|
||||||
|
A printf-style string that is output at the beginning of each log line.
|
||||||
|
Upstream default is `'%m [%p] '`, i.e. it includes the timestamp. We do
|
||||||
|
not include the timestamp, because journal has it anyway.
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
|
||||||
|
port = mkOption {
|
||||||
|
type = types.port;
|
||||||
|
default = 5432;
|
||||||
|
description = lib.mdDoc ''
|
||||||
|
The port on which PostgreSQL listens.
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
default = {};
|
default = {};
|
||||||
description = lib.mdDoc ''
|
description = lib.mdDoc ''
|
||||||
PostgreSQL configuration. Refer to
|
PostgreSQL configuration. Refer to
|
||||||
@ -439,9 +454,7 @@ in
|
|||||||
hba_file = "${pkgs.writeText "pg_hba.conf" cfg.authentication}";
|
hba_file = "${pkgs.writeText "pg_hba.conf" cfg.authentication}";
|
||||||
ident_file = "${pkgs.writeText "pg_ident.conf" cfg.identMap}";
|
ident_file = "${pkgs.writeText "pg_ident.conf" cfg.identMap}";
|
||||||
log_destination = "stderr";
|
log_destination = "stderr";
|
||||||
log_line_prefix = cfg.logLinePrefix;
|
|
||||||
listen_addresses = if cfg.enableTCPIP then "*" else "localhost";
|
listen_addresses = if cfg.enableTCPIP then "*" else "localhost";
|
||||||
port = cfg.port;
|
|
||||||
jit = mkDefault (if cfg.enableJIT then "on" else "off");
|
jit = mkDefault (if cfg.enableJIT then "on" else "off");
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -524,7 +537,7 @@ in
|
|||||||
# Wait for PostgreSQL to be ready to accept connections.
|
# Wait for PostgreSQL to be ready to accept connections.
|
||||||
postStart =
|
postStart =
|
||||||
''
|
''
|
||||||
PSQL="psql --port=${toString cfg.port}"
|
PSQL="psql --port=${toString cfg.settings.port}"
|
||||||
|
|
||||||
while ! $PSQL -d postgres -c "" 2> /dev/null; do
|
while ! $PSQL -d postgres -c "" 2> /dev/null; do
|
||||||
if ! kill -0 "$MAINPID"; then exit 1; fi
|
if ! kill -0 "$MAINPID"; then exit 1; fi
|
||||||
|
@ -114,11 +114,11 @@ in
|
|||||||
|
|
||||||
port = mkOption {
|
port = mkOption {
|
||||||
type = types.port;
|
type = types.port;
|
||||||
default = if !usePostgresql then 3306 else pg.port;
|
default = if usePostgresql then pg.settings.port else 3306;
|
||||||
defaultText = literalExpression ''
|
defaultText = literalExpression ''
|
||||||
if config.${opt.database.type} != "postgresql"
|
if config.${opt.database.type} != "postgresql"
|
||||||
then 3306
|
then 3306
|
||||||
else config.${options.services.postgresql.port}
|
else 5432
|
||||||
'';
|
'';
|
||||||
description = mdDoc "Database host port.";
|
description = mdDoc "Database host port.";
|
||||||
};
|
};
|
||||||
|
@ -100,11 +100,11 @@ in
|
|||||||
|
|
||||||
port = mkOption {
|
port = mkOption {
|
||||||
type = types.port;
|
type = types.port;
|
||||||
default = if !usePostgresql then 3306 else pg.port;
|
default = if usePostgresql then pg.settings.port else 3306;
|
||||||
defaultText = literalExpression ''
|
defaultText = literalExpression ''
|
||||||
if config.${opt.database.type} != "postgresql"
|
if config.${opt.database.type} != "postgresql"
|
||||||
then 3306
|
then 3306
|
||||||
else config.${options.services.postgresql.port}
|
else 5432
|
||||||
'';
|
'';
|
||||||
description = lib.mdDoc "Database host port.";
|
description = lib.mdDoc "Database host port.";
|
||||||
};
|
};
|
||||||
|
@ -103,11 +103,11 @@ in
|
|||||||
|
|
||||||
port = mkOption {
|
port = mkOption {
|
||||||
type = types.port;
|
type = types.port;
|
||||||
default = if cfg.database.type == "mysql" then mysql.port else pgsql.port;
|
default = if cfg.database.type == "mysql" then mysql.port else pgsql.services.port;
|
||||||
defaultText = literalExpression ''
|
defaultText = literalExpression ''
|
||||||
if config.${opt.database.type} == "mysql"
|
if config.${opt.database.type} == "mysql"
|
||||||
then config.${options.services.mysql.port}
|
then config.${options.services.mysql.port}
|
||||||
else config.${options.services.postgresql.port}
|
else config.services.postgresql.settings.port
|
||||||
'';
|
'';
|
||||||
description = lib.mdDoc "Database host port.";
|
description = lib.mdDoc "Database host port.";
|
||||||
};
|
};
|
||||||
|
@ -95,11 +95,11 @@ in
|
|||||||
|
|
||||||
port = mkOption {
|
port = mkOption {
|
||||||
type = types.port;
|
type = types.port;
|
||||||
default = if cfg.database.type == "mysql" then mysql.port else pgsql.port;
|
default = if cfg.database.type == "mysql" then mysql.port else pgsql.settings.port;
|
||||||
defaultText = literalExpression ''
|
defaultText = literalExpression ''
|
||||||
if config.${opt.database.type} == "mysql"
|
if config.${opt.database.type} == "mysql"
|
||||||
then config.${options.services.mysql.port}
|
then config.${options.services.mysql.port}
|
||||||
else config.${options.services.postgresql.port}
|
else config.services.postgresql.settings.port
|
||||||
'';
|
'';
|
||||||
description = lib.mdDoc "Database host port.";
|
description = lib.mdDoc "Database host port.";
|
||||||
};
|
};
|
||||||
|
@ -346,8 +346,8 @@ in
|
|||||||
|
|
||||||
port = lib.mkOption {
|
port = lib.mkOption {
|
||||||
type = types.port;
|
type = types.port;
|
||||||
default = options.services.postgresql.port.default;
|
default = config.services.postgresql.settings.port;
|
||||||
defaultText = lib.literalExpression "options.services.postgresql.port.default";
|
defaultText = lib.literalExpression "config.services.postgresql.settings.port";
|
||||||
description = lib.mdDoc ''
|
description = lib.mdDoc ''
|
||||||
The port of the database Invidious should use.
|
The port of the database Invidious should use.
|
||||||
|
|
||||||
|
@ -76,11 +76,11 @@ in
|
|||||||
type = types.port;
|
type = types.port;
|
||||||
default =
|
default =
|
||||||
if cfg.database.type == "mysql" then config.services.mysql.port
|
if cfg.database.type == "mysql" then config.services.mysql.port
|
||||||
else if cfg.database.type == "pgsql" then config.services.postgresql.port
|
else if cfg.database.type == "pgsql" then config.services.postgresql.settings.port
|
||||||
else 1521;
|
else 1521;
|
||||||
defaultText = literalExpression ''
|
defaultText = literalExpression ''
|
||||||
if config.${opt.database.type} == "mysql" then config.${options.services.mysql.port}
|
if config.${opt.database.type} == "mysql" then config.${options.services.mysql.port}
|
||||||
else if config.${opt.database.type} == "pgsql" then config.${options.services.postgresql.port}
|
else if config.${opt.database.type} == "pgsql" then config.services.postgresql.settings.port
|
||||||
else 1521
|
else 1521
|
||||||
'';
|
'';
|
||||||
description = lib.mdDoc "Database host port.";
|
description = lib.mdDoc "Database host port.";
|
||||||
|
@ -18,7 +18,7 @@ import ./make-test-python.nix ({ pkgs, ... }: {
|
|||||||
host invidious invidious samenet scram-sha-256
|
host invidious invidious samenet scram-sha-256
|
||||||
'';
|
'';
|
||||||
};
|
};
|
||||||
networking.firewall.allowedTCPPorts = [ config.services.postgresql.port ];
|
networking.firewall.allowedTCPPorts = [ config.services.postgresql.settings.port ];
|
||||||
};
|
};
|
||||||
machine = { config, lib, pkgs, ... }: {
|
machine = { config, lib, pkgs, ... }: {
|
||||||
services.invidious = {
|
services.invidious = {
|
||||||
|
@ -76,7 +76,7 @@ in
|
|||||||
systemd.services.postgresql.postStart = lib.mkAfter ''
|
systemd.services.postgresql.postStart = lib.mkAfter ''
|
||||||
$PSQL -tAd miniflux -c 'CREATE EXTENSION hstore;'
|
$PSQL -tAd miniflux -c 'CREATE EXTENSION hstore;'
|
||||||
'';
|
'';
|
||||||
networking.firewall.allowedTCPPorts = [ config.services.postgresql.port ];
|
networking.firewall.allowedTCPPorts = [ config.services.postgresql.settings.port ];
|
||||||
};
|
};
|
||||||
externalDb = { ... }: {
|
externalDb = { ... }: {
|
||||||
security.apparmor.enable = true;
|
security.apparmor.enable = true;
|
||||||
|
@ -7,7 +7,7 @@ import ./make-test-python.nix ({ pkgs, lib, ... }: {
|
|||||||
services.postgresql = {
|
services.postgresql = {
|
||||||
enable = true;
|
enable = true;
|
||||||
extraPlugins = ps: [ ps.anonymizer ];
|
extraPlugins = ps: [ ps.anonymizer ];
|
||||||
settings.shared_preload_libraries = "anon";
|
settings.shared_preload_libraries = [ "anon" ];
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -21,7 +21,7 @@ in
|
|||||||
pgmanage = {
|
pgmanage = {
|
||||||
enable = true;
|
enable = true;
|
||||||
connections = {
|
connections = {
|
||||||
${conn} = "hostaddr=127.0.0.1 port=${toString config.services.postgresql.port} dbname=postgres";
|
${conn} = "hostaddr=127.0.0.1 port=${toString config.services.postgresql.settings.port} dbname=postgres";
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
@ -33,7 +33,7 @@ in
|
|||||||
extraHosts = hosts;
|
extraHosts = hosts;
|
||||||
firewall.allowedTCPPorts = [
|
firewall.allowedTCPPorts = [
|
||||||
config.services.redis.servers.mastodon.port
|
config.services.redis.servers.mastodon.port
|
||||||
config.services.postgresql.port
|
config.services.postgresql.settings.port
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user