summaryrefslogtreecommitdiffstats
path: root/nixos/modules/services/video/motioneye.nix
blob: 6a8c2a01cf1ac0b0bb65a6944928a417b4e5a625 (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
{
  config,
  pkgs,
  lib,
  ...
}:

let
  cfg = config.services.motioneye;
in
{
  options.services.motioneye = {
    enable = lib.mkEnableOption "motionEye";

    packages = {
      motioneye = lib.mkPackageOption pkgs "motioneye" { };
      motion = lib.mkPackageOption pkgs "motion" { };
      ffmpeg = lib.mkPackageOption pkgs "ffmpeg-headless" { };
    };

    user = lib.mkOption {
      type = lib.types.str;
      default = "motioneye";
      description = "User to run motionEye under.";
    };

    group = lib.mkOption {
      type = lib.types.str;
      default = "motioneye";
      description = "Group to run motionEye under.";
    };

    settings = lib.mkOption {
      type = lib.types.attrsOf lib.types.str;
      default = { };
      defaultText = lib.literalExpression /* nix */ ''
        {
          conf_path = lib.mkDefault "/var/lib/motioneye/conf";
          run_path = lib.mkDefault "/run/motioneye";
          log_path = lib.mkDefault "/var/log/motioneye";
          media_path = lib.mkDefault "/var/lib/motioneye/media";
        }
      '';
      description = ''
        Configuration to put in motioneye.conf.
        See <https://github.com/motioneye-project/motioneye/wiki/Configuration-File>  for more details.
      '';
    };
  };

  config = lib.mkIf cfg.enable {
    services.motioneye.settings = {
      conf_path = lib.mkDefault "/var/lib/motioneye/conf";
      run_path = lib.mkDefault "/run/motioneye";
      log_path = lib.mkDefault "/var/log/motioneye";
      media_path = lib.mkDefault "/var/lib/motioneye/media";
    };

    users = {
      groups.${cfg.group} = { };
      users.${cfg.user} = {
        inherit (cfg) group;
        isSystemUser = true;

        # allow v4l access
        extraGroups = [ "video" ];
      };
    };

    systemd.tmpfiles.settings.motioneye =
      let
        config = {
          d = {
            inherit (cfg) user group;
            mode = "0750";
          };
        };
      in
      {
        "${cfg.settings.conf_path}" = config;
        "${cfg.settings.run_path}" = config;
        "${cfg.settings.log_path}" = config;
        "${cfg.settings.media_path}" = config;
      };

    environment.etc."motioneye/motioneye.conf".text = lib.concatMapAttrsStringSep "\n" (
      key: value: "${key} ${lib.escapeShellArg value}"
    ) cfg.settings;

    # https://github.com/motioneye-project/motioneye/blob/main/motioneye/extra/motioneye.systemd
    systemd.services.motioneye = {
      description = "motionEye Server";
      after = [
        "network.target"
        "local-fs.target"
        "remote-fs.target"
      ];
      wantedBy = [ "multi-user.target" ];
      path =
        (with pkgs; [
          which
          v4l-utils
        ])
        ++ [
          cfg.packages.motion
          cfg.packages.ffmpeg
        ];
      restartTriggers = [
        config.environment.etc."motioneye/motioneye.conf".source
      ];
      serviceConfig = {
        User = cfg.user;
        Group = cfg.group;
        RuntimeDirectory = "motioneye";
        LogsDirectory = "motioneye";
        StateDirectory = "motioneye";
        ExecStart = "${lib.getExe' cfg.packages.motioneye "meyectl"} startserver -c /etc/motioneye/motioneye.conf";
        Restart = "on-abort";
      };
    };
  };
}