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 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 custom/motion.nix (limited to 'custom') 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" ]; + }; + }; +} -- cgit v1.2.3