chore(motion): clean up settings

This commit is contained in:
arcnmx 2024-06-09 11:21:15 -07:00
parent 690f86b3bd
commit f24b9582f7
4 changed files with 134 additions and 47 deletions

View file

@ -1,4 +1,51 @@
{
let
cameraModule = {
pkgs,
config,
gensokyo-zone,
name,
lib,
lib'motion,
...
}: let
inherit (lib.options) mkOption mkEnableOption;
inherit (lib.modules) mkIf mkMerge mkOptionDefault;
inherit (lib.strings) hasPrefix;
in {
options = with lib.types; {
enable = mkEnableOption "camera" // {
default = true;
};
settings = mkOption {
type = attrsOf (oneOf [str int bool]);
description = "https://motion-project.github.io/motion_config.html";
};
extraConfig = mkOption {
type = lines;
default = "";
};
configText = mkOption {
type = lines;
internal = true;
};
configFile = mkOption {
type = path;
};
};
config = let
configFile = pkgs.writeText "motion.conf" config.configText;
in {
settings = {
videodevice = mkIf (hasPrefix "/" name) (mkOptionDefault name);
};
configFile = mkOptionDefault "${configFile}";
configText = mkMerge (
(lib'motion.mkMotionSettings config.settings)
++ [config.extraConfig]
);
};
};
in {
pkgs,
config,
gensokyo-zone,
@ -6,23 +53,26 @@
lib,
...
}: let
inherit (gensokyo-zone.lib) mapOptionDefaults;
inherit (lib.options) mkOption mkPackageOption mkEnableOption;
inherit (lib.modules) mkIf mkMerge mkAfter mkOptionDefault;
inherit (lib.attrsets) mapAttrsToList;
inherit (lib.attrsets) attrValues mapAttrsToList;
inherit (lib.lists) filter;
inherit (lib.meta) getExe;
cfg = config.services.motion;
mkMotionValue = value:
if value == true
then "on"
else if value == false
then "off"
else toString value;
mkMotionSetting = key: value: "${key} ${mkMotionValue value}";
lib'motion = config.lib.motion;
in {
options.services.motion = with lib.types; {
enable = mkEnableOption "motion";
package = mkPackageOption pkgs "motion" {};
cameras = mkOption {
type = attrsOf (submoduleWith {
modules = [ cameraModule ];
specialArgs = {
inherit pkgs gensokyo-zone lib'motion;
nixosConfig = config;
};
});
};
dataDir = mkOption {
type = path;
default = "/var/lib/motion";
@ -37,7 +87,7 @@ in {
};
settings = mkOption {
type = attrsOf (oneOf [str int bool]);
description = "https://linux.die.net/man/1/motion";
description = "https://motion-project.github.io/motion_config.html";
};
extraArgs = mkOption {
type = listOf str;
@ -57,14 +107,19 @@ in {
};
config.services.motion = let
configFile = pkgs.writeText "motion.conf" cfg.configText;
enableIPv6 = mkIf config.networking.enableIPv6 (mkOptionDefault true);
enabledCameras = filter (camera: camera.enable) (attrValues cfg.cameras);
in {
settings = mapOptionDefaults {
target_dir = cfg.dataDir;
settings = {
target_dir = mkOptionDefault cfg.dataDir;
ipv6_enabled = enableIPv6;
webcontrol_ipv6 = enableIPv6;
};
configFile = mkOptionDefault "${configFile}";
configText = mkMerge (
(mapAttrsToList mkMotionSetting cfg.settings)
++ [(mkAfter cfg.extraConfig)]
(lib'motion.mkMotionSettings cfg.settings)
++ [cfg.extraConfig]
++ map (camera: mkAfter "camera ${camera.configFile}") enabledCameras
);
};
config.users = mkIf cfg.enable {
@ -101,4 +156,14 @@ in {
];
};
};
config.lib.motion = {
mkMotionValue = value:
if value == true
then "on"
else if value == false
then "off"
else toString value;
mkMotionSetting = key: value: "${key} ${lib'motion.mkMotionValue value}";
mkMotionSettings = mapAttrsToList lib'motion.mkMotionSetting;
};
}