Skip to content

Commit 6e0b3be

Browse files
matrix: add configurePostgres option
1 parent b741c38 commit 6e0b3be

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

modules/matrix.nix

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ in
1111
services.matrix-synapse = {
1212
addAdditionalOembedProvider = libS.mkOpinionatedOption "add additional oembed providers from oembed.com";
1313

14+
configurePostgres = lib.mkOption {
15+
type = lib.types.bool;
16+
default = false;
17+
example = true;
18+
description = "Whether to configure and create a local PostgreSQL database.";
19+
};
20+
1421
domain = lib.mkOption {
1522
type = lib.types.str;
1623
example = "matrix.example.com";
@@ -198,12 +205,24 @@ in
198205
];
199206
};
200207

208+
services.postgresql = lib.mkIf cfg.configurePostgres {
209+
databases = [ "matrix-synapse" ]; # some parts of nixos-modules read this field to know all databases
210+
};
211+
201212
services.portunus.seedSettings.groups = lib.mkIf (cfgl.userGroup != null) [ {
202213
long_name = "Matrix Users";
203214
name = cfgl.userGroup;
204215
permissions = { };
205216
} ];
206217

218+
systemd.services = lib.mkIf cfg.configurePostgres {
219+
# https://element-hq.github.io/synapse/latest/postgres.html#set-up-database
220+
# https://github.com/NixOS/nixpkgs/blob/nixos-unstable/nixos/modules/services/databases/postgresql.nix#L655
221+
postgresql.postStart = ''
222+
$PSQL -tAc "SELECT 1 FROM pg_database WHERE datname = 'matrix-synapse'" | grep -q 1 || $PSQL -tAc 'CREATE DATABASE "matrix-synapse" ENCODING="UTF8" LOCALE="C" TEMPLATE="template0" OWNER="matrix-synapse"'
223+
'';
224+
};
225+
207226
users.users = lib.mkIf cfg.listenOnSocket {
208227
nginx.extraGroups = [ "matrix-synapse" ];
209228
};

0 commit comments

Comments
 (0)