From 6f58127130cfd2b87ddf29b8bd1279ba9f470ada Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Fri, 13 Apr 2018 13:18:14 +0200 Subject: motion on odin --- custom/motion.nix | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ odin.nix | 54 ++++++++++++++++++++++++++ 2 files changed, 166 insertions(+) create mode 100644 custom/motion.nix 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 @@ +{ 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.writeStript "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" ]; + }; + }; +} diff --git a/odin.nix b/odin.nix index dd2255db..b9d3a9be 100644 --- a/odin.nix +++ b/odin.nix @@ -12,6 +12,7 @@ ./custom/uucp.nix ./custom/uucp-mediaserver.nix ./custom/borgbackup.nix + ./custom/motion.nix ]; # Use the GRUB 2 boot loader. @@ -198,6 +199,59 @@ ''; }; + services.motion = { + cameras = [ + { + extraConfig = '' + camera_name ipcam01 + + netcam_url mjpeg://ipcam01.hlidskjalf.yggdrasil:80/mjpeg.cgi + netcam_keepalive on + width 640 + height 480 + + stream_port 8081 + + despeckle_filter EedDl + threshold_tune on + noise_tune on + lightswitch 50 + minimum_motion_frames 5 + ''; + configFiles = [ "/etc/motion/ipcam01.secret" ]; + } + ]; + + extraConfig = '' + text_left "%$\n%F %T %Z" + text_right "" + text_changes off + + event_gap 20 + + output_pictures off + + ffmpeg_output_movies on + ffmpeg_video_codec mkv + ffmpeg_bps 500000 + max_movie_time 600 + + target_dir /srv/hlidskjalf + movie_filename %Y-%m-%d/%H-%M-%S.%$ + + stream_localhost off + stream_auth_method 1 + + stream_quality 80 + stream_maxrate 100 + stream_motion on + + webcontrol_port 0 + ''; + + configFiles = [ "/etc/motion/motion.secret" ]; + }; + system.autoUpgrade.enable = true; system.stateVersion = "18.09"; -- cgit v1.2.3