summary refs log tree commit diff
path: root/nixos/modules/services/networking/supybot.nix
blob: 94b79c7e247ff713ef087f1b437c3b857285f5bb (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
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
155
156
157
158
159
160
161
162
163
{ config, lib, pkgs, ... }:

with lib;

let
  cfg  = config.services.supybot;
  isStateDirHome = hasPrefix "/home/" cfg.stateDir;
  isStateDirVar = cfg.stateDir == "/var/lib/supybot";
  pyEnv = pkgs.python3.withPackages (p: [ p.limnoria ] ++ (cfg.extraPackages p));
in
{
  options = {

    services.supybot = {

      enable = mkOption {
        type = types.bool;
        default = false;
        description = "Enable Supybot, an IRC bot (also known as Limnoria).";
      };

      stateDir = mkOption {
        type = types.path;
        default = if versionAtLeast config.system.stateVersion "20.09"
          then "/var/lib/supybot"
          else "/home/supybot";
        defaultText = literalExpression "/var/lib/supybot";
        description = "The root directory, logs and plugins are stored here";
      };

      configFile = mkOption {
        type = types.path;
        description = ''
          Path to initial supybot config file. This can be generated by
          running supybot-wizard.

          Note: all paths should include the full path to the stateDir
          directory (backup conf data logs logs/plugins plugins tmp web).
        '';
      };

      plugins = mkOption {
        type = types.attrsOf types.path;
        default = {};
        description = ''
          Attribute set of additional plugins that will be symlinked to the
          <filename>plugin</filename> subdirectory.

          Please note that you still need to add the plugins to the config
          file (or with <literal>!load</literal>) using their attribute name.
        '';
        example = literalExpression ''
          let
            plugins = pkgs.fetchzip {
              url = "https://github.com/ProgVal/Supybot-plugins/archive/57c2450c.zip";
              sha256 = "077snf84ibnva3sbpzdfpfma6hcdw7dflwnhg6pw7mgnf0nd84qd";
            };
          in
          {
            Wikipedia = "''${plugins}/Wikipedia";
            Decide = ./supy-decide;
          }
        '';
      };

      extraPackages = mkOption {
        type = types.functionTo (types.listOf types.package);
        default = p: [];
        defaultText = literalExpression "p: []";
        description = ''
          Extra Python packages available to supybot plugins. The
          value must be a function which receives the attrset defined
          in <varname>python3Packages</varname> as the sole argument.
        '';
        example = literalExpression "p: [ p.lxml p.requests ]";
      };

    };

  };

  config = mkIf cfg.enable {

    environment.systemPackages = [ pkgs.python3Packages.limnoria ];

    users.users.supybot = {
      uid = config.ids.uids.supybot;
      group = "supybot";
      description = "Supybot IRC bot user";
      home = cfg.stateDir;
      isSystemUser = true;
    };

    users.groups.supybot = {
      gid = config.ids.gids.supybot;
    };

    systemd.services.supybot = {
      description = "Supybot, an IRC bot";
      documentation = [ "https://limnoria.readthedocs.io/" ];
      after = [ "network.target" ];
      wantedBy = [ "multi-user.target" ];
      preStart = ''
        # This needs to be created afresh every time
        rm -f '${cfg.stateDir}/supybot.cfg.bak'
      '';

      startLimitIntervalSec = 5 * 60;  # 5 min
      startLimitBurst = 1;
      serviceConfig = {
        ExecStart = "${pyEnv}/bin/supybot ${cfg.stateDir}/supybot.cfg";
        PIDFile = "/run/supybot.pid";
        User = "supybot";
        Group = "supybot";
        UMask = "0007";
        Restart = "on-abort";

        NoNewPrivileges = true;
        PrivateDevices = true;
        PrivateMounts = true;
        PrivateTmp = true;
        ProtectControlGroups = true;
        ProtectKernelModules = true;
        ProtectKernelTunables = true;
        RestrictAddressFamilies = [ "AF_INET" "AF_INET6" ];
        RestrictSUIDSGID = true;
        SystemCallArchitectures = "native";
        RestrictNamespaces = true;
        RestrictRealtime = true;
        LockPersonality = true;
        MemoryDenyWriteExecute = true;
        RemoveIPC = true;
        ProtectHostname = true;
        CapabilityBoundingSet = "";
        ProtectSystem = "full";
      }
      // optionalAttrs isStateDirVar {
        StateDirectory = "supybot";
        ProtectSystem = "strict";
      }
      // optionalAttrs (!isStateDirHome) {
        ProtectHome = true;
      };
    };

    systemd.tmpfiles.rules = [
      "d '${cfg.stateDir}'              0700 supybot supybot - -"
      "d '${cfg.stateDir}/backup'       0750 supybot supybot - -"
      "d '${cfg.stateDir}/conf'         0750 supybot supybot - -"
      "d '${cfg.stateDir}/data'         0750 supybot supybot - -"
      "d '${cfg.stateDir}/plugins'      0750 supybot supybot - -"
      "d '${cfg.stateDir}/logs'         0750 supybot supybot - -"
      "d '${cfg.stateDir}/logs/plugins' 0750 supybot supybot - -"
      "d '${cfg.stateDir}/tmp'          0750 supybot supybot - -"
      "d '${cfg.stateDir}/web'          0750 supybot supybot - -"
      "L '${cfg.stateDir}/supybot.cfg'  -    -       -       - ${cfg.configFile}"
    ]
    ++ (flip mapAttrsToList cfg.plugins (name: dest:
      "L+ '${cfg.stateDir}/plugins/${name}' - - - - ${dest}"
    ));

  };
}