2022-01-26 13:01:47 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
2022-02-16 01:33:10 +01:00
|
|
|
with lib;
|
|
|
|
|
2022-01-26 13:01:47 +01:00
|
|
|
let
|
|
|
|
cfg = config.services.zammad;
|
2022-02-16 05:33:10 +01:00
|
|
|
settingsFormat = pkgs.formats.yaml { };
|
2022-02-16 06:49:51 +01:00
|
|
|
filterNull = filterAttrs (_: v: v != null);
|
2022-01-26 13:01:47 +01:00
|
|
|
serviceConfig = {
|
|
|
|
Type = "simple";
|
|
|
|
Restart = "always";
|
|
|
|
|
|
|
|
User = "zammad";
|
|
|
|
Group = "zammad";
|
|
|
|
PrivateTmp = true;
|
2022-02-16 01:33:10 +01:00
|
|
|
StateDirectory = "zammad";
|
2022-01-26 13:01:47 +01:00
|
|
|
WorkingDirectory = cfg.dataDir;
|
|
|
|
};
|
2022-02-16 05:33:10 +01:00
|
|
|
environment = {
|
2022-01-26 13:01:47 +01:00
|
|
|
RAILS_ENV = "production";
|
|
|
|
NODE_ENV = "production";
|
2022-02-16 22:50:22 +01:00
|
|
|
RAILS_SERVE_STATIC_FILES = "true";
|
|
|
|
RAILS_LOG_TO_STDOUT = "true";
|
2022-01-26 13:01:47 +01:00
|
|
|
};
|
2022-02-16 05:33:10 +01:00
|
|
|
databaseConfig = settingsFormat.generate "database.yml" cfg.database.settings;
|
2022-02-16 22:50:22 +01:00
|
|
|
in
|
|
|
|
{
|
2022-01-26 13:01:47 +01:00
|
|
|
|
|
|
|
options = {
|
|
|
|
services.zammad = {
|
2022-02-16 01:33:10 +01:00
|
|
|
enable = mkEnableOption "Zammad, a web-based, open source user support/ticketing solution.";
|
2022-01-26 13:01:47 +01:00
|
|
|
|
2022-02-16 01:33:10 +01:00
|
|
|
package = mkOption {
|
|
|
|
type = types.package;
|
2022-01-26 13:01:47 +01:00
|
|
|
default = pkgs.zammad;
|
2022-02-16 01:33:10 +01:00
|
|
|
defaultText = literalExpression "pkgs.zammad";
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc "Zammad package to use.";
|
2022-01-26 13:01:47 +01:00
|
|
|
};
|
|
|
|
|
2022-02-16 01:33:10 +01:00
|
|
|
dataDir = mkOption {
|
|
|
|
type = types.path;
|
2022-01-26 13:01:47 +01:00
|
|
|
default = "/var/lib/zammad";
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc ''
|
2022-01-26 13:01:47 +01:00
|
|
|
Path to a folder that will contain Zammad working directory.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2022-02-16 01:33:10 +01:00
|
|
|
host = mkOption {
|
|
|
|
type = types.str;
|
2022-01-26 13:01:47 +01:00
|
|
|
default = "127.0.0.1";
|
|
|
|
example = "192.168.23.42";
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc "Host address.";
|
2022-01-26 13:01:47 +01:00
|
|
|
};
|
|
|
|
|
2022-02-16 01:33:10 +01:00
|
|
|
openPorts = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc "Whether to open firewall ports for Zammad";
|
2022-02-16 01:33:10 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
port = mkOption {
|
|
|
|
type = types.port;
|
2022-01-26 13:01:47 +01:00
|
|
|
default = 3000;
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc "Web service port.";
|
2022-01-26 13:01:47 +01:00
|
|
|
};
|
|
|
|
|
2022-02-16 01:33:10 +01:00
|
|
|
websocketPort = mkOption {
|
|
|
|
type = types.port;
|
2022-01-26 13:01:47 +01:00
|
|
|
default = 6042;
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc "Websocket service port.";
|
2022-01-26 13:01:47 +01:00
|
|
|
};
|
|
|
|
|
2022-02-16 05:33:10 +01:00
|
|
|
database = {
|
|
|
|
type = mkOption {
|
|
|
|
type = types.enum [ "PostgreSQL" "MySQL" ];
|
|
|
|
default = "PostgreSQL";
|
|
|
|
example = "MySQL";
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc "Database engine to use.";
|
2022-02-16 05:33:10 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
host = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = {
|
|
|
|
PostgreSQL = "/run/postgresql";
|
|
|
|
MySQL = "localhost";
|
|
|
|
}.${cfg.database.type};
|
2022-02-23 22:19:10 +01:00
|
|
|
defaultText = literalExpression ''
|
|
|
|
{
|
|
|
|
PostgreSQL = "/run/postgresql";
|
|
|
|
MySQL = "localhost";
|
|
|
|
}.''${config.services.zammad.database.type};
|
|
|
|
'';
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc ''
|
2022-02-16 05:33:10 +01:00
|
|
|
Database host address.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
port = mkOption {
|
|
|
|
type = types.nullOr types.port;
|
|
|
|
default = null;
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc "Database port. Use `null` for default port.";
|
2022-02-16 05:33:10 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
name = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "zammad";
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc ''
|
2022-02-16 05:33:10 +01:00
|
|
|
Database name.
|
|
|
|
'';
|
|
|
|
};
|
2022-01-26 13:01:47 +01:00
|
|
|
|
2022-02-16 05:33:10 +01:00
|
|
|
user = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = "zammad";
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc "Database user.";
|
2022-02-16 05:33:10 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
passwordFile = mkOption {
|
|
|
|
type = types.nullOr types.path;
|
|
|
|
default = null;
|
|
|
|
example = "/run/keys/zammad-dbpassword";
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
A file containing the password for {option}`services.zammad.database.user`.
|
2022-02-16 05:33:10 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
createLocally = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
2022-07-28 23:19:15 +02:00
|
|
|
description = lib.mdDoc "Whether to create a local database automatically.";
|
2022-02-16 05:33:10 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
settings = mkOption {
|
|
|
|
type = settingsFormat.type;
|
2022-02-16 22:50:22 +01:00
|
|
|
default = { };
|
2022-02-16 05:33:10 +01:00
|
|
|
example = literalExpression ''
|
|
|
|
{
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
description = ''
|
|
|
|
The <filename>database.yml</filename> configuration file as key value set.
|
|
|
|
See <link xlink:href='TODO' />
|
|
|
|
for list of configuration parameters.
|
|
|
|
'';
|
|
|
|
};
|
2022-01-26 13:01:47 +01:00
|
|
|
};
|
|
|
|
|
2022-02-16 06:49:51 +01:00
|
|
|
secretKeyBaseFile = mkOption {
|
2022-02-16 05:33:10 +01:00
|
|
|
type = types.nullOr types.path;
|
2022-01-26 13:01:47 +01:00
|
|
|
default = null;
|
2022-02-16 06:49:51 +01:00
|
|
|
example = "/run/keys/secret_key_base";
|
2022-01-26 13:01:47 +01:00
|
|
|
description = ''
|
2022-02-16 06:49:51 +01:00
|
|
|
The path to a file containing the
|
|
|
|
<literal>secret_key_base</literal> secret.
|
|
|
|
|
|
|
|
Zammad uses <literal>secret_key_base</literal> to encrypt
|
|
|
|
the cookie store, which contains session data, and to digest
|
|
|
|
user auth tokens.
|
|
|
|
|
|
|
|
Needs to be a 64 byte long string of hexadecimal
|
|
|
|
characters. You can generate one by running
|
|
|
|
|
|
|
|
<screen>
|
|
|
|
<prompt>$ </prompt>openssl rand -hex 64 >/path/to/secret_key_base_file
|
|
|
|
</screen>
|
|
|
|
|
|
|
|
This should be a string, not a nix path, since nix paths are
|
|
|
|
copied into the world-readable nix store.
|
2022-01-26 13:01:47 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-02-16 01:33:10 +01:00
|
|
|
config = mkIf cfg.enable {
|
2022-01-26 13:01:47 +01:00
|
|
|
|
2022-02-16 05:33:10 +01:00
|
|
|
services.zammad.database.settings = {
|
2022-02-16 06:49:51 +01:00
|
|
|
production = mapAttrs (_: v: mkDefault v) (filterNull {
|
2022-02-16 05:33:10 +01:00
|
|
|
adapter = {
|
|
|
|
PostgreSQL = "postgresql";
|
|
|
|
MySQL = "mysql2";
|
|
|
|
}.${cfg.database.type};
|
|
|
|
database = cfg.database.name;
|
|
|
|
pool = 50;
|
|
|
|
timeout = 5000;
|
|
|
|
encoding = "utf8";
|
|
|
|
username = cfg.database.user;
|
|
|
|
host = cfg.database.host;
|
2022-02-16 06:49:51 +01:00
|
|
|
port = cfg.database.port;
|
2022-02-16 05:33:10 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-02-16 01:33:10 +01:00
|
|
|
networking.firewall.allowedTCPPorts = mkIf cfg.openPorts [
|
2022-01-26 13:01:47 +01:00
|
|
|
config.services.zammad.port
|
|
|
|
config.services.zammad.websocketPort
|
|
|
|
];
|
|
|
|
|
|
|
|
users.users.zammad = {
|
|
|
|
isSystemUser = true;
|
|
|
|
home = cfg.dataDir;
|
|
|
|
group = "zammad";
|
|
|
|
};
|
|
|
|
|
2022-02-16 22:50:22 +01:00
|
|
|
users.groups.zammad = { };
|
2022-01-26 13:01:47 +01:00
|
|
|
|
2022-02-16 05:33:10 +01:00
|
|
|
assertions = [
|
2022-02-16 22:50:22 +01:00
|
|
|
{
|
|
|
|
assertion = cfg.database.createLocally -> cfg.database.user == "zammad";
|
2022-02-16 05:33:10 +01:00
|
|
|
message = "services.zammad.database.user must be set to \"zammad\" if services.zammad.database.createLocally is set to true";
|
|
|
|
}
|
2022-02-16 22:50:22 +01:00
|
|
|
{
|
|
|
|
assertion = cfg.database.createLocally -> cfg.database.passwordFile == null;
|
2022-02-16 05:33:10 +01:00
|
|
|
message = "a password cannot be specified if services.zammad.database.createLocally is set to true";
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
services.mysql = optionalAttrs (cfg.database.createLocally && cfg.database.type == "MySQL") {
|
2022-01-26 13:01:47 +01:00
|
|
|
enable = true;
|
2022-02-16 05:33:10 +01:00
|
|
|
package = mkDefault pkgs.mariadb;
|
|
|
|
ensureDatabases = [ cfg.database.name ];
|
2022-01-26 13:01:47 +01:00
|
|
|
ensureUsers = [
|
2022-02-16 22:50:22 +01:00
|
|
|
{
|
|
|
|
name = cfg.database.user;
|
2022-02-16 05:33:10 +01:00
|
|
|
ensurePermissions = { "${cfg.database.name}.*" = "ALL PRIVILEGES"; };
|
2022-01-26 13:01:47 +01:00
|
|
|
}
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
2022-02-16 05:33:10 +01:00
|
|
|
services.postgresql = optionalAttrs (cfg.database.createLocally && cfg.database.type == "PostgreSQL") {
|
|
|
|
enable = true;
|
|
|
|
ensureDatabases = [ cfg.database.name ];
|
|
|
|
ensureUsers = [
|
2022-02-16 22:50:22 +01:00
|
|
|
{
|
|
|
|
name = cfg.database.user;
|
2022-02-16 05:33:10 +01:00
|
|
|
ensurePermissions = { "DATABASE ${cfg.database.name}" = "ALL PRIVILEGES"; };
|
|
|
|
}
|
|
|
|
];
|
2022-01-26 13:01:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
systemd.services.zammad-web = {
|
2022-02-16 05:33:10 +01:00
|
|
|
inherit environment;
|
|
|
|
serviceConfig = serviceConfig // {
|
|
|
|
# loading all the gems takes time
|
2022-02-22 11:37:03 +01:00
|
|
|
TimeoutStartSec = 1200;
|
2022-02-16 05:33:10 +01:00
|
|
|
};
|
2022-01-26 13:01:47 +01:00
|
|
|
after = [
|
|
|
|
"network.target"
|
|
|
|
"postgresql.service"
|
|
|
|
];
|
|
|
|
requires = [
|
|
|
|
"postgresql.service"
|
|
|
|
];
|
|
|
|
description = "Zammad web";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
preStart = ''
|
|
|
|
# Blindly copy the whole project here.
|
2022-02-16 22:58:20 +01:00
|
|
|
chmod -R +w .
|
2022-02-16 05:33:10 +01:00
|
|
|
rm -rf ./public/assets/*
|
|
|
|
rm -rf ./tmp/*
|
|
|
|
rm -rf ./log/*
|
|
|
|
cp -r --no-preserve=owner ${cfg.package}/* .
|
2022-02-16 22:58:20 +01:00
|
|
|
chmod -R +w .
|
2022-02-16 05:33:10 +01:00
|
|
|
# config file
|
|
|
|
cp ${databaseConfig} ./config/database.yml
|
2022-02-16 22:58:20 +01:00
|
|
|
chmod -R +w .
|
2022-02-16 06:49:51 +01:00
|
|
|
${optionalString (cfg.database.passwordFile != null) ''
|
|
|
|
{
|
|
|
|
echo -n " password: "
|
|
|
|
cat ${cfg.database.passwordFile}
|
|
|
|
} >> ./config/database.yml
|
|
|
|
''}
|
|
|
|
${optionalString (cfg.secretKeyBaseFile != null) ''
|
|
|
|
{
|
|
|
|
echo "production: "
|
|
|
|
echo -n " secret_key_base: "
|
|
|
|
cat ${cfg.secretKeyBaseFile}
|
|
|
|
} > ./config/secrets.yml
|
|
|
|
''}
|
2022-02-22 11:37:03 +01:00
|
|
|
|
2022-01-26 13:01:47 +01:00
|
|
|
if [ `${config.services.postgresql.package}/bin/psql \
|
2022-02-16 05:33:10 +01:00
|
|
|
--host ${cfg.database.host} \
|
|
|
|
${optionalString
|
2022-02-22 11:44:26 +01:00
|
|
|
(cfg.database.port != null)
|
2022-02-16 05:33:10 +01:00
|
|
|
"--port ${toString cfg.database.port}"} \
|
|
|
|
--username ${cfg.database.user} \
|
|
|
|
--dbname ${cfg.database.name} \
|
2022-01-26 13:01:47 +01:00
|
|
|
--command "SELECT COUNT(*) FROM pg_class c \
|
|
|
|
JOIN pg_namespace s ON s.oid = c.relnamespace \
|
|
|
|
WHERE s.nspname NOT IN ('pg_catalog', 'pg_toast', 'information_schema') \
|
|
|
|
AND s.nspname NOT LIKE 'pg_temp%';" | sed -n 3p` -eq 0 ]; then
|
|
|
|
echo "Initialize database"
|
2022-02-16 01:27:40 +01:00
|
|
|
./bin/rake --no-system db:migrate
|
|
|
|
./bin/rake --no-system db:seed
|
2022-01-26 13:01:47 +01:00
|
|
|
else
|
|
|
|
echo "Migrate database"
|
2022-02-16 01:27:40 +01:00
|
|
|
./bin/rake --no-system db:migrate
|
2022-01-26 13:01:47 +01:00
|
|
|
fi
|
|
|
|
echo "Done"
|
|
|
|
'';
|
2022-02-16 05:33:10 +01:00
|
|
|
script = "./script/rails server -b ${cfg.host} -p ${toString cfg.port}";
|
2022-01-26 13:01:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
systemd.services.zammad-websocket = {
|
2022-02-16 05:33:10 +01:00
|
|
|
inherit serviceConfig environment;
|
2022-01-26 13:01:47 +01:00
|
|
|
after = [ "zammad-web.service" ];
|
2022-02-16 05:33:10 +01:00
|
|
|
requires = [ "zammad-web.service" ];
|
2022-01-26 13:01:47 +01:00
|
|
|
description = "Zammad websocket";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
2022-02-16 05:33:10 +01:00
|
|
|
script = "./script/websocket-server.rb -b ${cfg.host} -p ${toString cfg.websocketPort} start";
|
2022-01-26 13:01:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
systemd.services.zammad-scheduler = {
|
2022-02-16 05:33:10 +01:00
|
|
|
inherit environment;
|
|
|
|
serviceConfig = serviceConfig // { Type = "forking"; };
|
2022-01-26 13:01:47 +01:00
|
|
|
after = [ "zammad-web.service" ];
|
2022-02-16 05:33:10 +01:00
|
|
|
requires = [ "zammad-web.service" ];
|
2022-01-26 13:01:47 +01:00
|
|
|
description = "Zammad scheduler";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
2022-02-16 05:33:10 +01:00
|
|
|
script = "./script/scheduler.rb start";
|
2022-01-26 13:01:47 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-02-16 05:33:10 +01:00
|
|
|
meta.maintainers = with lib.maintainers; [ garbas taeer ];
|
2022-01-26 13:01:47 +01:00
|
|
|
}
|