1{
2 config,
3 lib,
4 pkgs,
5 ...
6}:
7
8with lib;
9
10let
11 cfg = config.services.supybot;
12 isStateDirHome = hasPrefix "/home/" cfg.stateDir;
13 isStateDirVar = cfg.stateDir == "/var/lib/supybot";
14 pyEnv = pkgs.python3.withPackages (p: [ p.limnoria ] ++ (cfg.extraPackages p));
15in
16{
17 options = {
18
19 services.supybot = {
20
21 enable = mkOption {
22 type = types.bool;
23 default = false;
24 description = "Enable Supybot, an IRC bot (also known as Limnoria).";
25 };
26
27 stateDir = mkOption {
28 type = types.path;
29 default =
30 if versionAtLeast config.system.stateVersion "20.09" then "/var/lib/supybot" else "/home/supybot";
31 defaultText = literalExpression "/var/lib/supybot";
32 description = "The root directory, logs and plugins are stored here";
33 };
34
35 configFile = mkOption {
36 type = types.path;
37 description = ''
38 Path to initial supybot config file. This can be generated by
39 running supybot-wizard.
40
41 Note: all paths should include the full path to the stateDir
42 directory (backup conf data logs logs/plugins plugins tmp web).
43 '';
44 };
45
46 plugins = mkOption {
47 type = types.attrsOf types.path;
48 default = { };
49 description = ''
50 Attribute set of additional plugins that will be symlinked to the
51 {file}`plugin` subdirectory.
52
53 Please note that you still need to add the plugins to the config
54 file (or with `!load`) using their attribute name.
55 '';
56 example = literalExpression ''
57 let
58 plugins = pkgs.fetchzip {
59 url = "https://github.com/ProgVal/Supybot-plugins/archive/57c2450c.zip";
60 sha256 = "077snf84ibnva3sbpzdfpfma6hcdw7dflwnhg6pw7mgnf0nd84qd";
61 };
62 in
63 {
64 Wikipedia = "''${plugins}/Wikipedia";
65 Decide = ./supy-decide;
66 }
67 '';
68 };
69
70 extraPackages = mkOption {
71 type = types.functionTo (types.listOf types.package);
72 default = p: [ ];
73 defaultText = literalExpression "p: []";
74 description = ''
75 Extra Python packages available to supybot plugins. The
76 value must be a function which receives the attrset defined
77 in {var}`python3Packages` as the sole argument.
78 '';
79 example = literalExpression "p: [ p.lxml p.requests ]";
80 };
81
82 };
83
84 };
85
86 config = mkIf cfg.enable {
87
88 environment.systemPackages = [ pkgs.python3Packages.limnoria ];
89
90 users.users.supybot = {
91 uid = config.ids.uids.supybot;
92 group = "supybot";
93 description = "Supybot IRC bot user";
94 home = cfg.stateDir;
95 isSystemUser = true;
96 };
97
98 users.groups.supybot = {
99 gid = config.ids.gids.supybot;
100 };
101
102 systemd.services.supybot = {
103 description = "Supybot, an IRC bot";
104 documentation = [ "https://limnoria.readthedocs.io/" ];
105 after = [ "network.target" ];
106 wantedBy = [ "multi-user.target" ];
107 preStart = ''
108 # This needs to be created afresh every time
109 rm -f '${cfg.stateDir}/supybot.cfg.bak'
110 '';
111
112 startLimitIntervalSec = 5 * 60; # 5 min
113 startLimitBurst = 1;
114 serviceConfig = {
115 ExecStart = "${pyEnv}/bin/supybot ${cfg.stateDir}/supybot.cfg";
116 PIDFile = "/run/supybot.pid";
117 User = "supybot";
118 Group = "supybot";
119 UMask = "0007";
120 Restart = "on-abort";
121
122 NoNewPrivileges = true;
123 PrivateDevices = true;
124 PrivateMounts = true;
125 PrivateTmp = true;
126 ProtectControlGroups = true;
127 ProtectKernelModules = true;
128 ProtectKernelTunables = true;
129 RestrictAddressFamilies = [
130 "AF_INET"
131 "AF_INET6"
132 ];
133 RestrictSUIDSGID = true;
134 SystemCallArchitectures = "native";
135 RestrictNamespaces = true;
136 RestrictRealtime = true;
137 LockPersonality = true;
138 MemoryDenyWriteExecute = true;
139 RemoveIPC = true;
140 ProtectHostname = true;
141 CapabilityBoundingSet = "";
142 ProtectSystem = "full";
143 }
144 // optionalAttrs isStateDirVar {
145 StateDirectory = "supybot";
146 ProtectSystem = "strict";
147 }
148 // optionalAttrs (!isStateDirHome) {
149 ProtectHome = true;
150 };
151 };
152
153 systemd.tmpfiles.rules = [
154 "d '${cfg.stateDir}' 0700 supybot supybot - -"
155 "d '${cfg.stateDir}/backup' 0750 supybot supybot - -"
156 "d '${cfg.stateDir}/conf' 0750 supybot supybot - -"
157 "d '${cfg.stateDir}/data' 0750 supybot supybot - -"
158 "d '${cfg.stateDir}/plugins' 0750 supybot supybot - -"
159 "d '${cfg.stateDir}/logs' 0750 supybot supybot - -"
160 "d '${cfg.stateDir}/logs/plugins' 0750 supybot supybot - -"
161 "d '${cfg.stateDir}/tmp' 0750 supybot supybot - -"
162 "d '${cfg.stateDir}/web' 0750 supybot supybot - -"
163 "L '${cfg.stateDir}/supybot.cfg' - - - - ${cfg.configFile}"
164 ]
165 ++ (flip mapAttrsToList cfg.plugins (
166 name: dest: "L+ '${cfg.stateDir}/plugins/${name}' - - - - ${dest}"
167 ));
168
169 };
170}