summary refs log tree commit diff
path: root/nixos/modules/services/video/mediamtx.nix
blob: c3abd9cdcc5cb806a030f85bc95b57f2843cf5b7 (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
{ config, lib, pkgs, ... }:

let
  cfg = config.services.mediamtx;
  format = pkgs.formats.yaml {};
in
{
  meta.maintainers = with lib.maintainers; [ fpletz ];

  options = {
    services.mediamtx = {
      enable = lib.mkEnableOption (lib.mdDoc "MediaMTX");

      package = lib.mkPackageOptionMD pkgs "mediamtx" { };

      settings = lib.mkOption {
        description = lib.mdDoc ''
          Settings for MediaMTX. Refer to the defaults at
          <https://github.com/bluenviron/mediamtx/blob/main/mediamtx.yml>.
        '';
        type = format.type;
        default = {};
        example = {
          paths = {
            cam = {
              runOnInit = "\${lib.getExe pkgs.ffmpeg} -f v4l2 -i /dev/video0 -f rtsp rtsp://localhost:$RTSP_PORT/$RTSP_PATH";
              runOnInitRestart = true;
            };
          };
        };
      };

      env = lib.mkOption {
        type = with lib.types; attrsOf anything;
        description = lib.mdDoc "Extra environment variables for MediaMTX";
        default = {};
        example = {
          MTX_CONFKEY = "mykey";
        };
      };

      allowVideoAccess = lib.mkEnableOption (lib.mdDoc ''
        Enable access to video devices like cameras on the system.
      '');
    };
  };

  config = lib.mkIf cfg.enable {
    # NOTE: mediamtx watches this file and automatically reloads if it changes
    environment.etc."mediamtx.yaml".source = format.generate "mediamtx.yaml" cfg.settings;

    systemd.services.mediamtx = {
      after = [ "network.target" ];
      wantedBy = [ "multi-user.target" ];

      environment = cfg.env;

      serviceConfig = {
        DynamicUser = true;
        User = "mediamtx";
        Group = "mediamtx";
        SupplementaryGroups = lib.mkIf cfg.allowVideoAccess "video";
        ExecStart = "${cfg.package}/bin/mediamtx /etc/mediamtx.yaml";
      };
    };
  };
}