1{
2 lib,
3 config,
4 pkgs,
5 ...
6}:
7
8let
9 inherit (lib)
10 mkEnableOption
11 mkOption
12 mkIf
13 types
14 ;
15 format = pkgs.formats.toml { };
16 cfg = config.services.hebbot;
17 settingsFile = format.generate "config.toml" cfg.settings;
18 mkTemplateOption =
19 templateName:
20 mkOption {
21 type = types.path;
22 description = ''
23 A path to the Markdown file for the ${templateName}.
24 '';
25 };
26in
27{
28 meta.maintainers = [ lib.maintainers.raitobezarius ];
29 options.services.hebbot = {
30 enable = mkEnableOption "hebbot";
31 package = lib.mkPackageOption pkgs "hebbot" { };
32 botPasswordFile = mkOption {
33 type = types.path;
34 description = ''
35 A path to the password file for your bot.
36
37 Consider using a path that does not end up in your Nix store
38 as it would be world readable.
39 '';
40 };
41 templates = {
42 project = mkTemplateOption "project template";
43 report = mkTemplateOption "report template";
44 section = mkTemplateOption "section template";
45 };
46 settings = mkOption {
47 type = format.type;
48 default = { };
49 description = ''
50 Configuration for Hebbot, see, for examples:
51
52 - <https://github.com/matrix-org/twim-config/blob/master/config.toml>
53 - <https://gitlab.gnome.org/Teams/Websites/thisweek.gnome.org/-/blob/main/hebbot/config.toml>
54 '';
55 };
56 };
57
58 config = mkIf cfg.enable {
59 systemd.services.hebbot = {
60 description = "hebbot - a TWIM-style Matrix bot written in Rust";
61 after = [ "network.target" ];
62 wantedBy = [ "multi-user.target" ];
63
64 preStart = ''
65 ln -sf ${cfg.templates.project} ./project_template.md
66 ln -sf ${cfg.templates.report} ./report_template.md
67 ln -sf ${cfg.templates.section} ./section_template.md
68 ln -sf ${settingsFile} ./config.toml
69 '';
70
71 script = ''
72 export BOT_PASSWORD="$(cat $CREDENTIALS_DIRECTORY/bot-password-file)"
73 ${lib.getExe cfg.package}
74 '';
75
76 serviceConfig = {
77 DynamicUser = true;
78 Restart = "on-failure";
79 LoadCredential = "bot-password-file:${cfg.botPasswordFile}";
80 RestartSec = "10s";
81 StateDirectory = "hebbot";
82 WorkingDirectory = "/var/lib/hebbot";
83 };
84 };
85 };
86}