summaryrefslogtreecommitdiffstats
path: root/nixos/modules/services/matrix/hebbot.nix
blob: d4c2daf47ab4d67258e6fccd77492e3b11db07a5 (plain)
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
80
81
82
83
84
85
86
{
  lib,
  config,
  pkgs,
  ...
}:

let
  inherit (lib)
    mkEnableOption
    mkOption
    mkIf
    types
    ;
  format = pkgs.formats.toml { };
  cfg = config.services.hebbot;
  settingsFile = format.generate "config.toml" cfg.settings;
  mkTemplateOption =
    templateName:
    mkOption {
      type = types.path;
      description = ''
        A path to the Markdown file for the ${templateName}.
      '';
    };
in
{
  meta.maintainers = [ ];
  options.services.hebbot = {
    enable = mkEnableOption "hebbot";
    package = lib.mkPackageOption pkgs "hebbot" { };
    botPasswordFile = mkOption {
      type = types.path;
      description = ''
        A path to the password file for your bot.

        Consider using a path that does not end up in your Nix store
        as it would be world readable.
      '';
    };
    templates = {
      project = mkTemplateOption "project template";
      report = mkTemplateOption "report template";
      section = mkTemplateOption "section template";
    };
    settings = mkOption {
      type = format.type;
      default = { };
      description = ''
        Configuration for Hebbot, see, for examples:

        - <https://github.com/matrix-org/twim-config/blob/master/config.toml>
        - <https://gitlab.gnome.org/Teams/Websites/thisweek.gnome.org/-/blob/main/hebbot/config.toml>
      '';
    };
  };

  config = mkIf cfg.enable {
    systemd.services.hebbot = {
      description = "hebbot - a TWIM-style Matrix bot written in Rust";
      after = [ "network.target" ];
      wantedBy = [ "multi-user.target" ];

      preStart = ''
        ln -sf ${cfg.templates.project} ./project_template.md
        ln -sf ${cfg.templates.report} ./report_template.md
        ln -sf ${cfg.templates.section} ./section_template.md
        ln -sf ${settingsFile} ./config.toml
      '';

      script = ''
        export BOT_PASSWORD="$(cat $CREDENTIALS_DIRECTORY/bot-password-file)"
        ${lib.getExe cfg.package}
      '';

      serviceConfig = {
        DynamicUser = true;
        Restart = "on-failure";
        LoadCredential = "bot-password-file:${cfg.botPasswordFile}";
        RestartSec = "10s";
        StateDirectory = "hebbot";
        WorkingDirectory = "/var/lib/hebbot";
      };
    };
  };
}