summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--custom/motion.nix112
-rw-r--r--odin.nix54
2 files changed, 166 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}
diff --git a/odin.nix b/odin.nix
index dd2255db..b9d3a9be 100644
--- a/odin.nix
+++ b/odin.nix
@@ -12,6 +12,7 @@
12 ./custom/uucp.nix 12 ./custom/uucp.nix
13 ./custom/uucp-mediaserver.nix 13 ./custom/uucp-mediaserver.nix
14 ./custom/borgbackup.nix 14 ./custom/borgbackup.nix
15 ./custom/motion.nix
15 ]; 16 ];
16 17
17 # Use the GRUB 2 boot loader. 18 # Use the GRUB 2 boot loader.
@@ -198,6 +199,59 @@
198 ''; 199 '';
199 }; 200 };
200 201
202 services.motion = {
203 cameras = [
204 {
205 extraConfig = ''
206 camera_name ipcam01
207
208 netcam_url mjpeg://ipcam01.hlidskjalf.yggdrasil:80/mjpeg.cgi
209 netcam_keepalive on
210 width 640
211 height 480
212
213 stream_port 8081
214
215 despeckle_filter EedDl
216 threshold_tune on
217 noise_tune on
218 lightswitch 50
219 minimum_motion_frames 5
220 '';
221 configFiles = [ "/etc/motion/ipcam01.secret" ];
222 }
223 ];
224
225 extraConfig = ''
226 text_left "%$\n%F %T %Z"
227 text_right ""
228 text_changes off
229
230 event_gap 20
231
232 output_pictures off
233
234 ffmpeg_output_movies on
235 ffmpeg_video_codec mkv
236 ffmpeg_bps 500000
237 max_movie_time 600
238
239 target_dir /srv/hlidskjalf
240 movie_filename %Y-%m-%d/%H-%M-%S.%$
241
242 stream_localhost off
243 stream_auth_method 1
244
245 stream_quality 80
246 stream_maxrate 100
247 stream_motion on
248
249 webcontrol_port 0
250 '';
251
252 configFiles = [ "/etc/motion/motion.secret" ];
253 };
254
201 system.autoUpgrade.enable = true; 255 system.autoUpgrade.enable = true;
202 system.stateVersion = "18.09"; 256 system.stateVersion = "18.09";
203 257