summaryrefslogtreecommitdiffstats
path: root/krebs/3modules/newsbot-js.nix
blob: 00e346f8e226abf7af15bdd29ddc8d3490e1be69 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
{ config, lib, pkgs, ... }:

with import <stockholm/lib>;

let

  cfg = config.krebs.newsbot-js;

  enable = cfg != {};

  out = {
    options.krebs.newsbot-js = api;
    config = mkIf enable imp;
  };

  api = mkOption {
    type = types.attrsOf (types.submodule ({ config, ... }: {
      options = {
        enable = mkEnableOption "Enable krebs newsbot" // { default = true; };

        channel = mkOption {
          type = types.str;
          default = "#${config._module.args.name}";
          description = "post the news in this channel";
        };
        feeds = mkOption {
          type = types.path;
          description = ''
            file with feeds to post
            format:
            $nick|$feedURI
          '';
        };
        ircServer = mkOption {
          type = types.str;
          default = "localhost";
          description = "to which server the bot should connect";
        };
        masterNick = mkOption {
          type = types.str;
          default = config._module.args.name;
          description = "nickname of the master bot";
        };
        package = mkOption {
          type = types.package;
          default = pkgs.newsbot-js;
          description = "newsbot package to use";
        };
        urlShortenerHost = mkOption {
          type = types.str;
          default = "go";
          description = "what server to use for url shortening, host";
        };
        urlShortenerPort = mkOption {
          type = types.str;
          default = "80";
          description = "what server to use for url shortening, port";
        };
      };
    }));
    default = {};
  };

  imp = {
    users.extraUsers.newsbot-js = {
      name = "newsbot-js";
      uid = genid "newsbot-js";
      description = "newsbot-js user";
      home = "/var/empty";
    };

    systemd.services = mapAttrs' (name: newsbot:
      nameValuePair "newsbot-${name}" {
        after = [ "network.target" ];
        wantedBy = [ "multi-user.target" ];

        path = with pkgs; [
          newsbot-js
        ];

        environment = {
          irc_server = newsbot.ircServer;
          master_nick = newsbot.masterNick;
          news_channel = newsbot.channel;
          feeds_file = newsbot.feeds;
          url_shortener_host = newsbot.urlShortenerHost;
          url_shortener_port = newsbot.urlShortenerPort;
        };

        restartIfChanged = true;

        serviceConfig = {
          User = "newsbot-js";
          Restart = "always";
          ExecStart = "${newsbot.package}/bin/newsbot";
          WatchdogSec = "86400";
        };
      }
    ) cfg;
  };

in out