2023-01-06 14:46:11 -08:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
let
|
|
|
|
cfg = config.services.objectifier;
|
|
|
|
|
|
|
|
pythonYolo = pkgs.python3.withPackages (pyPkgs:
|
|
|
|
with pyPkgs; [
|
|
|
|
fastapi
|
|
|
|
gunicorn
|
|
|
|
opencv4
|
|
|
|
python-multipart
|
|
|
|
uvicorn
|
|
|
|
]);
|
|
|
|
|
|
|
|
in {
|
|
|
|
options.services.objectifier = with types; {
|
|
|
|
enable = mkEnableOption "Enable Objectifier object-detection web sevice.";
|
|
|
|
|
|
|
|
port = mkOption {
|
|
|
|
type = port;
|
|
|
|
description = "Port on which to run the Objectifier web service.";
|
|
|
|
default = 5121;
|
|
|
|
};
|
|
|
|
|
|
|
|
workers = mkOption {
|
|
|
|
type = int;
|
|
|
|
description = "Number of worker threads to launch.";
|
|
|
|
default = 3;
|
|
|
|
};
|
|
|
|
|
|
|
|
listen-addresses = mkOption {
|
|
|
|
type = listOf str;
|
|
|
|
description =
|
|
|
|
"List of IP addresses on which to listen for incoming requests.";
|
|
|
|
default = [ "127.0.0.1" ];
|
|
|
|
};
|
2023-01-07 12:38:14 -08:00
|
|
|
|
|
|
|
cleanup = {
|
|
|
|
max_file_age = mkOption {
|
|
|
|
type = int;
|
|
|
|
description =
|
|
|
|
"Maximum age of a file (in seconds), after which it will be removed.";
|
2023-01-07 12:43:18 -08:00
|
|
|
default = (60 * 60 * 8); # 8 hours
|
2023-01-07 12:38:14 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
delay = {
|
|
|
|
type = int;
|
|
|
|
description = "Time between cleanup sweeps.";
|
2023-01-07 12:43:18 -08:00
|
|
|
default = (60 * 10); # 10 minutes
|
2023-01-07 12:38:14 -08:00
|
|
|
};
|
|
|
|
};
|
2023-01-06 14:46:11 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf cfg.enable {
|
|
|
|
systemd.services.objectifier = {
|
|
|
|
after = [ "network-online.target" ];
|
2023-01-06 14:59:27 -08:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
2023-01-06 14:46:11 -08:00
|
|
|
reloadIfChanged = true;
|
|
|
|
path = with pkgs; [ pythonYolo ];
|
|
|
|
environment = {
|
2023-01-06 18:13:26 -08:00
|
|
|
OBJECTIFIER_YOLOV3_CONFIG = "${pkgs.yolov3-data}/yolov3.cfg";
|
|
|
|
OBJECTIFIER_YOLOV3_WEIGHTS = "${pkgs.yolov3-data}/yolov3.weights";
|
|
|
|
OBJECTIFIER_YOLOV3_LABELS = "${pkgs.yolov3-data}/labels";
|
2023-01-06 18:20:05 -08:00
|
|
|
OBJECTIFIER_BUFFER_SIZE = "524288";
|
2023-01-07 12:38:14 -08:00
|
|
|
OBJECTIFIER_CLEANUP_MAX_AGE = toString cfg.cleanup.max_file_age;
|
|
|
|
OBJECTIFIER_CLEANUP_DELAY = toString cfg.cleanup.delay;
|
2023-01-06 14:46:11 -08:00
|
|
|
};
|
|
|
|
serviceConfig = {
|
|
|
|
# PrivateUsers = true;
|
|
|
|
# PrivateDevices = true;
|
|
|
|
# PrivateTmp = true;
|
|
|
|
# PrivateMounts = true;
|
|
|
|
# ProtectControlGroups = true;
|
|
|
|
# ProtectKernelTunables = true;
|
|
|
|
# ProtectKernelModules = true;
|
|
|
|
# ProtectSystem = true;
|
|
|
|
# ProtectHostname = true;
|
|
|
|
# ProtectHome = true;
|
|
|
|
# ProtectClock = true;
|
|
|
|
# ProtectKernelLogs = true;
|
|
|
|
# DynamicUser = true;
|
|
|
|
# MemoryDenyWriteExecute = true;
|
|
|
|
# RestrictRealtime = true;
|
|
|
|
# LockPersonality = true;
|
|
|
|
# PermissionsStartOnly = true;
|
|
|
|
WorkingDirectory = "${pkgs.objectifier}";
|
2023-01-06 14:59:27 -08:00
|
|
|
LimitNOFILE = 4096;
|
2023-01-06 14:46:11 -08:00
|
|
|
Restart = "on-failure";
|
2023-01-06 14:59:27 -08:00
|
|
|
RestartSec = "5s";
|
2023-01-06 14:46:11 -08:00
|
|
|
Type = "simple";
|
|
|
|
PIDFile = "/run/objectifier.pid";
|
|
|
|
ExecStart = let
|
2023-01-06 16:11:56 -08:00
|
|
|
bindClause = concatStringsSep " "
|
2023-01-06 16:16:33 -08:00
|
|
|
(map (addr: "--bind ${addr}:${toString cfg.port}")
|
|
|
|
cfg.listen-addresses);
|
2023-01-06 15:50:48 -08:00
|
|
|
in (concatStringsSep " " [
|
2023-01-06 14:46:11 -08:00
|
|
|
"gunicorn"
|
|
|
|
bindClause
|
2023-01-06 16:20:17 -08:00
|
|
|
"--workers ${toString cfg.workers}"
|
2023-01-06 14:46:11 -08:00
|
|
|
"-k uvicorn.workers.UvicornWorker"
|
|
|
|
"--pid /run/objectifier.pid"
|
2023-01-06 16:11:56 -08:00
|
|
|
"objectifier:app"
|
2023-01-06 15:50:48 -08:00
|
|
|
]);
|
2023-01-06 14:46:11 -08:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|