1
0
Fork 0
mirror of https://github.com/NixOS/nixpkgs.git synced 2025-06-25 02:26:19 +03:00
nixpkgs/nixos/modules/services/development/zammad.nix

379 lines
11 KiB
Nix
Raw Permalink Normal View History

{
config,
lib,
pkgs,
...
}:
let
cfg = config.services.zammad;
2022-02-15 23:33:10 -05:00
settingsFormat = pkgs.formats.yaml { };
filterNull = lib.filterAttrs (_: v: v != null);
serviceConfig = {
Type = "simple";
Restart = "always";
User = "zammad";
Group = "zammad";
PrivateTmp = true;
2022-02-15 19:33:10 -05:00
StateDirectory = "zammad";
WorkingDirectory = cfg.dataDir;
};
2022-02-15 23:33:10 -05:00
environment = {
RAILS_ENV = "production";
NODE_ENV = "production";
2022-02-16 16:50:22 -05:00
RAILS_SERVE_STATIC_FILES = "true";
RAILS_LOG_TO_STDOUT = "true";
2023-11-23 18:13:51 +01:00
REDIS_URL = "redis://${cfg.redis.host}:${toString cfg.redis.port}";
};
2022-02-15 23:33:10 -05:00
databaseConfig = settingsFormat.generate "database.yml" cfg.database.settings;
2022-02-16 16:50:22 -05:00
in
{
options = {
services.zammad = {
enable = lib.mkEnableOption "Zammad, a web-based, open source user support/ticketing solution";
package = lib.mkPackageOption pkgs "zammad" { };
dataDir = lib.mkOption {
type = lib.types.path;
default = "/var/lib/zammad";
description = ''
Path to a folder that will contain Zammad working directory.
'';
};
host = lib.mkOption {
type = lib.types.str;
default = "127.0.0.1";
example = "192.168.23.42";
description = "Host address.";
};
openPorts = lib.mkOption {
type = lib.types.bool;
2022-02-15 19:33:10 -05:00
default = false;
description = "Whether to open firewall ports for Zammad";
2022-02-15 19:33:10 -05:00
};
port = lib.mkOption {
type = lib.types.port;
default = 3000;
description = "Web service port.";
};
websocketPort = lib.mkOption {
type = lib.types.port;
default = 6042;
description = "Websocket service port.";
};
2023-11-23 18:13:51 +01:00
redis = {
createLocally = lib.mkOption {
type = lib.types.bool;
2023-11-23 18:13:51 +01:00
default = true;
description = "Whether to create a local redis automatically.";
2023-11-23 18:13:51 +01:00
};
name = lib.mkOption {
type = lib.types.str;
2023-11-23 18:13:51 +01:00
default = "zammad";
description = ''
2023-11-23 18:13:51 +01:00
Name of the redis server. Only used if `createLocally` is set to true.
'';
};
host = lib.mkOption {
type = lib.types.str;
2023-11-23 18:13:51 +01:00
default = "localhost";
description = ''
2023-11-23 18:13:51 +01:00
Redis server address.
'';
};
port = lib.mkOption {
type = lib.types.port;
2023-11-23 18:13:51 +01:00
default = 6379;
description = "Port of the redis server.";
2023-11-23 18:13:51 +01:00
};
};
2022-02-15 23:33:10 -05:00
database = {
type = lib.mkOption {
type = lib.types.enum [
"PostgreSQL"
"MySQL"
];
2022-02-15 23:33:10 -05:00
default = "PostgreSQL";
example = "MySQL";
description = "Database engine to use.";
2022-02-15 23:33:10 -05:00
};
host = lib.mkOption {
type = lib.types.nullOr lib.types.str;
default =
{
PostgreSQL = "/run/postgresql";
MySQL = "localhost";
}
.${cfg.database.type};
defaultText = lib.literalExpression ''
2022-02-23 16:19:10 -05:00
{
PostgreSQL = "/run/postgresql";
MySQL = "localhost";
}.''${config.services.zammad.database.type};
'';
description = ''
2022-02-15 23:33:10 -05:00
Database host address.
'';
};
port = lib.mkOption {
type = lib.types.nullOr lib.types.port;
2022-02-15 23:33:10 -05:00
default = null;
description = "Database port. Use `null` for default port.";
2022-02-15 23:33:10 -05:00
};
name = lib.mkOption {
type = lib.types.str;
2022-02-15 23:33:10 -05:00
default = "zammad";
description = ''
2022-02-15 23:33:10 -05:00
Database name.
'';
};
user = lib.mkOption {
type = lib.types.nullOr lib.types.str;
2022-02-15 23:33:10 -05:00
default = "zammad";
description = "Database user.";
2022-02-15 23:33:10 -05:00
};
passwordFile = lib.mkOption {
type = lib.types.nullOr lib.types.path;
2022-02-15 23:33:10 -05:00
default = null;
example = "/run/keys/zammad-dbpassword";
description = ''
A file containing the password for {option}`services.zammad.database.user`.
2022-02-15 23:33:10 -05:00
'';
};
createLocally = lib.mkOption {
type = lib.types.bool;
2022-02-15 23:33:10 -05:00
default = true;
description = "Whether to create a local database automatically.";
2022-02-15 23:33:10 -05:00
};
settings = lib.mkOption {
2022-02-15 23:33:10 -05:00
type = settingsFormat.type;
2022-02-16 16:50:22 -05:00
default = { };
example = lib.literalExpression ''
2022-02-15 23:33:10 -05:00
{
}
'';
description = ''
The {file}`database.yml` configuration file as key value set.
See \<TODO\>
2022-02-15 23:33:10 -05:00
for list of configuration parameters.
'';
};
};
secretKeyBaseFile = lib.mkOption {
type = lib.types.nullOr lib.types.path;
default = null;
2022-02-16 00:49:51 -05:00
example = "/run/keys/secret_key_base";
description = ''
2022-02-16 00:49:51 -05:00
The path to a file containing the
`secret_key_base` secret.
2022-02-16 00:49:51 -05:00
Zammad uses `secret_key_base` to encrypt
2022-02-16 00:49:51 -05:00
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
```
openssl rand -hex 64 >/path/to/secret_key_base_file
```
2022-02-16 00:49:51 -05:00
This should be a string, not a nix path, since nix paths are
copied into the world-readable nix store.
'';
};
};
};
config = lib.mkIf cfg.enable {
2022-02-15 23:33:10 -05:00
services.zammad.database.settings = {
production = lib.mapAttrs (_: v: lib.mkDefault v) (filterNull {
adapter =
{
PostgreSQL = "postgresql";
MySQL = "mysql2";
}
.${cfg.database.type};
2022-02-15 23:33:10 -05:00
database = cfg.database.name;
pool = 50;
timeout = 5000;
encoding = "utf8";
username = cfg.database.user;
host = cfg.database.host;
2022-02-16 00:49:51 -05:00
port = cfg.database.port;
2022-02-15 23:33:10 -05:00
});
};
networking.firewall.allowedTCPPorts = lib.mkIf cfg.openPorts [
config.services.zammad.port
config.services.zammad.websocketPort
];
users.users.zammad = {
isSystemUser = true;
home = cfg.dataDir;
group = "zammad";
};
2022-02-16 16:50:22 -05:00
users.groups.zammad = { };
2022-02-15 23:33:10 -05:00
assertions = [
2022-02-16 16:50:22 -05:00
{
assertion =
cfg.database.createLocally -> cfg.database.user == "zammad" && cfg.database.name == "zammad";
2022-02-15 23:33:10 -05:00
message = "services.zammad.database.user must be set to \"zammad\" if services.zammad.database.createLocally is set to true";
}
2022-02-16 16:50:22 -05:00
{
assertion = cfg.database.createLocally -> cfg.database.passwordFile == null;
2022-02-15 23:33:10 -05:00
message = "a password cannot be specified if services.zammad.database.createLocally is set to true";
}
2023-11-23 18:13:51 +01:00
{
assertion = cfg.redis.createLocally -> cfg.redis.host == "localhost";
message = "the redis host must be localhost if services.zammad.redis.createLocally is set to true";
}
2022-02-15 23:33:10 -05:00
];
services.mysql = lib.optionalAttrs (cfg.database.createLocally && cfg.database.type == "MySQL") {
enable = true;
package = lib.mkDefault pkgs.mariadb;
2022-02-15 23:33:10 -05:00
ensureDatabases = [ cfg.database.name ];
ensureUsers = [
2022-02-16 16:50:22 -05:00
{
name = cfg.database.user;
ensurePermissions = {
"${cfg.database.name}.*" = "ALL PRIVILEGES";
};
}
];
};
services.postgresql =
lib.optionalAttrs (cfg.database.createLocally && cfg.database.type == "PostgreSQL")
2022-02-16 16:50:22 -05:00
{
enable = true;
ensureDatabases = [ cfg.database.name ];
ensureUsers = [
{
name = cfg.database.user;
ensureDBOwnership = true;
}
];
};
services.redis = lib.optionalAttrs cfg.redis.createLocally {
2023-11-23 18:13:51 +01:00
servers."${cfg.redis.name}" = {
enable = true;
port = cfg.redis.port;
};
};
systemd.services.zammad-web = {
2022-02-15 23:33:10 -05:00
inherit environment;
serviceConfig = serviceConfig // {
# loading all the gems takes time
2022-02-22 05:37:03 -05:00
TimeoutStartSec = 1200;
2022-02-15 23:33:10 -05:00
};
after =
[
"network.target"
"postgresql.service"
]
++ lib.optionals cfg.redis.createLocally [
"redis-${cfg.redis.name}.service"
];
requires = [
"postgresql.service"
];
description = "Zammad web";
wantedBy = [ "multi-user.target" ];
preStart = ''
# Blindly copy the whole project here.
2022-02-16 16:58:20 -05:00
chmod -R +w .
2023-05-13 12:38:05 +02:00
rm -rf ./public/assets/
2022-02-15 23:33:10 -05:00
rm -rf ./tmp/*
rm -rf ./log/*
cp -r --no-preserve=owner ${cfg.package}/* .
2022-02-16 16:58:20 -05:00
chmod -R +w .
2022-02-15 23:33:10 -05:00
# config file
cp ${databaseConfig} ./config/database.yml
2022-02-16 16:58:20 -05:00
chmod -R +w .
${lib.optionalString (cfg.database.passwordFile != null) ''
{
echo -n " password: "
cat ${cfg.database.passwordFile}
} >> ./config/database.yml
2022-02-16 00:49:51 -05:00
''}
${lib.optionalString (cfg.secretKeyBaseFile != null) ''
{
echo "production: "
echo -n " secret_key_base: "
cat ${cfg.secretKeyBaseFile}
} > ./config/secrets.yml
2022-02-16 00:49:51 -05:00
''}
2022-02-22 05:37:03 -05:00
if [ `${config.services.postgresql.package}/bin/psql \
2022-02-15 23:33:10 -05:00
--host ${cfg.database.host} \
${lib.optionalString (cfg.database.port != null) "--port ${toString cfg.database.port}"} \
2022-02-15 23:33:10 -05:00
--username ${cfg.database.user} \
--dbname ${cfg.database.name} \
--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-15 19:27:40 -05:00
./bin/rake --no-system db:migrate
./bin/rake --no-system db:seed
else
echo "Migrate database"
2022-02-15 19:27:40 -05:00
./bin/rake --no-system db:migrate
fi
echo "Done"
'';
2022-02-15 23:33:10 -05:00
script = "./script/rails server -b ${cfg.host} -p ${toString cfg.port}";
};
systemd.services.zammad-websocket = {
2022-02-15 23:33:10 -05:00
inherit serviceConfig environment;
after = [ "zammad-web.service" ];
2022-02-15 23:33:10 -05:00
requires = [ "zammad-web.service" ];
description = "Zammad websocket";
wantedBy = [ "multi-user.target" ];
2022-02-15 23:33:10 -05:00
script = "./script/websocket-server.rb -b ${cfg.host} -p ${toString cfg.websocketPort} start";
};
2023-11-23 18:13:51 +01:00
systemd.services.zammad-worker = {
inherit serviceConfig environment;
after = [ "zammad-web.service" ];
2022-02-15 23:33:10 -05:00
requires = [ "zammad-web.service" ];
2023-11-23 18:13:51 +01:00
description = "Zammad background worker";
wantedBy = [ "multi-user.target" ];
2023-11-23 18:13:51 +01:00
script = "./script/background-worker.rb start";
};
};
meta.maintainers = with lib.maintainers; [
taeer
netali
];
}