2020-06-01 21:31:10 +01:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.go-neb;
|
|
|
|
|
2021-06-22 08:07:50 +01:00
|
|
|
settingsFormat = pkgs.formats.yaml {};
|
|
|
|
configFile = settingsFormat.generate "config.yaml" cfg.config;
|
2020-06-01 21:31:10 +01:00
|
|
|
in {
|
|
|
|
options.services.go-neb = {
|
|
|
|
enable = mkEnableOption "Extensible matrix bot written in Go";
|
|
|
|
|
|
|
|
bindAddress = mkOption {
|
|
|
|
type = types.str;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "Port (and optionally address) to listen on.";
|
2020-06-01 21:31:10 +01:00
|
|
|
default = ":4050";
|
|
|
|
};
|
|
|
|
|
2021-06-22 08:07:50 +01:00
|
|
|
secretFile = mkOption {
|
|
|
|
type = types.nullOr types.path;
|
|
|
|
default = null;
|
|
|
|
example = "/run/keys/go-neb.env";
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
2021-06-22 08:07:50 +01:00
|
|
|
Environment variables from this file will be interpolated into the
|
2022-07-28 22:19:15 +01:00
|
|
|
final config file using envsubst with this syntax: `$ENVIRONMENT`
|
|
|
|
or `''${VARIABLE}`.
|
|
|
|
The file should contain lines formatted as `SECRET_VAR=SECRET_VALUE`.
|
2021-06-22 08:07:50 +01:00
|
|
|
This is useful to avoid putting secrets into the nix store.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2020-06-01 21:31:10 +01:00
|
|
|
baseUrl = mkOption {
|
|
|
|
type = types.str;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc "Public-facing endpoint that can receive webhooks.";
|
2020-06-01 21:31:10 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
config = mkOption {
|
2021-06-22 08:07:50 +01:00
|
|
|
inherit (settingsFormat) type;
|
2022-07-28 22:19:15 +01:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
Your {file}`config.yaml` as a Nix attribute set.
|
|
|
|
See [config.sample.yaml](https://github.com/matrix-org/go-neb/blob/master/config.sample.yaml)
|
2020-06-01 21:31:10 +01:00
|
|
|
for possible options.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
2021-06-22 08:07:50 +01:00
|
|
|
systemd.services.go-neb = let
|
|
|
|
finalConfigFile = if cfg.secretFile == null then configFile else "/var/run/go-neb/config.yaml";
|
|
|
|
in {
|
2020-06-01 21:31:10 +01:00
|
|
|
description = "Extensible matrix bot written in Go";
|
|
|
|
after = [ "network.target" ];
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
environment = {
|
|
|
|
BASE_URL = cfg.baseUrl;
|
|
|
|
BIND_ADDRESS = cfg.bindAddress;
|
2021-06-22 08:07:50 +01:00
|
|
|
CONFIG_FILE = finalConfigFile;
|
2020-06-01 21:31:10 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
serviceConfig = {
|
2021-06-22 08:07:50 +01:00
|
|
|
ExecStartPre = lib.optional (cfg.secretFile != null)
|
|
|
|
(pkgs.writeShellScript "pre-start" ''
|
|
|
|
umask 077
|
|
|
|
export $(xargs < ${cfg.secretFile})
|
|
|
|
${pkgs.envsubst}/bin/envsubst -i "${configFile}" > ${finalConfigFile}
|
|
|
|
chown go-neb ${finalConfigFile}
|
|
|
|
'');
|
|
|
|
PermissionsStartOnly = true;
|
|
|
|
RuntimeDirectory = "go-neb";
|
2020-06-01 21:31:10 +01:00
|
|
|
ExecStart = "${pkgs.go-neb}/bin/go-neb";
|
2021-06-22 08:07:50 +01:00
|
|
|
User = "go-neb";
|
2020-06-01 21:31:10 +01:00
|
|
|
DynamicUser = true;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
meta.maintainers = with maintainers; [ hexa maralorn ];
|
|
|
|
}
|