summaryrefslogtreecommitdiff
path: root/custom/motion.nix
diff options
context:
space:
mode:
Diffstat (limited to 'custom/motion.nix')
-rw-r--r--custom/motion.nix112
1 files changed, 112 insertions, 0 deletions
diff --git a/custom/motion.nix b/custom/motion.nix
new file mode 100644
index 00000000..10d8db66
--- /dev/null
+++ b/custom/motion.nix
@@ -0,0 +1,112 @@
1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6 cfg = config.services.motion;
7
8 cameraConfig = {
9 options = {
10 configFiles = mkOption {
11 type = types.listOf types.path;
12 default = [];
13 description = "Config files to append to camera config";
14 };
15
16 extraConfig = mkOption {
17 type = types.lines;
18 default = "";
19 description = "Config to append verbatim";
20 };
21 };
22 };
23
24 userConfig = {
25 options = {
26 name = mkOption {
27 type = types.str;
28 default = "motion";
29 description = "Name of the dedicated user";
30 };
31
32 config = {
33 type = types.attrs;
34 default = {};
35 };
36 };
37 };
38
39 cameraConfig' = cameraId: cCfg: pkgs.writeText "${toString cameraId}.conf" ''
40 camera_id ${toString cameraId}
41
42 ${cCfg.extraConfig}
43 '';
44
45 compileCamera = cameraId: cCfg: ''
46 cat ${cameraConfig' cameraId cCfg} ${escapeShellArgs cCfg.configFiles} > cameras/${cameraId}.conf
47 '';
48
49 motionConfig = pkgs.writeText "motion.conf" ''
50 ${cfg.extraConfig}
51
52 camera_dir cameras
53 '';
54
55 preStart = pkgs.writeStript "motion-pre-start" ''
56 cat ${motionConfig} ${escapShellArgs cfg.configFiles} > motion.conf
57
58 mkdir -p cameras
59 ${concatStringsSep "\n" (imap0 compileCamera cfg.cameras)}
60
61 chown -R ${cfg.user.name} .
62 chmod -R u+rX .
63 '';
64in {
65 options = {
66 services.motion = {
67 cameras = mkOption{
68 type = types.listOf (types.submodule cameraConfig);
69 default = [];
70 };
71
72 extraConfig = mkOption {
73 type = types.lines;
74 default = "";
75 description = "Config to append verbatim";
76 };
77
78 configFiles = mkOption {
79 type = types.listOf types.path;
80 default = [];
81 description = "Config files to append to service config";
82 };
83
84 user = mkOption {
85 type = types.submodule userConfig;
86 description = "Overrides for the motion user";
87 };
88 };
89 };
90
91 config = mkIf (cfg.cameras != []) {
92 users.users."${cfg.user.name}" = {
93 name = cfg.user.name;
94 isSystemUser = true;
95 isNormalUser = false;
96 description = "User for motion daemon";
97 } // cfg.user.config;
98
99 systemd.services."motion" = {
100 serviceConfig = {
101 User = cfg.user.name;
102 RuntimeDirectory = [ "motion" ];
103 RuntimeDirectoryMode = "0700";
104 WorkingDirectory = "/run/motion";
105 ExecStart = "${pkgs.motion}/bin/motion -n -c motion.conf";
106 ExecStartPre = "+${preStart}";
107 };
108
109 WantedBy = [ "default.target" ];
110 };
111 };
112}