···
-
{ lib, config, pkgs, ... }:
-
cfg = config.services.mautrix-signal;
-
dataDir = "/var/lib/mautrix-signal";
-
registrationFile = "${dataDir}/signal-registration.yaml";
-
settingsFile = "${dataDir}/config.json";
-
settingsFileUnsubstituted =
-
settingsFormat.generate "mautrix-signal-config-unsubstituted.json"
-
settingsFormat = pkgs.formats.json { };
-
appservicePort = 29328;
-
mkDefaults = lib.mapAttrsRecursive (n: v: lib.mkDefault v);
-
homeserver.address = "http://localhost:8448";
-
socket_path = config.services.signald.socketPath;
-
outgoing_attachment_dir = "/var/lib/signald/tmp";
-
database.type = "sqlite3";
-
database.uri = "${dataDir}/mautrix-signal.db";
-
bot.username = "signalbot";
-
bot.displayname = "Signal Bridge Bot";
-
username_template = "signal_{{.}}";
-
double_puppet_server_map = { };
-
login_shared_secret_map = { };
-
permissions."*" = "relay";
-
writers = lib.singleton {
-
format = "pretty-colored";
-
options.services.mautrix-signal = {
-
enable = lib.mkEnableOption (lib.mdDoc
-
"mautrix-signal, a puppeting/relaybot bridge between Matrix and Signal.");
-
settings = lib.mkOption {
-
type = settingsFormat.type;
-
default = defaultConfig;
-
description = lib.mdDoc ''
-
{file}`config.yaml` configuration as a Nix attribute set.
-
Configuration options should match those described in
-
[example-config.yaml](https://github.com/mautrix/signal/blob/master/example-config.yaml).
-
uri = "postgresql:///mautrix_signal?host=/run/postgresql";
-
ephemeral_events = false;
-
history_sync = { request_full_sync = true; };
-
private_chat_portal_meta = true;
-
provisioning = { shared_secret = "disable"; };
-
permissions = { "example.com" = "user"; };
-
serviceDependencies = lib.mkOption {
-
type = with lib.types; listOf str;
-
default = lib.optional config.services.matrix-synapse.enable
-
config.services.matrix-synapse.serviceUnit;
-
defaultText = lib.literalExpression ''
-
optional config.services.matrix-synapse.enable config.services.matrix-synapse.serviceUnits
-
description = lib.mdDoc ''
-
List of Systemd services to require and wait for when starting the application service.
-
config = lib.mkIf cfg.enable {
-
services.signald.enable = true;
-
users.users.mautrix-signal = {
-
group = "mautrix-signal";
-
description = "Mautrix-Signal bridge user";
-
users.groups.mautrix-signal = { };
-
services.mautrix-signal.settings = lib.mkMerge (map mkDefaults [
-
# Note: this is defined here to avoid the docs depending on `config`
-
homeserver.domain = config.services.matrix-synapse.settings.server_name;
-
systemd.services.mautrix-signal = {
-
description = "Mautrix-Signal Service - A Signal bridge for Matrix";
-
requires = [ "signald.service" ];
-
# voice messages need `ffmpeg`
-
path = [ pkgs.ffmpeg ];
-
wantedBy = [ "multi-user.target" ];
-
wants = [ "network-online.target" ] ++ cfg.serviceDependencies;
-
after = [ "network-online.target" "signald.service" ]
-
++ cfg.serviceDependencies;
-
# substitute the settings file by environment variables
-
# in this case read from EnvironmentFile
-
test -f '${settingsFile}' && rm -f '${settingsFile}'
-
${pkgs.envsubst}/bin/envsubst \
-
-i '${settingsFileUnsubstituted}'
-
# generate the appservice's registration file if absent
-
if [ ! -f '${registrationFile}' ]; then
-
${pkgs.mautrix-signal}/bin/mautrix-signal \
-
--generate-registration \
-
--config='${settingsFile}' \
-
--registration='${registrationFile}'
-
chmod 640 ${registrationFile}
-
${pkgs.yq}/bin/yq -s '.[0].appservice.as_token = .[1].as_token
-
| .[0].appservice.hs_token = .[1].hs_token
-
| .[0]' '${settingsFile}' '${registrationFile}' \
-
> '${settingsFile}.tmp'
-
mv '${settingsFile}.tmp' '${settingsFile}'
-
SupplementaryGroups = [ "signald" ];
-
User = "mautrix-signal";
-
Group = "mautrix-signal";
-
StateDirectory = baseNameOf dataDir;
-
WorkingDirectory = dataDir;
-
${pkgs.mautrix-signal}/bin/mautrix-signal \
-
--config='${settingsFile}' \
-
--registration='${registrationFile}'
-
LockPersonality = true;
-
MemoryDenyWriteExecute = true;
-
NoNewPrivileges = true;
-
ProtectControlGroups = true;
-
ProtectHostname = true;
-
ProtectKernelLogs = true;
-
ProtectKernelModules = true;
-
ProtectKernelTunables = true;
-
ProtectSystem = "strict";
-
Restart = "on-failure";
-
RestrictRealtime = true;
-
RestrictSUIDSGID = true;
-
SystemCallArchitectures = "native";
-
SystemCallErrorNumber = "EPERM";
-
SystemCallFilter = [ "@system-service" ];
-
restartTriggers = [ settingsFileUnsubstituted ];