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;
|
|
|
|
};
|
|
|
|
|
2023-03-19 09:56:51 -07:00
|
|
|
pool-size = mkOption {
|
|
|
|
type = int;
|
|
|
|
description = "Number of nets to initialize.";
|
|
|
|
default = 5;
|
|
|
|
};
|
|
|
|
|
2023-01-06 14:46:11 -08:00
|
|
|
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
|
|
|
|
2023-03-15 12:31:01 -07:00
|
|
|
detection-timeout = mkOption {
|
|
|
|
type = int;
|
|
|
|
description = "Time in seconds to allow for detection to start.";
|
|
|
|
default = 5;
|
|
|
|
};
|
|
|
|
|
2023-03-19 09:56:51 -07:00
|
|
|
hostname = mkOption {
|
|
|
|
type = nullOr str;
|
|
|
|
description = "Hostname on which to listen for requests.";
|
|
|
|
default = null;
|
|
|
|
};
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2023-01-07 13:41:23 -08:00
|
|
|
delay = mkOption {
|
2023-01-07 12:38:14 -08:00
|
|
|
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 {
|
2023-03-19 09:56:51 -07:00
|
|
|
services.nginx = mkIf (!isNull cfg.hostname) {
|
|
|
|
enable = true;
|
2023-03-19 10:25:27 -07:00
|
|
|
recommendedOptimisation = true;
|
2023-03-19 09:56:51 -07:00
|
|
|
recommendedProxySettings = true;
|
|
|
|
recommendedGzipSettings = true;
|
|
|
|
|
|
|
|
virtualHosts."${cfg.hostname}" = {
|
|
|
|
locations."/".proxyPass = "http://127.0.0.1:${toString cfg.port}";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2023-06-05 11:35:04 -07:00
|
|
|
systemd.services = {
|
|
|
|
objectifier = {
|
|
|
|
after = [ "network-online.target" ];
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
environment = {
|
|
|
|
OBJECTIFIER_YOLOV3_CONFIG = "${pkgs.yolov3-data}/yolov3.cfg";
|
|
|
|
OBJECTIFIER_YOLOV3_WEIGHTS = "${pkgs.yolov3-data}/yolov3.weights";
|
|
|
|
OBJECTIFIER_YOLOV3_LABELS = "${pkgs.yolov3-data}/labels";
|
|
|
|
OBJECTIFIER_BUFFER_SIZE = "524288";
|
|
|
|
OBJECTIFIER_CLEANUP_MAX_AGE = toString cfg.cleanup.max_file_age;
|
|
|
|
OBJECTIFIER_CLEANUP_DELAY = toString cfg.cleanup.delay;
|
|
|
|
OBJECTIFIER_TIMEOUT = toString cfg.detection-timeout;
|
|
|
|
OBJECTIFIER_POOL_SIZE = toString cfg.pool-size;
|
|
|
|
};
|
|
|
|
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}";
|
|
|
|
StateDirectory = "objectifier";
|
|
|
|
CacheDirectory = "objectifier";
|
|
|
|
RuntimeDirectory = "objectifier";
|
|
|
|
LimitNOFILE = 4096;
|
|
|
|
Restart = "on-failure";
|
|
|
|
RestartSec = "5s";
|
|
|
|
Type = "simple";
|
|
|
|
ExecStart = let
|
|
|
|
bindClause = concatStringsSep " "
|
|
|
|
(map (addr: "--bind ${addr}:${toString cfg.port}")
|
|
|
|
cfg.listen-addresses);
|
|
|
|
in (concatStringsSep " " [
|
|
|
|
"${pkgs.objectifier}/bin/objectifier"
|
|
|
|
bindClause
|
|
|
|
"--workers ${toString cfg.workers}"
|
|
|
|
]);
|
|
|
|
};
|
2023-01-06 14:46:11 -08:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|