summary refs log tree commit diff
path: root/nixos/modules/services/networking/go-neb.nix
blob: 765834fad83e198e89ebead5f5ff8705a20cc274 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.services.go-neb;

  settingsFormat = pkgs.formats.yaml {};
  configFile = settingsFormat.generate "config.yaml" cfg.config;
in {
  options.services.go-neb = {
    enable = mkEnableOption "Extensible matrix bot written in Go";

    bindAddress = mkOption {
      type = types.str;
      description = "Port (and optionally address) to listen on.";
      default = ":4050";
    };

    secretFile = mkOption {
      type = types.nullOr types.path;
      default = null;
      example = "/run/keys/go-neb.env";
      description = ''
        Environment variables from this file will be interpolated into the
        final config file using envsubst with this syntax: <literal>$ENVIRONMENT</literal>
        or <literal>''${VARIABLE}</literal>.
        The file should contain lines formatted as <literal>SECRET_VAR=SECRET_VALUE</literal>.
        This is useful to avoid putting secrets into the nix store.
      '';
    };

    baseUrl = mkOption {
      type = types.str;
      description = "Public-facing endpoint that can receive webhooks.";
    };

    config = mkOption {
      inherit (settingsFormat) type;
      description = ''
        Your <filename>config.yaml</filename> as a Nix attribute set.
        See <link xlink:href="https://github.com/matrix-org/go-neb/blob/master/config.sample.yaml">config.sample.yaml</link>
        for possible options.
      '';
    };
  };

  config = mkIf cfg.enable {
    systemd.services.go-neb = let
      finalConfigFile = if cfg.secretFile == null then configFile else "/var/run/go-neb/config.yaml";
    in {
      description = "Extensible matrix bot written in Go";
      after = [ "network.target" ];
      wantedBy = [ "multi-user.target" ];
      environment = {
        BASE_URL = cfg.baseUrl;
        BIND_ADDRESS = cfg.bindAddress;
        CONFIG_FILE = finalConfigFile;
      };

      serviceConfig = {
        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";
        ExecStart = "${pkgs.go-neb}/bin/go-neb";
        User = "go-neb";
        DynamicUser = true;
      };
    };
  };

  meta.maintainers = with maintainers; [ hexa maralorn ];
}