Skip to content

Commit

Permalink
nixos/mautrix-telegram: substitute secrets in config file at runtime (N…
Browse files Browse the repository at this point in the history
…ixOS#112966)

In the latest release of mautrix-telegram not all secrets can be set
using environment variables (see mautrix/telegram#584).
This change allows these secret values to be set without ending up in
the Nix store.
  • Loading branch information
Milan authored Mar 13, 2021
1 parent 51f34b2 commit 24133ea
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions nixos/modules/services/misc/mautrix-telegram.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ let
dataDir = "/var/lib/mautrix-telegram";
registrationFile = "${dataDir}/telegram-registration.yaml";
cfg = config.services.mautrix-telegram;
# TODO: switch to configGen.json once RFC42 is implemented
settingsFile = pkgs.writeText "mautrix-telegram-settings.json" (builtins.toJSON cfg.settings);
settingsFormat = pkgs.formats.json {};
settingsFileUnsubstituted = settingsFormat.generate "mautrix-telegram-config-unsubstituted.json" cfg.settings;
settingsFile = "${dataDir}/config.json";

in {
options = {
services.mautrix-telegram = {
enable = mkEnableOption "Mautrix-Telegram, a Matrix-Telegram hybrid puppeting/relaybot bridge";

settings = mkOption rec {
# TODO: switch to types.config.json as prescribed by RFC42 once it's implemented
type = types.attrs;
apply = recursiveUpdate default;
inherit (settingsFormat) type;
default = {
appservice = rec {
database = "sqlite:///${dataDir}/mautrix-telegram.db";
Expand Down Expand Up @@ -124,6 +124,16 @@ in {
after = [ "network-online.target" ] ++ cfg.serviceDependencies;

preStart = ''
# Not all secrets can be passed as environment variable (yet)
# https://github.com/tulir/mautrix-telegram/issues/584
[ -f ${settingsFile} ] && rm -f ${settingsFile}
old_umask=$(umask)
umask 0277
${pkgs.envsubst}/bin/envsubst \
-o ${settingsFile} \
-i ${settingsFileUnsubstituted}
umask $old_umask
# generate the appservice's registration file if absent
if [ ! -f '${registrationFile}' ]; then
${pkgs.mautrix-telegram}/bin/mautrix-telegram \
Expand Down Expand Up @@ -159,6 +169,8 @@ in {
--config='${settingsFile}'
'';
};

restartTriggers = [ settingsFileUnsubstituted ];
};
};

Expand Down

0 comments on commit 24133ea

Please sign in to comment.