···
database = "postgres://${cfg.localDatabaseUser}:${cfg.localDatabasePassword}@localhost:5432/${cfg.localDatabaseName}?sslmode=disable&connect_timeout=10";
11
-
mattermostConf = recursiveUpdate
11
+
postgresPackage = config.services.postgresql.package;
14
+
statePath ? cfg.statePath,
15
+
localDatabaseUser ? cfg.localDatabaseUser,
16
+
localDatabasePassword ? cfg.localDatabasePassword,
17
+
localDatabaseName ? cfg.localDatabaseName,
20
+
if ! test -e "${statePath}/.db-created"; then
21
+
${lib.optionalString useSudo "${pkgs.sudo}/bin/sudo -u ${config.services.postgresql.superUser} \\"}
22
+
${postgresPackage}/bin/psql postgres -c \
23
+
"CREATE ROLE ${localDatabaseUser} WITH LOGIN NOCREATEDB NOCREATEROLE ENCRYPTED PASSWORD '${localDatabasePassword}'"
24
+
${lib.optionalString useSudo "${pkgs.sudo}/bin/sudo -u ${config.services.postgresql.superUser} \\"}
25
+
${postgresPackage}/bin/createdb \
26
+
--owner ${localDatabaseUser} ${localDatabaseName}
27
+
touch ${statePath}/.db-created
31
+
mattermostPluginDerivations = with pkgs;
32
+
if cfg.plugins == null then null
33
+
else map (plugin: stdenv.mkDerivation {
34
+
name = "mattermost-plugin";
37
+
cp ${plugin} $out/share/plugin.tar.gz
41
+
dontConfigure = true;
43
+
preferLocalBuild = true;
46
+
mattermostPlugins = with pkgs;
47
+
if cfg.plugins == null then null
48
+
else stdenv.mkDerivation {
49
+
name = "${cfg.package.name}-plugins";
50
+
nativeBuildInputs = [
53
+
] ++ mattermostPluginDerivations;
58
+
# Create a temporary Mattermost install to unpack plugins
59
+
ln -sf ${cfg.package}/{bin,fonts,i18n,templates,client} .
60
+
mkdir -p ./{data,logs}
62
+
# Create the database
64
+
db_socket_dir="$db_dir/run"
65
+
initdb --no-locale --encoding=utf8 --pgdata="$db_dir"
66
+
mkdir -p "$db_socket_dir"
67
+
echo "unix_socket_directories = '$db_socket_dir'" >> db/postgresql.conf
68
+
echo "listen_addresses = '''" >> db/postgresql.conf
71
+
pg_ctl start --pgdata="$db_dir"
73
+
pg_ctl stop --pgdata="$db_dir"
77
+
# Create the Mattermost user
78
+
export PGHOST="$db_socket_dir"
81
+
localDatabaseUser = "mattermost";
82
+
localDatabasePassword = "mattermost";
83
+
localDatabaseName = "mattermost";
87
+
# Create destination paths for client and server plugins
88
+
mkdir -p $out/client $out/server
90
+
# Create the Mattermost config
92
+
--arg serverPlugins $out/server \
93
+
--arg clientPlugins $out/client \
94
+
--arg socketDir "$db_socket_dir" \ '
96
+
.PluginSettings.Directory = $serverPlugins |
97
+
.PluginSettings.ClientDirectory = $clientPlugins |
98
+
.SqlSettings.DriverName = "postgres" |
99
+
.SqlSettings.DataSource = "postgres://mattermost:mattermost@/mattermost?host=" + $socketDir
100
+
' ${cfg.package}/config/config.json > $out/config.json
103
+
plugins=(${escapeShellArgs (map (plugin: "${plugin}/share/plugin.tar.gz") mattermostPluginDerivations)})
104
+
if [ ''${#plugins[@]} -gt 0 ]; then
105
+
mattermost plugin add "''${plugins[@]}" --config $out/config.json
111
+
dontConfigure = true;
113
+
preferLocalBuild = true;
116
+
mattermostConfWithoutPlugins = recursiveUpdate
{ ServiceSettings.SiteURL = cfg.siteUrl;
ServiceSettings.ListenAddress = cfg.listenAddress;
TeamSettings.SiteName = cfg.siteName;
···
125
+
mattermostConf = recursiveUpdate
126
+
mattermostConfWithoutPlugins
128
+
if mattermostPlugins == null then {}
132
+
EnableUploads = false;
133
+
Directory = "${mattermostPlugins}/server";
134
+
ClientDirectory = "${mattermostPlugins}/client";
mattermostConfJSON = pkgs.writeText "mattermost-config.json" (builtins.toJSON mattermostConf);
···
enable = mkEnableOption "Mattermost chat server";
148
+
package = mkOption {
149
+
type = types.package;
150
+
default = pkgs.mattermost;
151
+
defaultText = "pkgs.mattermost";
152
+
description = "Mattermost derivation to use.";
···
219
+
plugins = mkOption {
220
+
type = types.nullOr (types.listOf (types.oneOf [types.path types.package]));
222
+
example = "[ ./com.github.moussetc.mattermost.plugin.giphy-2.0.0.tar.gz ]";
224
+
Plugins to add to the configuration. Overrides any installed if non-null.
225
+
This is a list of paths to .tar.gz files or derivations evaluating to
226
+
.tar.gz files. All entries will be passed to `mattermost plugin add`.
localDatabaseCreate = mkOption {
···
enable = mkEnableOption "Mattermost IRC bridge";
280
+
package = mkOption {
281
+
type = types.package;
282
+
default = pkgs.matterircd;
283
+
defaultText = "pkgs.matterircd";
284
+
description = "matterircd derivation to use.";
type = types.listOf types.str;
···
mkdir -p ${cfg.statePath}/{data,config,logs}
185
-
ln -sf ${pkgs.mattermost}/{bin,fonts,i18n,templates,client} ${cfg.statePath}
328
+
ln -sf ${cfg.package}/{bin,fonts,i18n,templates,client} ${cfg.statePath}
'' + lib.optionalString (!cfg.mutableConfig) ''
rm -f ${cfg.statePath}/config/config.json
188
-
${pkgs.jq}/bin/jq -s '.[0] * .[1]' ${pkgs.mattermost}/config/config.json ${mattermostConfJSON} > ${cfg.statePath}/config/config.json
189
-
${pkgs.mattermost}/bin/mattermost config migrate ${cfg.statePath}/config/config.json ${database}
331
+
${pkgs.jq}/bin/jq -s '.[0] * .[1]' ${cfg.package}/config/config.json ${mattermostConfJSON} > ${cfg.statePath}/config/config.json
332
+
${cfg.package}/bin/mattermost config migrate ${cfg.statePath}/config/config.json ${database}
'' + lib.optionalString cfg.mutableConfig ''
if ! test -e "${cfg.statePath}/config/.initial-created"; then
rm -f ${cfg.statePath}/config/config.json
193
-
${pkgs.jq}/bin/jq -s '.[0] * .[1]' ${pkgs.mattermost}/config/config.json ${mattermostConfJSON} > ${cfg.statePath}/config/config.json
336
+
${pkgs.jq}/bin/jq -s '.[0] * .[1]' ${cfg.package}/config/config.json ${mattermostConfJSON} > ${cfg.statePath}/config/config.json
touch ${cfg.statePath}/config/.initial-created
'' + lib.optionalString (cfg.mutableConfig && cfg.preferNixConfig) ''
···
rm -f ${cfg.statePath}/config/config.json
echo "$newConfig" > ${cfg.statePath}/config/config.json
201
-
'' + lib.optionalString cfg.localDatabaseCreate ''
202
-
if ! test -e "${cfg.statePath}/.db-created"; then
203
-
${pkgs.sudo}/bin/sudo -u ${config.services.postgresql.superUser} \
204
-
${config.services.postgresql.package}/bin/psql postgres -c \
205
-
"CREATE ROLE ${cfg.localDatabaseUser} WITH LOGIN NOCREATEDB NOCREATEROLE ENCRYPTED PASSWORD '${cfg.localDatabasePassword}'"
206
-
${pkgs.sudo}/bin/sudo -u ${config.services.postgresql.superUser} \
207
-
${config.services.postgresql.package}/bin/createdb \
208
-
--owner ${cfg.localDatabaseUser} ${cfg.localDatabaseName}
209
-
touch ${cfg.statePath}/.db-created
344
+
'' + lib.optionalString cfg.localDatabaseCreate (createDb {}) + ''
chown ${cfg.user}:${cfg.group} -R ${cfg.statePath}
chmod u+rw,g+r,o-rwx -R ${cfg.statePath}
···
PermissionsStartOnly = true;
220
-
ExecStart = "${pkgs.mattermost}/bin/mattermost" +
221
-
(lib.optionalString (!cfg.mutableConfig) " -c ${database}");
353
+
ExecStart = "${cfg.package}/bin/mattermost " + (
355
+
(lib.optionals (!cfg.mutableConfig) ["-c" database])
WorkingDirectory = "${cfg.statePath}";
···
237
-
ExecStart = "${pkgs.matterircd}/bin/matterircd ${concatStringsSep " " cfg.matterircd.parameters}";
372
+
ExecStart = "${cfg.matterircd.package}/bin/matterircd ${escapeShellArgs cfg.matterircd.parameters}";
WorkingDirectory = "/tmp";