From cb67bb0335554e213b823f1561251dfb9dff857e Mon Sep 17 00:00:00 2001 From: Maximilian Bosch Date: Mon, 9 Oct 2023 17:06:21 +0200 Subject: [PATCH 1/2] nixos/matrix-synapse: mergeable log configuration Right now there's no trivial way to override parts of synapse's log config such as the log-level because the only thing that's changeable is the path to the log-file used by synapse and its workers. Now, there's a new option called `services.matrix-synapse.log` which contains the default log config as Nix attribute-set (except `handlers.journal.SYSLOG_IDENTIFIER`). It has default priority, so new things can be added like services.matrix-synapse.log = { my.extra.field = 23; } without discarding the rest. If desired, this can still be done via `lib.mkForce`. If the log configuration for a single worker or synapse, but not all workers should be changed, `services.matrix-synapse.settings.log_config` or `services.matrix-synapse.workers._name_.worker_log_config` can be used. --- nixos/modules/services/matrix/synapse.nix | 80 ++++++++++++++++------- 1 file changed, 58 insertions(+), 22 deletions(-) diff --git a/nixos/modules/services/matrix/synapse.nix b/nixos/modules/services/matrix/synapse.nix index 1354a8cb58b4..10534ea23521 100644 --- a/nixos/modules/services/matrix/synapse.nix +++ b/nixos/modules/services/matrix/synapse.nix @@ -70,13 +70,12 @@ let inherit (cfg) plugins; }; - logConfig = logName: { + defaultCommonLogConfig = { version = 1; formatters.journal_fmt.format = "%(name)s: [%(request)s] %(message)s"; handlers.journal = { class = "systemd.journal.JournalHandler"; formatter = "journal_fmt"; - SYSLOG_IDENTIFIER = logName; }; root = { level = "INFO"; @@ -84,33 +83,27 @@ let }; disable_existing_loggers = false; }; + + defaultCommonLogConfigText = generators.toPretty { } defaultCommonLogConfig; + logConfigText = logName: - let - expr = '' - { - version = 1; - formatters.journal_fmt.format = "%(name)s: [%(request)s] %(message)s"; - handlers.journal = { - class = "systemd.journal.JournalHandler"; - formatter = "journal_fmt"; - SYSLOG_IDENTIFIER = "${logName}"; - }; - root = { - level = "INFO"; - handlers = [ "journal" ]; - }; - disable_existing_loggers = false; - }; - ''; - in lib.literalMD '' Path to a yaml file generated from this Nix expression: ``` - ${expr} + ${generators.toPretty { } ( + recursiveUpdate defaultCommonLogConfig { handlers.journal.SYSLOG_IDENTIFIER = logName; } + )} ``` ''; - genLogConfigFile = logName: format.generate "synapse-log-${logName}.yaml" (logConfig logName); + + genLogConfigFile = logName: format.generate + "synapse-log-${logName}.yaml" + (cfg.log // optionalAttrs (cfg.log?handlers.journal) { + handlers.journal = cfg.log.handlers.journal // { + SYSLOG_IDENTIFIER = logName; + }; + }); in { imports = [ @@ -394,6 +387,47 @@ in { ''; }; + log = mkOption { + type = types.attrsOf format.type; + defaultText = literalExpression defaultCommonLogConfigText; + description = mdDoc '' + Default configuration for the loggers used by `matrix-synapse` and its workers. + The defaults are added with the default priority which means that + these will be merged with additional declarations. For instance + the log-level for synapse and its workers can be changed like this: + + ```nix + { lib, ... }: { + services.matrix-synapse.log.root.level = lib.mkForce "WARNING"; + } + ``` + + And another field can be added like this: + + ```nix + { + services.matrix-synapse.log = { + loggers."synapse.http.matrixfederationclient".level = "DEBUG"; + }; + } + ``` + + Additionally, the field `handlers.journal.SYSLOG_IDENTIFIER` will be added to + each log config, i.e. + * `synapse` for `matrix-synapse.service` + * `synapse-` for `matrix-synapse-worker-.service` + + This is only done if this option has a `handlers.journal` field declared. + + To discard all settings declared by this option for each worker and synapse, + `lib.mkForce` can be used. + + To discard all settings declared by this option for a single worker or synapse only, + [](#opt-services.matrix-synapse.workers._name_.worker_log_config) or + [](#opt-services.matrix-synapse.settings.log_config) can be used. + ''; + }; + settings = mkOption { default = { }; description = mdDoc '' @@ -1008,6 +1042,8 @@ in { # default them, so they are additive services.matrix-synapse.extras = defaultExtras; + services.matrix-synapse.log = defaultCommonLogConfig; + users.users.matrix-synapse = { group = "matrix-synapse"; home = cfg.dataDir; From e5928d9a73678749def663f838aef4b9d37b1488 Mon Sep 17 00:00:00 2001 From: Maximilian Bosch Date: Sun, 15 Oct 2023 16:16:28 +0200 Subject: [PATCH 2/2] nixos/synapse: `mkDefault` each value of the log config That way it's not even needed to specify an `mkForce` when changing existing attributes, e.g. root's log level. --- nixos/modules/services/matrix/synapse.nix | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/nixos/modules/services/matrix/synapse.nix b/nixos/modules/services/matrix/synapse.nix index 10534ea23521..0cf2736bad45 100644 --- a/nixos/modules/services/matrix/synapse.nix +++ b/nixos/modules/services/matrix/synapse.nix @@ -393,12 +393,14 @@ in { description = mdDoc '' Default configuration for the loggers used by `matrix-synapse` and its workers. The defaults are added with the default priority which means that - these will be merged with additional declarations. For instance + these will be merged with additional declarations. These additional + declarations also take precedence over the defaults when declared + with at least normal priority. For instance the log-level for synapse and its workers can be changed like this: ```nix { lib, ... }: { - services.matrix-synapse.log.root.level = lib.mkForce "WARNING"; + services.matrix-synapse.log.root.level = "WARNING"; } ``` @@ -1042,7 +1044,7 @@ in { # default them, so they are additive services.matrix-synapse.extras = defaultExtras; - services.matrix-synapse.log = defaultCommonLogConfig; + services.matrix-synapse.log = mapAttrsRecursive (const mkDefault) defaultCommonLogConfig; users.users.matrix-synapse = { group = "matrix-synapse";