summaryrefslogtreecommitdiff
path: root/custom/motion.nix
blob: cd34548f46a8b8a5b5ae3ccd936d12cd52633b42 (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
{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.services.motion;

  cameraConfig = {
    options = {
      configFiles = mkOption {
        type = types.listOf types.path;
        default = [];
        description = "Config files to append to camera config";
      };
    
      extraConfig = mkOption {
        type = types.lines;
        default = "";
        description = "Config to append verbatim";
      };
    };
  };

  userConfig = {
    options = {
      name = mkOption {
        type = types.str;
        default = "motion";
        description = "Name of the dedicated user";
      };

      config = {
        type = types.attrs;
        default = {};
      };
    };
  };

  cameraConfig' = cameraId: cCfg: pkgs.writeText "${toString cameraId}.conf" ''
    camera_id ${toString cameraId}

    ${cCfg.extraConfig}
  '';

  compileCamera = cameraId: cCfg: ''
    cat ${cameraConfig' cameraId cCfg} ${escapeShellArgs cCfg.configFiles} > cameras/${cameraId}.conf
  '';

  motionConfig = pkgs.writeText "motion.conf" ''
    ${cfg.extraConfig}

    camera_dir cameras
  '';

  preStart = pkgs.writeScript "motion-pre-start" ''
    cat ${motionConfig} ${escapShellArgs cfg.configFiles} > motion.conf
      
    mkdir -p cameras
    ${concatStringsSep "\n" (imap0 compileCamera cfg.cameras)}

    chown -R ${cfg.user.name} .
    chmod -R u+rX .
  '';
in {
  options = {
    services.motion = {
      cameras = mkOption{
        type = types.listOf (types.submodule cameraConfig);
        default = [];
      };
    
      extraConfig = mkOption {
        type = types.lines;
        default = "";
        description = "Config to append verbatim";
      };

      configFiles = mkOption {
        type = types.listOf types.path;
        default = [];
        description = "Config files to append to service config";
      };
      
      user = mkOption {
        type = types.submodule userConfig;
        description = "Overrides for the motion user";
      };
    };
  };

  config = mkIf (cfg.cameras != []) {
    users.users."${cfg.user.name}" = {
      name = cfg.user.name;
      isSystemUser = true;
      isNormalUser = false;
      description = "User for motion daemon";
    } // cfg.user.config;

    systemd.services."motion" = {
      serviceConfig = {
        User = cfg.user.name;
        RuntimeDirectory = [ "motion" ];
        RuntimeDirectoryMode = "0700";
        WorkingDirectory = "/run/motion";
        ExecStart = "${pkgs.motion}/bin/motion -n -c motion.conf";
        ExecStartPre = "+${preStart}";
      };

      wantedBy = [ "default.target" ];
    };
  };
}