summaryrefslogtreecommitdiffstats
path: root/krebs/3modules/Reaktor.nix
blob: 669483f3cfbd327832f1419dda240cb5f882c9b8 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
{ config, lib, pkgs, ... }:

with import <stockholm/lib>;
let

  cfg = config.krebs.Reaktor;
  homedir = "/var/lib/Reaktor";

  out = {
    options.krebs.Reaktor = api;
    config = mkIf (cfg != {}) imp;
  };

  api = mkOption {
    default = {};
    type = with types; attrsOf (submodule ({ options = {

      nickname = mkOption {
        default = config.krebs.build.host.name + "|r";
        type = types.string;
        description = ''
          The nick name of the irc bot.
          Defaults to {hostname}|r
        '';
      };

      overrideConfig = mkOption {
        default = null;
        type = types.nullOr types.str;
        description = ''
          configuration to be used instead of default ones.
          Reaktor default cfg can be retrieved via `reaktor get-config`
        '';
      };

      plugins = mkOption {
        default = [pkgs.ReaktorPlugins.nixos-version];
      };

      workdir = mkOption {
        default = "/var/lib/Reaktor";
        type = types.path;
        description = ''
          path to be used as workdir (home dir is still /var/lib/Reaktor)
        '';
      };

      extraConfig = mkOption {
        default = "";
        type = types.string;
        description = ''
          configuration appended to the default or overridden configuration
        '';
      };

      extraEnviron = mkOption {
        default = {};
        type = types.attrsOf types.str;
        description = ''
          Environment to be provided to the service, can be:
            REAKTOR_HOST
            REAKTOR_PORT
            REAKTOR_STATEDIR

            debug and nickname can be set separately via the Reaktor api
        '';
      };

      channels = mkOption {
        default = [ "#krebs" ];
        type = types.listOf types.str;
        description = ''
          Channels the Reaktor should connect to at startup.
        '';
      };

      debug = mkOption {
        default = false;
        description = ''
          Reaktor debug output
        '';
      };
    };}));
  };

  imp = {
    # TODO get user per configured bot
    # TODO get home from api
    # for reaktor get-config
    users.extraUsers = singleton rec {
      name = "Reaktor";
      uid = genid name;
      description = "Reaktor user";
      home = homedir;
      createHome = true;
    };

    #users.extraGroups = singleton {
    #  name = "Reaktor";
    #  gid = config.ids.gids.Reaktor;
    #};

    systemd.services = mapAttrs' (name: botcfg:
      let
        ReaktorConfig = pkgs.writeText "config.py" ''
          ${if (isString botcfg.overrideConfig ) then ''
          # Overriden Config
          ${botcfg.overrideConfig}
          '' else ""}
          ## Extra Config
          ${concatStringsSep "\n" (map (plug: plug.config) botcfg.plugins)}
          ${botcfg.extraConfig}
        '';
      in nameValuePair "Reaktor-${name}" {
        path = with pkgs; [
          utillinux #flock for tell_on-join
          git # for nag
          python # for caps
          ];
        description = "Reaktor IRC Bot";
        after = [ "network.target" ];
        wantedBy = [ "multi-user.target" ];
        environment = {
          GIT_SSL_CAINFO = "${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt";
          PYTHONPATH = "${pkgs.Reaktor}/lib/python3.6/site-packages";
          REAKTOR_NICKNAME = botcfg.nickname;
          REAKTOR_DEBUG = (if botcfg.debug  then "True" else "False");
          REAKTOR_CHANNELS = lib.concatStringsSep "," botcfg.channels;
          state_dir = botcfg.workdir;

        } // botcfg.extraEnviron;
        serviceConfig= {
          ExecStartPre = pkgs.writeScript "Reaktor-init" ''
            #! /bin/sh
            ${if (isString botcfg.overrideConfig) then
              ''cp ${ReaktorConfig} /tmp/reaktor-${name}-config.py''
            else
              ''(${pkgs.Reaktor}/bin/reaktor get-config;cat "${ReaktorConfig}" ) > /tmp/reaktor-${name}-config.py''
            }
            mkdir -p ${botcfg.workdir}
          '';
          ExecStart = "${pkgs.Reaktor}/bin/reaktor run /tmp/reaktor-${name}-config.py";
          PrivateTmp = "true";
          User = "Reaktor";
          Restart = "always";
          RestartSec= "30" ;
        };
      }
    ) cfg;

  };

in
out