nixpkgs/nixos/modules/services/development/zammad.nix

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

362 lines
11 KiB
Nix
Raw Normal View History

{ config, lib, pkgs, ... }:
2022-02-16 00:33:10 +00:00
with lib;
let
cfg = config.services.zammad;
2022-02-16 04:33:10 +00:00
settingsFormat = pkgs.formats.yaml { };
2022-02-16 05:49:51 +00:00
filterNull = filterAttrs (_: v: v != null);
serviceConfig = {
Type = "simple";
Restart = "always";
User = "zammad";
Group = "zammad";
PrivateTmp = true;
2022-02-16 00:33:10 +00:00
StateDirectory = "zammad";
WorkingDirectory = cfg.dataDir;
};
2022-02-16 04:33:10 +00:00
environment = {
RAILS_ENV = "production";
NODE_ENV = "production";
2022-02-16 21:50:22 +00:00
RAILS_SERVE_STATIC_FILES = "true";
RAILS_LOG_TO_STDOUT = "true";
2023-11-23 17:13:51 +00:00
REDIS_URL = "redis://${cfg.redis.host}:${toString cfg.redis.port}";
};
2022-02-16 04:33:10 +00:00
databaseConfig = settingsFormat.generate "database.yml" cfg.database.settings;
2022-02-16 21:50:22 +00:00
in
{
options = {
services.zammad = {
enable = mkEnableOption (lib.mdDoc "Zammad, a web-based, open source user support/ticketing solution");
package = mkPackageOption pkgs "zammad" { };
2022-02-16 00:33:10 +00:00
dataDir = mkOption {
type = types.path;
default = "/var/lib/zammad";
description = lib.mdDoc ''
Path to a folder that will contain Zammad working directory.
'';
};
2022-02-16 00:33:10 +00:00
host = mkOption {
type = types.str;
default = "127.0.0.1";
example = "192.168.23.42";
description = lib.mdDoc "Host address.";
};
2022-02-16 00:33:10 +00:00
openPorts = mkOption {
type = types.bool;
default = false;
description = lib.mdDoc "Whether to open firewall ports for Zammad";
};
port = mkOption {
type = types.port;
default = 3000;
description = lib.mdDoc "Web service port.";
};
2022-02-16 00:33:10 +00:00
websocketPort = mkOption {
type = types.port;
default = 6042;
description = lib.mdDoc "Websocket service port.";
};
2023-11-23 17:13:51 +00:00
redis = {
createLocally = mkOption {
type = types.bool;
default = true;
description = lib.mdDoc "Whether to create a local redis automatically.";
};
name = mkOption {
type = types.str;
default = "zammad";
description = lib.mdDoc ''
Name of the redis server. Only used if `createLocally` is set to true.
'';
};
host = mkOption {
type = types.str;
default = "localhost";
description = lib.mdDoc ''
Redis server address.
'';
};
port = mkOption {
type = types.port;
default = 6379;
description = lib.mdDoc "Port of the redis server.";
};
};
2022-02-16 04:33:10 +00:00
database = {
type = mkOption {
type = types.enum [ "PostgreSQL" "MySQL" ];
default = "PostgreSQL";
example = "MySQL";
description = lib.mdDoc "Database engine to use.";
};
host = mkOption {
type = types.nullOr types.str;
default = {
PostgreSQL = "/run/postgresql";
MySQL = "localhost";
}.${cfg.database.type};
2022-02-23 21:19:10 +00:00
defaultText = literalExpression ''
{
PostgreSQL = "/run/postgresql";
MySQL = "localhost";
}.''${config.services.zammad.database.type};
'';
2022-02-16 04:33:10 +00:00
description = lib.mdDoc ''
Database host address.
'';
};
port = mkOption {
type = types.nullOr types.port;
default = null;
description = lib.mdDoc "Database port. Use `null` for default port.";
};
name = mkOption {
type = types.str;
default = "zammad";
description = lib.mdDoc ''
Database name.
'';
};
2022-02-16 04:33:10 +00:00
user = mkOption {
type = types.nullOr types.str;
default = "zammad";
description = lib.mdDoc "Database user.";
};
passwordFile = mkOption {
type = types.nullOr types.path;
default = null;
example = "/run/keys/zammad-dbpassword";
description = lib.mdDoc ''
A file containing the password for {option}`services.zammad.database.user`.
'';
};
createLocally = mkOption {
type = types.bool;
default = true;
description = lib.mdDoc "Whether to create a local database automatically.";
};
settings = mkOption {
type = settingsFormat.type;
2022-02-16 21:50:22 +00:00
default = { };
2022-02-16 04:33:10 +00:00
example = literalExpression ''
{
}
'';
description = lib.mdDoc ''
The {file}`database.yml` configuration file as key value set.
See \<TODO\>
for list of configuration parameters.
'';
};
};
2022-02-16 05:49:51 +00:00
secretKeyBaseFile = mkOption {
2022-02-16 04:33:10 +00:00
type = types.nullOr types.path;
default = null;
2022-02-16 05:49:51 +00:00
example = "/run/keys/secret_key_base";
description = lib.mdDoc ''
2022-02-16 05:49:51 +00:00
The path to a file containing the
`secret_key_base` secret.
Zammad uses `secret_key_base` 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
```
openssl rand -hex 64 >/path/to/secret_key_base_file
```
2022-02-16 05:49:51 +00:00
This should be a string, not a nix path, since nix paths are
copied into the world-readable nix store.
'';
};
};
};
2022-02-16 00:33:10 +00:00
config = mkIf cfg.enable {
2022-02-16 04:33:10 +00:00
services.zammad.database.settings = {
2022-02-16 05:49:51 +00:00
production = mapAttrs (_: v: mkDefault v) (filterNull {
2022-02-16 04:33:10 +00: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 05:49:51 +00:00
port = cfg.database.port;
2022-02-16 04:33:10 +00:00
});
};
2022-02-16 00:33:10 +00:00
networking.firewall.allowedTCPPorts = mkIf cfg.openPorts [
config.services.zammad.port
config.services.zammad.websocketPort
];
users.users.zammad = {
isSystemUser = true;
home = cfg.dataDir;
group = "zammad";
};
2022-02-16 21:50:22 +00:00
users.groups.zammad = { };
2022-02-16 04:33:10 +00:00
assertions = [
2022-02-16 21:50:22 +00:00
{
nixos/postgresql: drop ensurePermissions, fix ensureUsers for postgresql15 Closes #216989 First of all, a bit of context: in PostgreSQL, newly created users don't have the CREATE privilege on the public schema of a database even with `ALL PRIVILEGES` granted via `ensurePermissions` which is how most of the DB users are currently set up "declaratively"[1]. This means e.g. a freshly deployed Nextcloud service will break early because Nextcloud itself cannot CREATE any tables in the public schema anymore. The other issue here is that `ensurePermissions` is a mere hack. It's effectively a mixture of SQL code (e.g. `DATABASE foo` is relying on how a value is substituted in a query. You'd have to parse a subset of SQL to actually know which object are permissions granted to for a user). After analyzing the existing modules I realized that in every case with a single exception[2] the UNIX system user is equal to the db user is equal to the db name and I don't see a compelling reason why people would change that in 99% of the cases. In fact, some modules would even break if you'd change that because the declarations of the system user & the db user are mixed up[3]. So I decided to go with something new which restricts the ways to use `ensure*` options rather than expanding those[4]. Effectively this means that * The DB user _must_ be equal to the DB name. * Permissions are granted via `ensureDBOwnerhip` for an attribute-set in `ensureUsers`. That way, the user is actually the owner and can perform `CREATE`. * For such a postgres user, a database must be declared in `ensureDatabases`. For anything else, a custom state management should be implemented. This can either be `initialScript`, doing it manual, outside of the module or by implementing proper state management for postgresql[5], but the current state of `ensure*` isn't even declarative, but a convergent tool which is what Nix actually claims to _not_ do. Regarding existing setups: there are effectively two options: * Leave everything as-is (assuming that system user == db user == db name): then the DB user will automatically become the DB owner and everything else stays the same. * Drop the `createDatabase = true;` declarations: nothing will change because a removal of `ensure*` statements is ignored, so it doesn't matter at all whether this option is kept after the first deploy (and later on you'd usually restore from backups anyways). The DB user isn't the owner of the DB then, but for an existing setup this is irrelevant because CREATE on the public schema isn't revoked from existing users (only not granted for new users). [1] not really declarative though because removals of these statements are simply ignored for instance: https://github.com/NixOS/nixpkgs/issues/206467 [2] `services.invidious`: I removed the `ensure*` part temporarily because it IMHO falls into the category "manage the state on your own" (see the commit message). See also https://github.com/NixOS/nixpkgs/pull/265857 [3] e.g. roundcube had `"DATABASE ${cfg.database.username}" = "ALL PRIVILEGES";` [4] As opposed to other changes that are considered a potential fix, but also add more things like collation for DBs or passwords that are _never_ touched again when changing those. [5] As suggested in e.g. https://github.com/NixOS/nixpkgs/issues/206467
2023-11-08 11:50:09 +00:00
assertion = cfg.database.createLocally -> cfg.database.user == "zammad" && cfg.database.name == "zammad";
2022-02-16 04:33:10 +00:00
message = "services.zammad.database.user must be set to \"zammad\" if services.zammad.database.createLocally is set to true";
}
2022-02-16 21:50:22 +00:00
{
assertion = cfg.database.createLocally -> cfg.database.passwordFile == null;
2022-02-16 04:33:10 +00:00
message = "a password cannot be specified if services.zammad.database.createLocally is set to true";
}
2023-11-23 17:13:51 +00: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-16 04:33:10 +00:00
];
services.mysql = optionalAttrs (cfg.database.createLocally && cfg.database.type == "MySQL") {
enable = true;
2022-02-16 04:33:10 +00:00
package = mkDefault pkgs.mariadb;
ensureDatabases = [ cfg.database.name ];
ensureUsers = [
2022-02-16 21:50:22 +00:00
{
name = cfg.database.user;
2022-02-16 04:33:10 +00:00
ensurePermissions = { "${cfg.database.name}.*" = "ALL PRIVILEGES"; };
}
];
};
2022-02-16 04:33:10 +00:00
services.postgresql = optionalAttrs (cfg.database.createLocally && cfg.database.type == "PostgreSQL") {
enable = true;
ensureDatabases = [ cfg.database.name ];
ensureUsers = [
2022-02-16 21:50:22 +00:00
{
name = cfg.database.user;
nixos/postgresql: drop ensurePermissions, fix ensureUsers for postgresql15 Closes #216989 First of all, a bit of context: in PostgreSQL, newly created users don't have the CREATE privilege on the public schema of a database even with `ALL PRIVILEGES` granted via `ensurePermissions` which is how most of the DB users are currently set up "declaratively"[1]. This means e.g. a freshly deployed Nextcloud service will break early because Nextcloud itself cannot CREATE any tables in the public schema anymore. The other issue here is that `ensurePermissions` is a mere hack. It's effectively a mixture of SQL code (e.g. `DATABASE foo` is relying on how a value is substituted in a query. You'd have to parse a subset of SQL to actually know which object are permissions granted to for a user). After analyzing the existing modules I realized that in every case with a single exception[2] the UNIX system user is equal to the db user is equal to the db name and I don't see a compelling reason why people would change that in 99% of the cases. In fact, some modules would even break if you'd change that because the declarations of the system user & the db user are mixed up[3]. So I decided to go with something new which restricts the ways to use `ensure*` options rather than expanding those[4]. Effectively this means that * The DB user _must_ be equal to the DB name. * Permissions are granted via `ensureDBOwnerhip` for an attribute-set in `ensureUsers`. That way, the user is actually the owner and can perform `CREATE`. * For such a postgres user, a database must be declared in `ensureDatabases`. For anything else, a custom state management should be implemented. This can either be `initialScript`, doing it manual, outside of the module or by implementing proper state management for postgresql[5], but the current state of `ensure*` isn't even declarative, but a convergent tool which is what Nix actually claims to _not_ do. Regarding existing setups: there are effectively two options: * Leave everything as-is (assuming that system user == db user == db name): then the DB user will automatically become the DB owner and everything else stays the same. * Drop the `createDatabase = true;` declarations: nothing will change because a removal of `ensure*` statements is ignored, so it doesn't matter at all whether this option is kept after the first deploy (and later on you'd usually restore from backups anyways). The DB user isn't the owner of the DB then, but for an existing setup this is irrelevant because CREATE on the public schema isn't revoked from existing users (only not granted for new users). [1] not really declarative though because removals of these statements are simply ignored for instance: https://github.com/NixOS/nixpkgs/issues/206467 [2] `services.invidious`: I removed the `ensure*` part temporarily because it IMHO falls into the category "manage the state on your own" (see the commit message). See also https://github.com/NixOS/nixpkgs/pull/265857 [3] e.g. roundcube had `"DATABASE ${cfg.database.username}" = "ALL PRIVILEGES";` [4] As opposed to other changes that are considered a potential fix, but also add more things like collation for DBs or passwords that are _never_ touched again when changing those. [5] As suggested in e.g. https://github.com/NixOS/nixpkgs/issues/206467
2023-11-08 11:50:09 +00:00
ensureDBOwnership = true;
2022-02-16 04:33:10 +00:00
}
];
};
2023-11-23 17:13:51 +00:00
services.redis = optionalAttrs cfg.redis.createLocally {
servers."${cfg.redis.name}" = {
enable = true;
port = cfg.redis.port;
};
};
systemd.services.zammad-web = {
2022-02-16 04:33:10 +00:00
inherit environment;
serviceConfig = serviceConfig // {
# loading all the gems takes time
2022-02-22 10:37:03 +00:00
TimeoutStartSec = 1200;
2022-02-16 04:33:10 +00:00
};
after = [
"network.target"
"postgresql.service"
2023-11-23 17:13:51 +00:00
] ++ 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 21:58:20 +00:00
chmod -R +w .
2023-05-13 10:38:05 +00:00
rm -rf ./public/assets/
2022-02-16 04:33:10 +00:00
rm -rf ./tmp/*
rm -rf ./log/*
cp -r --no-preserve=owner ${cfg.package}/* .
2022-02-16 21:58:20 +00:00
chmod -R +w .
2022-02-16 04:33:10 +00:00
# config file
cp ${databaseConfig} ./config/database.yml
2022-02-16 21:58:20 +00:00
chmod -R +w .
2022-02-16 05:49:51 +00: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 10:37:03 +00:00
if [ `${config.services.postgresql.package}/bin/psql \
2022-02-16 04:33:10 +00:00
--host ${cfg.database.host} \
${optionalString
2022-02-22 10:44:26 +00:00
(cfg.database.port != null)
2022-02-16 04:33:10 +00:00
"--port ${toString cfg.database.port}"} \
--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-16 00:27:40 +00:00
./bin/rake --no-system db:migrate
./bin/rake --no-system db:seed
else
echo "Migrate database"
2022-02-16 00:27:40 +00:00
./bin/rake --no-system db:migrate
fi
echo "Done"
'';
2022-02-16 04:33:10 +00:00
script = "./script/rails server -b ${cfg.host} -p ${toString cfg.port}";
};
systemd.services.zammad-websocket = {
2022-02-16 04:33:10 +00:00
inherit serviceConfig environment;
after = [ "zammad-web.service" ];
2022-02-16 04:33:10 +00:00
requires = [ "zammad-web.service" ];
description = "Zammad websocket";
wantedBy = [ "multi-user.target" ];
2022-02-16 04:33:10 +00:00
script = "./script/websocket-server.rb -b ${cfg.host} -p ${toString cfg.websocketPort} start";
};
2023-11-23 17:13:51 +00:00
systemd.services.zammad-worker = {
inherit serviceConfig environment;
after = [ "zammad-web.service" ];
2022-02-16 04:33:10 +00:00
requires = [ "zammad-web.service" ];
2023-11-23 17:13:51 +00:00
description = "Zammad background worker";
wantedBy = [ "multi-user.target" ];
2023-11-23 17:13:51 +00:00
script = "./script/background-worker.rb start";
};
};
2023-11-23 17:13:51 +00:00
meta.maintainers = with lib.maintainers; [ taeer netali ];
}