2016-08-15 02:17:53 +01:00
|
|
|
{ config, pkgs, lib, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
cfg = config.services.mattermost;
|
|
|
|
|
2019-09-20 18:39:57 +01:00
|
|
|
database = "postgres://${cfg.localDatabaseUser}:${cfg.localDatabasePassword}@localhost:5432/${cfg.localDatabaseName}?sslmode=disable&connect_timeout=10";
|
2016-08-15 02:17:53 +01:00
|
|
|
|
2021-04-18 03:46:24 +01:00
|
|
|
postgresPackage = config.services.postgresql.package;
|
|
|
|
|
|
|
|
createDb = {
|
|
|
|
statePath ? cfg.statePath,
|
|
|
|
localDatabaseUser ? cfg.localDatabaseUser,
|
|
|
|
localDatabasePassword ? cfg.localDatabasePassword,
|
|
|
|
localDatabaseName ? cfg.localDatabaseName,
|
|
|
|
useSudo ? true
|
|
|
|
}: ''
|
2021-04-19 05:11:58 +01:00
|
|
|
if ! test -e ${escapeShellArg "${statePath}/.db-created"}; then
|
|
|
|
${lib.optionalString useSudo "${pkgs.sudo}/bin/sudo -u ${escapeShellArg config.services.postgresql.superUser} \\"}
|
2021-04-18 03:46:24 +01:00
|
|
|
${postgresPackage}/bin/psql postgres -c \
|
|
|
|
"CREATE ROLE ${localDatabaseUser} WITH LOGIN NOCREATEDB NOCREATEROLE ENCRYPTED PASSWORD '${localDatabasePassword}'"
|
2021-04-19 05:11:58 +01:00
|
|
|
${lib.optionalString useSudo "${pkgs.sudo}/bin/sudo -u ${escapeShellArg config.services.postgresql.superUser} \\"}
|
2021-04-18 03:46:24 +01:00
|
|
|
${postgresPackage}/bin/createdb \
|
2021-04-19 05:11:58 +01:00
|
|
|
--owner ${escapeShellArg localDatabaseUser} ${escapeShellArg localDatabaseName}
|
|
|
|
touch ${escapeShellArg "${statePath}/.db-created"}
|
2021-04-18 03:46:24 +01:00
|
|
|
fi
|
|
|
|
'';
|
|
|
|
|
|
|
|
mattermostPluginDerivations = with pkgs;
|
2021-07-06 16:37:10 +01:00
|
|
|
map (plugin: stdenv.mkDerivation {
|
2021-04-18 03:46:24 +01:00
|
|
|
name = "mattermost-plugin";
|
|
|
|
installPhase = ''
|
|
|
|
mkdir -p $out/share
|
|
|
|
cp ${plugin} $out/share/plugin.tar.gz
|
|
|
|
'';
|
|
|
|
dontUnpack = true;
|
|
|
|
dontPatch = true;
|
|
|
|
dontConfigure = true;
|
|
|
|
dontBuild = true;
|
|
|
|
preferLocalBuild = true;
|
|
|
|
}) cfg.plugins;
|
|
|
|
|
|
|
|
mattermostPlugins = with pkgs;
|
2021-07-06 16:37:10 +01:00
|
|
|
if mattermostPluginDerivations == [] then null
|
2021-04-18 03:46:24 +01:00
|
|
|
else stdenv.mkDerivation {
|
|
|
|
name = "${cfg.package.name}-plugins";
|
|
|
|
nativeBuildInputs = [
|
|
|
|
autoPatchelfHook
|
|
|
|
] ++ mattermostPluginDerivations;
|
|
|
|
buildInputs = [
|
|
|
|
cfg.package
|
|
|
|
];
|
|
|
|
installPhase = ''
|
2021-04-19 05:11:58 +01:00
|
|
|
mkdir -p $out/data/plugins
|
2021-04-18 03:46:24 +01:00
|
|
|
plugins=(${escapeShellArgs (map (plugin: "${plugin}/share/plugin.tar.gz") mattermostPluginDerivations)})
|
2021-04-19 05:11:58 +01:00
|
|
|
for plugin in "''${plugins[@]}"; do
|
|
|
|
hash="$(sha256sum "$plugin" | cut -d' ' -f1)"
|
|
|
|
mkdir -p "$hash"
|
|
|
|
tar -C "$hash" -xzf "$plugin"
|
|
|
|
autoPatchelf "$hash"
|
|
|
|
GZIP_OPT=-9 tar -C "$hash" -cvzf "$out/data/plugins/$hash.tar.gz" .
|
|
|
|
rm -rf "$hash"
|
|
|
|
done
|
2021-04-18 03:46:24 +01:00
|
|
|
'';
|
|
|
|
|
|
|
|
dontUnpack = true;
|
|
|
|
dontPatch = true;
|
|
|
|
dontConfigure = true;
|
|
|
|
dontBuild = true;
|
|
|
|
preferLocalBuild = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
mattermostConfWithoutPlugins = recursiveUpdate
|
2021-04-12 02:29:29 +01:00
|
|
|
{ ServiceSettings.SiteURL = cfg.siteUrl;
|
|
|
|
ServiceSettings.ListenAddress = cfg.listenAddress;
|
|
|
|
TeamSettings.SiteName = cfg.siteName;
|
|
|
|
SqlSettings.DriverName = "postgres";
|
|
|
|
SqlSettings.DataSource = database;
|
|
|
|
}
|
|
|
|
cfg.extraConfig;
|
2016-08-15 02:17:53 +01:00
|
|
|
|
2021-04-18 03:46:24 +01:00
|
|
|
mattermostConf = recursiveUpdate
|
|
|
|
mattermostConfWithoutPlugins
|
|
|
|
(
|
|
|
|
if mattermostPlugins == null then {}
|
|
|
|
else {
|
|
|
|
PluginSettings = {
|
|
|
|
Enable = true;
|
|
|
|
EnableUploads = false;
|
2021-04-19 05:11:58 +01:00
|
|
|
Directory = "${cfg.statePath}/plugins/server";
|
|
|
|
ClientDirectory = "${cfg.statePath}/plugins/client";
|
2021-04-18 03:46:24 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2021-04-12 02:29:29 +01:00
|
|
|
mattermostConfJSON = pkgs.writeText "mattermost-config.json" (builtins.toJSON mattermostConf);
|
2016-08-15 02:17:53 +01:00
|
|
|
|
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
options = {
|
|
|
|
services.mattermost = {
|
2018-06-20 01:12:13 +01:00
|
|
|
enable = mkEnableOption "Mattermost chat server";
|
2016-08-15 02:17:53 +01:00
|
|
|
|
2021-04-18 03:46:24 +01:00
|
|
|
package = mkOption {
|
|
|
|
type = types.package;
|
|
|
|
default = pkgs.mattermost;
|
|
|
|
defaultText = "pkgs.mattermost";
|
|
|
|
description = "Mattermost derivation to use.";
|
|
|
|
};
|
|
|
|
|
2016-08-15 02:17:53 +01:00
|
|
|
statePath = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "/var/lib/mattermost";
|
|
|
|
description = "Mattermost working directory";
|
|
|
|
};
|
|
|
|
|
|
|
|
siteUrl = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
example = "https://chat.example.com";
|
|
|
|
description = ''
|
2016-08-21 18:20:10 +01:00
|
|
|
URL this Mattermost instance is reachable under, without trailing slash.
|
2016-08-15 02:17:53 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
siteName = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "Mattermost";
|
|
|
|
description = "Name of this Mattermost site.";
|
|
|
|
};
|
|
|
|
|
|
|
|
listenAddress = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = ":8065";
|
|
|
|
example = "[::1]:8065";
|
|
|
|
description = ''
|
|
|
|
Address and port this Mattermost instance listens to.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
mutableConfig = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
Whether the Mattermost config.json is writeable by Mattermost.
|
|
|
|
|
|
|
|
Most of the settings can be edited in the system console of
|
|
|
|
Mattermost if this option is enabled. A template config using
|
|
|
|
the options specified in services.mattermost will be generated
|
|
|
|
but won't be overwritten on changes or rebuilds.
|
|
|
|
|
|
|
|
If this option is disabled, changes in the system console won't
|
|
|
|
be possible (default). If an config.json is present, it will be
|
|
|
|
overwritten!
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2021-04-12 02:29:29 +01:00
|
|
|
preferNixConfig = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
If both mutableConfig and this option are set, the Nix configuration
|
|
|
|
will take precedence over any settings configured in the server
|
|
|
|
console.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2016-08-15 02:17:53 +01:00
|
|
|
extraConfig = mkOption {
|
|
|
|
type = types.attrs;
|
|
|
|
default = { };
|
|
|
|
description = ''
|
|
|
|
Addtional configuration options as Nix attribute set in config.json schema.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2021-04-18 03:46:24 +01:00
|
|
|
plugins = mkOption {
|
2021-07-06 16:37:10 +01:00
|
|
|
type = types.listOf (types.oneOf [types.path types.package]);
|
|
|
|
default = [];
|
2021-04-18 03:46:24 +01:00
|
|
|
example = "[ ./com.github.moussetc.mattermost.plugin.giphy-2.0.0.tar.gz ]";
|
|
|
|
description = ''
|
|
|
|
Plugins to add to the configuration. Overrides any installed if non-null.
|
|
|
|
This is a list of paths to .tar.gz files or derivations evaluating to
|
|
|
|
.tar.gz files. All entries will be passed to `mattermost plugin add`.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2016-08-15 02:17:53 +01:00
|
|
|
localDatabaseCreate = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
|
|
|
description = ''
|
|
|
|
Create a local PostgreSQL database for Mattermost automatically.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
localDatabaseName = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "mattermost";
|
|
|
|
description = ''
|
|
|
|
Local Mattermost database name.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
localDatabaseUser = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "mattermost";
|
|
|
|
description = ''
|
|
|
|
Local Mattermost database username.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
localDatabasePassword = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "mmpgsecret";
|
|
|
|
description = ''
|
|
|
|
Password for local Mattermost database user.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
user = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "mattermost";
|
|
|
|
description = ''
|
|
|
|
User which runs the Mattermost service.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
group = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "mattermost";
|
|
|
|
description = ''
|
|
|
|
Group which runs the Mattermost service.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
matterircd = {
|
|
|
|
enable = mkEnableOption "Mattermost IRC bridge";
|
2021-04-18 03:46:24 +01:00
|
|
|
package = mkOption {
|
|
|
|
type = types.package;
|
|
|
|
default = pkgs.matterircd;
|
|
|
|
defaultText = "pkgs.matterircd";
|
|
|
|
description = "matterircd derivation to use.";
|
|
|
|
};
|
2016-08-15 02:17:53 +01:00
|
|
|
parameters = mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
default = [ ];
|
|
|
|
example = [ "-mmserver chat.example.com" "-bind [::]:6667" ];
|
|
|
|
description = ''
|
|
|
|
Set commandline parameters to pass to matterircd. See
|
|
|
|
https://github.com/42wim/matterircd#usage for more information.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkMerge [
|
|
|
|
(mkIf cfg.enable {
|
2019-09-14 18:51:29 +01:00
|
|
|
users.users = optionalAttrs (cfg.user == "mattermost") {
|
|
|
|
mattermost = {
|
|
|
|
group = cfg.group;
|
|
|
|
uid = config.ids.uids.mattermost;
|
|
|
|
home = cfg.statePath;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
users.groups = optionalAttrs (cfg.group == "mattermost") {
|
|
|
|
mattermost.gid = config.ids.gids.mattermost;
|
|
|
|
};
|
2016-08-15 02:17:53 +01:00
|
|
|
|
|
|
|
services.postgresql.enable = cfg.localDatabaseCreate;
|
|
|
|
|
|
|
|
# The systemd service will fail to execute the preStart hook
|
|
|
|
# if the WorkingDirectory does not exist
|
|
|
|
system.activationScripts.mattermost = ''
|
|
|
|
mkdir -p ${cfg.statePath}
|
|
|
|
'';
|
|
|
|
|
|
|
|
systemd.services.mattermost = {
|
2018-06-20 01:12:13 +01:00
|
|
|
description = "Mattermost chat service";
|
2016-08-15 02:17:53 +01:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
after = [ "network.target" "postgresql.service" ];
|
|
|
|
|
|
|
|
preStart = ''
|
|
|
|
mkdir -p ${cfg.statePath}/{data,config,logs}
|
2021-04-18 03:46:24 +01:00
|
|
|
ln -sf ${cfg.package}/{bin,fonts,i18n,templates,client} ${cfg.statePath}
|
2021-04-19 05:11:58 +01:00
|
|
|
'' + lib.optionalString (mattermostPlugins != null) ''
|
|
|
|
mkdir -p ${cfg.statePath}/plugins/{client,server}
|
|
|
|
rm -rf "${cfg.statePath}/data/plugins"
|
|
|
|
ln -sf ${mattermostPlugins}/data/plugins ${cfg.statePath}/data
|
2016-08-15 02:17:53 +01:00
|
|
|
'' + lib.optionalString (!cfg.mutableConfig) ''
|
2019-09-20 18:39:57 +01:00
|
|
|
rm -f ${cfg.statePath}/config/config.json
|
2021-04-18 03:46:24 +01:00
|
|
|
${pkgs.jq}/bin/jq -s '.[0] * .[1]' ${cfg.package}/config/config.json ${mattermostConfJSON} > ${cfg.statePath}/config/config.json
|
|
|
|
${cfg.package}/bin/mattermost config migrate ${cfg.statePath}/config/config.json ${database}
|
2016-08-15 02:17:53 +01:00
|
|
|
'' + lib.optionalString cfg.mutableConfig ''
|
|
|
|
if ! test -e "${cfg.statePath}/config/.initial-created"; then
|
|
|
|
rm -f ${cfg.statePath}/config/config.json
|
2021-04-18 03:46:24 +01:00
|
|
|
${pkgs.jq}/bin/jq -s '.[0] * .[1]' ${cfg.package}/config/config.json ${mattermostConfJSON} > ${cfg.statePath}/config/config.json
|
2016-08-15 02:17:53 +01:00
|
|
|
touch ${cfg.statePath}/config/.initial-created
|
|
|
|
fi
|
2021-04-12 02:29:29 +01:00
|
|
|
'' + lib.optionalString (cfg.mutableConfig && cfg.preferNixConfig) ''
|
2021-04-19 05:11:58 +01:00
|
|
|
new_config="$(${pkgs.jq}/bin/jq -s '.[0] * .[1]' ${cfg.statePath}/config/config.json ${mattermostConfJSON})"
|
2021-04-12 02:29:29 +01:00
|
|
|
|
|
|
|
rm -f ${cfg.statePath}/config/config.json
|
2021-04-19 05:11:58 +01:00
|
|
|
echo "$new_config" > ${cfg.statePath}/config/config.json
|
2021-04-18 03:46:24 +01:00
|
|
|
'' + lib.optionalString cfg.localDatabaseCreate (createDb {}) + ''
|
2021-07-06 16:37:10 +01:00
|
|
|
# Don't change permissions recursively on the data, current, and symlinked directories (see ln -sf command above).
|
2021-04-19 05:11:58 +01:00
|
|
|
# This dramatically increases startup times for installations with a lot of files.
|
2021-07-06 16:37:10 +01:00
|
|
|
find . -maxdepth 1 -not -name data -not -name client -not -name templates -not -name i18n -not -name fonts -not -name bin -not -name . \
|
|
|
|
-exec chown ${cfg.user}:${cfg.group} -R {} \; -exec chmod u+rw,g+r,o-rwx -R {} \;
|
|
|
|
|
2021-04-19 05:11:58 +01:00
|
|
|
chown ${cfg.user}:${cfg.group} ${cfg.statePath}/data .
|
|
|
|
chmod u+rw,g+r,o-rwx ${cfg.statePath}/data .
|
2016-08-15 02:17:53 +01:00
|
|
|
'';
|
|
|
|
|
|
|
|
serviceConfig = {
|
|
|
|
PermissionsStartOnly = true;
|
|
|
|
User = cfg.user;
|
|
|
|
Group = cfg.group;
|
2021-04-18 03:46:24 +01:00
|
|
|
ExecStart = "${cfg.package}/bin/mattermost " + (
|
|
|
|
escapeShellArgs
|
|
|
|
(lib.optionals (!cfg.mutableConfig) ["-c" database])
|
|
|
|
);
|
2016-08-15 02:17:53 +01:00
|
|
|
WorkingDirectory = "${cfg.statePath}";
|
|
|
|
Restart = "always";
|
|
|
|
RestartSec = "10";
|
|
|
|
LimitNOFILE = "49152";
|
|
|
|
};
|
2018-06-11 17:47:01 +01:00
|
|
|
unitConfig.JoinsNamespaceOf = mkIf cfg.localDatabaseCreate "postgresql.service";
|
2016-08-15 02:17:53 +01:00
|
|
|
};
|
|
|
|
})
|
|
|
|
(mkIf cfg.matterircd.enable {
|
|
|
|
systemd.services.matterircd = {
|
|
|
|
description = "Mattermost IRC bridge service";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
serviceConfig = {
|
|
|
|
User = "nobody";
|
|
|
|
Group = "nogroup";
|
2021-04-18 03:46:24 +01:00
|
|
|
ExecStart = "${cfg.matterircd.package}/bin/matterircd ${escapeShellArgs cfg.matterircd.parameters}";
|
2016-08-15 02:17:53 +01:00
|
|
|
WorkingDirectory = "/tmp";
|
|
|
|
PrivateTmp = true;
|
|
|
|
Restart = "always";
|
|
|
|
RestartSec = "5";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
})
|
|
|
|
];
|
|
|
|
}
|