1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6 cfg = config.services.go-neb;
7
8 settingsFormat = pkgs.formats.yaml {};
9 configFile = settingsFormat.generate "config.yaml" cfg.config;
10in {
11 options.services.go-neb = {
12 enable = mkEnableOption (lib.mdDoc "Extensible matrix bot written in Go");
13
14 bindAddress = mkOption {
15 type = types.str;
16 description = lib.mdDoc "Port (and optionally address) to listen on.";
17 default = ":4050";
18 };
19
20 secretFile = mkOption {
21 type = types.nullOr types.path;
22 default = null;
23 example = "/run/keys/go-neb.env";
24 description = lib.mdDoc ''
25 Environment variables from this file will be interpolated into the
26 final config file using envsubst with this syntax: `$ENVIRONMENT`
27 or `''${VARIABLE}`.
28 The file should contain lines formatted as `SECRET_VAR=SECRET_VALUE`.
29 This is useful to avoid putting secrets into the nix store.
30 '';
31 };
32
33 baseUrl = mkOption {
34 type = types.str;
35 description = lib.mdDoc "Public-facing endpoint that can receive webhooks.";
36 };
37
38 config = mkOption {
39 inherit (settingsFormat) type;
40 description = lib.mdDoc ''
41 Your {file}`config.yaml` as a Nix attribute set.
42 See [config.sample.yaml](https://github.com/matrix-org/go-neb/blob/master/config.sample.yaml)
43 for possible options.
44 '';
45 };
46 };
47
48 config = mkIf cfg.enable {
49 systemd.services.go-neb = let
50 finalConfigFile = if cfg.secretFile == null then configFile else "/var/run/go-neb/config.yaml";
51 in {
52 description = "Extensible matrix bot written in Go";
53 after = [ "network.target" ];
54 wantedBy = [ "multi-user.target" ];
55 environment = {
56 BASE_URL = cfg.baseUrl;
57 BIND_ADDRESS = cfg.bindAddress;
58 CONFIG_FILE = finalConfigFile;
59 };
60
61 serviceConfig = {
62 ExecStartPre = lib.optional (cfg.secretFile != null)
63 ("+" + pkgs.writeShellScript "pre-start" ''
64 umask 077
65 export $(xargs < ${cfg.secretFile})
66 ${pkgs.envsubst}/bin/envsubst -i "${configFile}" > ${finalConfigFile}
67 chown go-neb ${finalConfigFile}
68 '');
69 RuntimeDirectory = "go-neb";
70 ExecStart = "${pkgs.go-neb}/bin/go-neb";
71 User = "go-neb";
72 DynamicUser = true;
73 };
74 };
75 };
76
77 meta.maintainers = with maintainers; [ hexa maralorn ];
78}