summary refs log tree commit diff
path: root/nixos/modules/services/networking/jibri/default.nix
blob: b4e41a292826b237699142fb49ed8c7a32232547 (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.services.jibri;

  # Copied from the jitsi-videobridge.nix file.
  toHOCON = x:
    if isAttrs x && x ? __hocon_envvar then ("\${" + x.__hocon_envvar + "}")
    else if isAttrs x then "{${ concatStringsSep "," (mapAttrsToList (k: v: ''"${k}":${toHOCON v}'') x) }}"
    else if isList x then "[${ concatMapStringsSep "," toHOCON x }]"
    else builtins.toJSON x;

  # We're passing passwords in environment variables that have names generated
  # from an attribute name, which may not be a valid bash identifier.
  toVarName = s: "XMPP_PASSWORD_" + stringAsChars (c: if builtins.match "[A-Za-z0-9]" c != null then c else "_") s;

  defaultJibriConfig = {
    id = "";
    single-use-mode = false;

    api = {
      http.external-api-port = 2222;
      http.internal-api-port = 3333;

      xmpp.environments = flip mapAttrsToList cfg.xmppEnvironments (name: env: {
        inherit name;

        xmpp-server-hosts = env.xmppServerHosts;
        xmpp-domain = env.xmppDomain;
        control-muc = {
          domain = env.control.muc.domain;
          room-name = env.control.muc.roomName;
          nickname = env.control.muc.nickname;
        };

        control-login = {
          domain = env.control.login.domain;
          username = env.control.login.username;
          password.__hocon_envvar = toVarName "${name}_control";
        };

        call-login = {
          domain = env.call.login.domain;
          username = env.call.login.username;
          password.__hocon_envvar = toVarName "${name}_call";
        };

        strip-from-room-domain = env.stripFromRoomDomain;
        usage-timeout = env.usageTimeout;
        trust-all-xmpp-certs = env.disableCertificateVerification;
      });
    };

    recording = {
      recordings-directory = "/tmp/recordings";
      finalize-script = "${cfg.finalizeScript}";
    };

    streaming.rtmp-allow-list = [ ".*" ];

    chrome.flags = [
      "--use-fake-ui-for-media-stream"
      "--start-maximized"
      "--kiosk"
      "--enabled"
      "--disable-infobars"
      "--autoplay-policy=no-user-gesture-required"
    ];

    stats.enable-stats-d = true;
    webhook.subscribers = [ ];

    jwt-info = { };

    call-status-checks = {
      no-media-timout = "30 seconds";
      all-muted-timeout = "10 minutes";
      default-call-empty-timout = "30 seconds";
    };
  };
  # Allow overriding leaves of the default config despite types.attrs not doing any merging.
  jibriConfig = recursiveUpdate defaultJibriConfig cfg.config;
  configFile = pkgs.writeText "jibri.conf" (toHOCON { jibri = jibriConfig; });
in
{
  options.services.jibri = with types; {
    enable = mkEnableOption "Jitsi BRoadcasting Infrastructure. Currently the only supported way of enabling a jibri instance is with <option>services.jitsi-meet.jibri.enable</option>";
    config = mkOption {
      type = attrs;
      default = { };
      description = ''
        Jibri configuration.
        See <link xlink:href="https://github.com/jitsi/jibri/blob/master/src/main/resources/reference.conf" />
        for default configuration with comments.
      '';
    };

    finalizeScript = mkOption {
      type = types.path;
      default = pkgs.writeScript "finalize_recording.sh" ''
        #!/bin/sh

        RECORDINGS_DIR=$1

        echo "This is a dummy finalize script" > /tmp/finalize.out
        echo "The script was invoked with recordings directory $RECORDINGS_DIR." >> /tmp/finalize.out
        echo "You should put any finalize logic (renaming, uploading to a service" >> /tmp/finalize.out
        echo "or storage provider, etc.) in this script" >> /tmp/finalize.out

        exit 0
      '';
      defaultText = literalExpression ''
        pkgs.writeScript "finalize_recording.sh" ''''''
        #!/bin/sh

        RECORDINGS_DIR=$1

        echo "This is a dummy finalize script" > /tmp/finalize.out
        echo "The script was invoked with recordings directory $RECORDINGS_DIR." >> /tmp/finalize.out
        echo "You should put any finalize logic (renaming, uploading to a service" >> /tmp/finalize.out
        echo "or storage provider, etc.) in this script" >> /tmp/finalize.out

        exit 0
        '''''';
      '';
      example = literalExpression ''
        pkgs.writeScript "finalize_recording.sh" ''''''
        #!/bin/sh
        RECORDINGS_DIR=$1
        ${pkgs.rclone}/bin/rclone copy $RECORDINGS_DIR RCLONE_REMOTE:jibri-recordings/ -v --log-file=/var/log/jitsi/jibri/recording-upload.txt
        exit 0
        '''''';
      '';
      description = ''
        This script runs when jibri finishes recording a video of a conference.
      '';
    };

    xmppEnvironments = mkOption {
      description = ''
        XMPP servers to connect to.
      '';
      default = { };
      type = attrsOf (submodule ({ name, ... }: {
        options = {
          xmppServerHosts = mkOption {
            type = listOf str;
            example = [ "xmpp.example.org" ];
            description = ''
              Hostnames of the XMPP servers to connect to.
            '';
          };
          xmppDomain = mkOption {
            type = str;
            example = "xmpp.example.org";
            description = ''
              The base XMPP domain.
            '';
          };
          control.muc.domain = mkOption {
            type = str;
            description = ''
              The domain part of the MUC to connect to for control.
            '';
          };
          control.muc.roomName = mkOption {
            type = str;
            default = "JibriBrewery";
            description = ''
              The room name of the MUC to connect to for control.
            '';
          };
          control.muc.nickname = mkOption {
            type = str;
            default = "jibri";
            description = ''
              The nickname for this Jibri instance in the MUC.
            '';
          };
          control.login.domain = mkOption {
            type = str;
            description = ''
              The domain part of the JID for this Jibri instance.
            '';
          };
          control.login.username = mkOption {
            type = str;
            default = "jvb";
            description = ''
              User part of the JID.
            '';
          };
          control.login.passwordFile = mkOption {
            type = str;
            example = "/run/keys/jibri-xmpp1";
            description = ''
              File containing the password for the user.
            '';
          };

          call.login.domain = mkOption {
            type = str;
            example = "recorder.xmpp.example.org";
            description = ''
              The domain part of the JID for the recorder.
            '';
          };
          call.login.username = mkOption {
            type = str;
            default = "recorder";
            description = ''
              User part of the JID for the recorder.
            '';
          };
          call.login.passwordFile = mkOption {
            type = str;
            example = "/run/keys/jibri-recorder-xmpp1";
            description = ''
              File containing the password for the user.
            '';
          };
          disableCertificateVerification = mkOption {
            type = bool;
            default = false;
            description = ''
              Whether to skip validation of the server's certificate.
            '';
          };

          stripFromRoomDomain = mkOption {
            type = str;
            default = "0";
            example = "conference.";
            description = ''
              The prefix to strip from the room's JID domain to derive the call URL.
            '';
          };
          usageTimeout = mkOption {
            type = str;
            default = "0";
            example = "1 hour";
            description = ''
              The duration that the Jibri session can be.
              A value of zero means indefinitely.
            '';
          };
        };

        config =
          let
            nick = mkDefault (builtins.replaceStrings [ "." ] [ "-" ] (
              config.networking.hostName + optionalString (config.networking.domain != null) ".${config.networking.domain}"
            ));
          in
          {
            call.login.username = nick;
            control.muc.nickname = nick;
          };
      }));
    };
  };

  config = mkIf cfg.enable {
    users.groups.jibri = { };
    users.groups.plugdev = { };
    users.users.jibri = {
      isSystemUser = true;
      group = "jibri";
      home = "/var/lib/jibri";
      extraGroups = [ "jitsi-meet" "adm" "audio" "video" "plugdev" ];
    };

    systemd.services.jibri-xorg = {
      description = "Jitsi Xorg Process";

      after = [ "network.target" ];
      wantedBy = [ "jibri.service" "jibri-icewm.service" ];

      preStart = ''
        cp --no-preserve=mode,ownership ${pkgs.jibri}/etc/jitsi/jibri/* /var/lib/jibri
        mv /var/lib/jibri/{,.}asoundrc
      '';

      environment.DISPLAY = ":0";
      serviceConfig = {
        Type = "simple";

        User = "jibri";
        Group = "jibri";
        KillMode = "process";
        Restart = "on-failure";
        RestartPreventExitStatus = 255;

        StateDirectory = "jibri";

        ExecStart = "${pkgs.xorg.xorgserver}/bin/Xorg -nocursor -noreset +extension RANDR +extension RENDER -config ${pkgs.jibri}/etc/jitsi/jibri/xorg-video-dummy.conf -logfile /dev/null :0";
      };
    };

    systemd.services.jibri-icewm = {
      description = "Jitsi Window Manager";

      requires = [ "jibri-xorg.service" ];
      after = [ "jibri-xorg.service" ];
      wantedBy = [ "jibri.service" ];

      environment.DISPLAY = ":0";
      serviceConfig = {
        Type = "simple";

        User = "jibri";
        Group = "jibri";
        Restart = "on-failure";
        RestartPreventExitStatus = 255;

        StateDirectory = "jibri";

        ExecStart = "${pkgs.icewm}/bin/icewm-session";
      };
    };

    systemd.services.jibri = {
      description = "Jibri Process";

      requires = [ "jibri-icewm.service" "jibri-xorg.service" ];
      after = [ "network.target" ];
      wantedBy = [ "multi-user.target" ];

      path = with pkgs; [ chromedriver chromium ffmpeg-full ];

      script = (concatStrings (mapAttrsToList
        (name: env: ''
          export ${toVarName "${name}_control"}=$(cat ${env.control.login.passwordFile})
          export ${toVarName "${name}_call"}=$(cat ${env.call.login.passwordFile})
        '')
        cfg.xmppEnvironments))
      + ''
        ${pkgs.jre8_headless}/bin/java -Djava.util.logging.config.file=${./logging.properties-journal} -Dconfig.file=${configFile} -jar ${pkgs.jibri}/opt/jitsi/jibri/jibri.jar --config /var/lib/jibri/jibri.json
      '';

      environment.HOME = "/var/lib/jibri";

      serviceConfig = {
        Type = "simple";

        User = "jibri";
        Group = "jibri";
        Restart = "always";
        RestartPreventExitStatus = 255;

        StateDirectory = "jibri";
      };
    };

    systemd.tmpfiles.rules = [
      "d /var/log/jitsi/jibri 755 jibri jibri"
    ];



    # Configure Chromium to not show the "Chrome is being controlled by automatic test software" message.
    environment.etc."chromium/policies/managed/managed_policies.json".text = builtins.toJSON { CommandLineFlagSecurityWarningsEnabled = false; };
    warnings = [ "All security warnings for Chromium have been disabled. This is necessary for Jibri, but it also impacts all other uses of Chromium on this system." ];

    boot = {
      extraModprobeConfig = ''
        options snd-aloop enable=1,1,1,1,1,1,1,1
      '';
      kernelModules = [ "snd-aloop" ];
    };
  };

  meta.maintainers = lib.teams.jitsi.members;
}