2019-01-03 14:16:44 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.zoneminder;
|
2019-07-03 22:34:17 +01:00
|
|
|
fpm = config.services.phpfpm.pools.zoneminder;
|
2019-01-03 14:16:44 +00:00
|
|
|
pkg = pkgs.zoneminder;
|
|
|
|
|
|
|
|
dirName = pkg.dirName;
|
|
|
|
|
|
|
|
user = "zoneminder";
|
|
|
|
group = {
|
|
|
|
nginx = config.services.nginx.group;
|
|
|
|
none = user;
|
2019-08-13 22:52:01 +01:00
|
|
|
}.${cfg.webserver};
|
2019-01-03 14:16:44 +00:00
|
|
|
|
|
|
|
useNginx = cfg.webserver == "nginx";
|
|
|
|
|
|
|
|
defaultDir = "/var/lib/${user}";
|
|
|
|
home = if useCustomDir then cfg.storageDir else defaultDir;
|
|
|
|
|
2019-04-24 04:48:22 +01:00
|
|
|
useCustomDir = cfg.storageDir != null;
|
2019-01-03 14:16:44 +00:00
|
|
|
|
|
|
|
zms = "/cgi-bin/zms";
|
|
|
|
|
|
|
|
dirs = dirList: [ dirName ] ++ map (e: "${dirName}/${e}") dirList;
|
|
|
|
|
|
|
|
cacheDirs = [ "swap" ];
|
|
|
|
libDirs = [ "events" "exports" "images" "sounds" ];
|
|
|
|
|
|
|
|
dirStanzas = baseDir:
|
|
|
|
lib.concatStringsSep "\n" (map (e:
|
|
|
|
"ZM_DIR_${lib.toUpper e}=${baseDir}/${e}"
|
|
|
|
) libDirs);
|
|
|
|
|
|
|
|
defaultsFile = pkgs.writeText "60-defaults.conf" ''
|
|
|
|
# 01-system-paths.conf
|
|
|
|
${dirStanzas home}
|
|
|
|
ZM_PATH_ARP=${lib.getBin pkgs.nettools}/bin/arp
|
|
|
|
ZM_PATH_LOGS=/var/log/${dirName}
|
|
|
|
ZM_PATH_MAP=/dev/shm
|
|
|
|
ZM_PATH_SOCKS=/run/${dirName}
|
|
|
|
ZM_PATH_SWAP=/var/cache/${dirName}/swap
|
|
|
|
ZM_PATH_ZMS=${zms}
|
|
|
|
|
|
|
|
# 02-multiserver.conf
|
|
|
|
ZM_SERVER_HOST=
|
|
|
|
|
|
|
|
# Database
|
|
|
|
ZM_DB_TYPE=mysql
|
|
|
|
ZM_DB_HOST=${cfg.database.host}
|
2019-03-09 16:11:15 +00:00
|
|
|
ZM_DB_NAME=${cfg.database.name}
|
2019-06-21 16:36:02 +01:00
|
|
|
ZM_DB_USER=${cfg.database.username}
|
2019-01-03 14:16:44 +00:00
|
|
|
ZM_DB_PASS=${cfg.database.password}
|
|
|
|
|
|
|
|
# Web
|
|
|
|
ZM_WEB_USER=${user}
|
|
|
|
ZM_WEB_GROUP=${group}
|
|
|
|
'';
|
|
|
|
|
|
|
|
configFile = pkgs.writeText "80-nixos.conf" ''
|
|
|
|
# You can override defaults here
|
|
|
|
|
|
|
|
${cfg.extraConfig}
|
|
|
|
'';
|
|
|
|
|
|
|
|
in {
|
|
|
|
options = {
|
|
|
|
services.zoneminder = with lib; {
|
|
|
|
enable = lib.mkEnableOption ''
|
|
|
|
ZoneMinder
|
|
|
|
</para><para>
|
|
|
|
If you intend to run the database locally, you should set
|
|
|
|
`config.services.zoneminder.database.createLocally` to true. Otherwise,
|
|
|
|
when set to `false` (the default), you will have to create the database
|
|
|
|
and database user as well as populate the database yourself.
|
2020-02-09 18:28:32 +00:00
|
|
|
Additionally, you will need to run `zmupdate.pl` yourself when
|
|
|
|
upgrading to a newer version.
|
2019-01-03 14:16:44 +00:00
|
|
|
'';
|
|
|
|
|
|
|
|
webserver = mkOption {
|
|
|
|
type = types.enum [ "nginx" "none" ];
|
|
|
|
default = "nginx";
|
|
|
|
description = ''
|
|
|
|
The webserver to configure for the PHP frontend.
|
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
|
|
|
|
Set it to `none` if you want to configure it yourself. PRs are welcome
|
|
|
|
for support for other web servers.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
hostname = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "localhost";
|
|
|
|
description = ''
|
|
|
|
The hostname on which to listen.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
port = mkOption {
|
|
|
|
type = types.int;
|
|
|
|
default = 8095;
|
|
|
|
description = ''
|
|
|
|
The port on which to listen.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
openFirewall = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
Open the firewall port(s).
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
database = {
|
|
|
|
createLocally = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
Create the database and database user locally.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
host = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "localhost";
|
|
|
|
description = ''
|
|
|
|
Hostname hosting the database.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
name = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "zm";
|
|
|
|
description = ''
|
|
|
|
Name of database.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
username = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "zmuser";
|
|
|
|
description = ''
|
|
|
|
Username for accessing the database.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
password = mkOption {
|
|
|
|
type = types.str;
|
|
|
|
default = "zmpass";
|
|
|
|
description = ''
|
|
|
|
Username for accessing the database.
|
2019-06-21 16:36:02 +01:00
|
|
|
Not used if <literal>createLocally</literal> is set.
|
2019-01-03 14:16:44 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
cameras = mkOption {
|
|
|
|
type = types.int;
|
|
|
|
default = 1;
|
|
|
|
description = ''
|
|
|
|
Set this to the number of cameras you expect to support.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
storageDir = mkOption {
|
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
|
|
|
example = "/storage/tank";
|
|
|
|
description = ''
|
|
|
|
ZoneMinder can generate quite a lot of data, so in case you don't want
|
|
|
|
to use the default ${home}, you can override the path here.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
extraConfig = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
default = "";
|
|
|
|
description = ''
|
|
|
|
Additional configuration added verbatim to the configuration file.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = lib.mkIf cfg.enable {
|
|
|
|
|
2019-06-21 16:36:02 +01:00
|
|
|
assertions = [
|
|
|
|
{ assertion = cfg.database.createLocally -> cfg.database.username == user;
|
|
|
|
message = "services.zoneminder.database.username must be set to ${user} if services.zoneminder.database.createLocally is set true";
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2019-01-03 14:16:44 +00:00
|
|
|
environment.etc = {
|
|
|
|
"zoneminder/60-defaults.conf".source = defaultsFile;
|
|
|
|
"zoneminder/80-nixos.conf".source = configFile;
|
|
|
|
};
|
|
|
|
|
2019-06-11 07:30:45 +01:00
|
|
|
networking.firewall.allowedTCPPorts = lib.mkIf cfg.openFirewall [
|
|
|
|
cfg.port
|
|
|
|
6802 # zmtrigger
|
|
|
|
];
|
2019-01-03 14:16:44 +00:00
|
|
|
|
|
|
|
services = {
|
|
|
|
fcgiwrap = lib.mkIf useNginx {
|
|
|
|
enable = true;
|
|
|
|
preforkProcesses = cfg.cameras;
|
|
|
|
inherit user group;
|
|
|
|
};
|
|
|
|
|
|
|
|
mysql = lib.mkIf cfg.database.createLocally {
|
2019-06-21 16:36:02 +01:00
|
|
|
enable = true;
|
|
|
|
package = lib.mkDefault pkgs.mariadb;
|
2019-01-03 14:16:44 +00:00
|
|
|
ensureDatabases = [ cfg.database.name ];
|
2019-02-12 21:32:11 +00:00
|
|
|
ensureUsers = [{
|
2019-01-03 14:16:44 +00:00
|
|
|
name = cfg.database.username;
|
2019-02-12 21:32:11 +00:00
|
|
|
ensurePermissions = { "${cfg.database.name}.*" = "ALL PRIVILEGES"; };
|
|
|
|
}];
|
2019-01-03 14:16:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
nginx = lib.mkIf useNginx {
|
|
|
|
enable = true;
|
|
|
|
virtualHosts = {
|
2019-08-13 22:52:01 +01:00
|
|
|
${cfg.hostname} = {
|
2019-01-03 14:16:44 +00:00
|
|
|
default = true;
|
|
|
|
root = "${pkg}/share/zoneminder/www";
|
|
|
|
listen = [ { addr = "0.0.0.0"; inherit (cfg) port; } ];
|
|
|
|
extraConfig = let
|
|
|
|
fcgi = config.services.fcgiwrap;
|
|
|
|
in ''
|
|
|
|
index index.php;
|
|
|
|
|
|
|
|
location / {
|
|
|
|
try_files $uri $uri/ /index.php?$args =404;
|
|
|
|
|
2019-06-06 07:15:01 +01:00
|
|
|
rewrite ^/skins/.*/css/fonts/(.*)$ /fonts/$1 permanent;
|
|
|
|
|
2019-01-03 14:16:44 +00:00
|
|
|
location ~ /api/(css|img|ico) {
|
|
|
|
rewrite ^/api(.+)$ /api/app/webroot/$1 break;
|
|
|
|
try_files $uri $uri/ =404;
|
|
|
|
}
|
|
|
|
|
|
|
|
location ~ \.(gif|ico|jpg|jpeg|png)$ {
|
|
|
|
access_log off;
|
|
|
|
expires 30d;
|
|
|
|
}
|
|
|
|
|
|
|
|
location /api {
|
|
|
|
rewrite ^/api(.+)$ /api/app/webroot/index.php?p=$1 last;
|
|
|
|
}
|
|
|
|
|
|
|
|
location /cgi-bin {
|
|
|
|
gzip off;
|
|
|
|
|
|
|
|
include ${pkgs.nginx}/conf/fastcgi_params;
|
|
|
|
fastcgi_param SCRIPT_FILENAME ${pkg}/libexec/zoneminder/${zms};
|
|
|
|
fastcgi_param HTTP_PROXY "";
|
|
|
|
fastcgi_intercept_errors on;
|
|
|
|
|
|
|
|
fastcgi_pass ${fcgi.socketType}:${fcgi.socketAddress};
|
|
|
|
}
|
|
|
|
|
2019-06-26 19:45:55 +01:00
|
|
|
location /cache/ {
|
2019-08-04 19:48:23 +01:00
|
|
|
alias /var/cache/${dirName}/;
|
2019-01-03 14:16:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
location ~ \.php$ {
|
|
|
|
try_files $uri =404;
|
|
|
|
fastcgi_index index.php;
|
|
|
|
|
|
|
|
include ${pkgs.nginx}/conf/fastcgi_params;
|
|
|
|
fastcgi_param SCRIPT_FILENAME $request_filename;
|
|
|
|
fastcgi_param HTTP_PROXY "";
|
|
|
|
|
2019-07-03 22:34:17 +01:00
|
|
|
fastcgi_pass unix:${fpm.socket};
|
2019-01-03 14:16:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
phpfpm = lib.mkIf useNginx {
|
|
|
|
pools.zoneminder = {
|
2019-08-08 03:36:49 +01:00
|
|
|
inherit user group;
|
2020-04-11 00:11:24 +01:00
|
|
|
phpPackage = pkgs.php.withExtensions ({ enabled, all }: enabled ++ [ all.apcu ]);
|
2019-03-01 03:12:38 +00:00
|
|
|
phpOptions = ''
|
|
|
|
date.timezone = "${config.time.timeZone}"
|
|
|
|
'';
|
2019-08-08 03:36:49 +01:00
|
|
|
settings = lib.mapAttrs (name: lib.mkDefault) {
|
|
|
|
"listen.owner" = user;
|
|
|
|
"listen.group" = group;
|
|
|
|
"listen.mode" = "0660";
|
|
|
|
|
|
|
|
"pm" = "dynamic";
|
|
|
|
"pm.start_servers" = 1;
|
|
|
|
"pm.min_spare_servers" = 1;
|
|
|
|
"pm.max_spare_servers" = 2;
|
|
|
|
"pm.max_requests" = 500;
|
|
|
|
"pm.max_children" = 5;
|
|
|
|
"pm.status_path" = "/$pool-status";
|
|
|
|
"ping.path" = "/$pool-ping";
|
|
|
|
};
|
2019-01-03 14:16:44 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
systemd.services = {
|
2019-08-13 22:52:01 +01:00
|
|
|
zoneminder = with pkgs; {
|
2019-01-03 14:16:44 +00:00
|
|
|
inherit (zoneminder.meta) description;
|
|
|
|
documentation = [ "https://zoneminder.readthedocs.org/en/latest/" ];
|
|
|
|
path = [
|
|
|
|
coreutils
|
|
|
|
procps
|
|
|
|
psmisc
|
|
|
|
];
|
2019-06-21 16:36:02 +01:00
|
|
|
after = [ "nginx.service" ] ++ lib.optional cfg.database.createLocally "mysql.service";
|
2019-01-03 14:16:44 +00:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
|
|
|
restartTriggers = [ defaultsFile configFile ];
|
2019-06-21 16:36:02 +01:00
|
|
|
preStart = lib.optionalString useCustomDir ''
|
2019-01-03 14:16:44 +00:00
|
|
|
install -dm775 -o ${user} -g ${group} ${cfg.storageDir}/{${lib.concatStringsSep "," libDirs}}
|
2019-06-21 16:36:02 +01:00
|
|
|
'' + lib.optionalString cfg.database.createLocally ''
|
|
|
|
if ! test -e "/var/lib/${dirName}/db-created"; then
|
|
|
|
${config.services.mysql.package}/bin/mysql < ${pkg}/share/zoneminder/db/zm_create.sql
|
|
|
|
touch "/var/lib/${dirName}/db-created"
|
|
|
|
fi
|
2020-02-09 18:28:32 +00:00
|
|
|
|
|
|
|
${zoneminder}/bin/zmupdate.pl -nointeractive
|
2019-01-03 14:16:44 +00:00
|
|
|
'';
|
|
|
|
serviceConfig = {
|
|
|
|
User = user;
|
|
|
|
Group = group;
|
|
|
|
SupplementaryGroups = [ "video" ];
|
|
|
|
ExecStart = "${zoneminder}/bin/zmpkg.pl start";
|
|
|
|
ExecStop = "${zoneminder}/bin/zmpkg.pl stop";
|
|
|
|
ExecReload = "${zoneminder}/bin/zmpkg.pl restart";
|
|
|
|
PIDFile = "/run/${dirName}/zm.pid";
|
|
|
|
Type = "forking";
|
|
|
|
Restart = "on-failure";
|
|
|
|
RestartSec = "10s";
|
|
|
|
CacheDirectory = dirs cacheDirs;
|
|
|
|
RuntimeDirectory = dirName;
|
|
|
|
ReadWriteDirectories = lib.mkIf useCustomDir [ cfg.storageDir ];
|
|
|
|
StateDirectory = dirs (if useCustomDir then [] else libDirs);
|
|
|
|
LogsDirectory = dirName;
|
|
|
|
PrivateTmp = true;
|
|
|
|
ProtectSystem = "strict";
|
|
|
|
ProtectKernelTunables = true;
|
|
|
|
SystemCallArchitectures = "native";
|
|
|
|
NoNewPrivileges = true;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2019-08-13 22:52:01 +01:00
|
|
|
users.groups.${user} = {
|
2019-01-03 14:16:44 +00:00
|
|
|
gid = config.ids.gids.zoneminder;
|
|
|
|
};
|
|
|
|
|
2019-08-13 22:52:01 +01:00
|
|
|
users.users.${user} = {
|
2019-01-03 14:16:44 +00:00
|
|
|
uid = config.ids.uids.zoneminder;
|
|
|
|
group = user;
|
|
|
|
inherit home;
|
|
|
|
inherit (pkgs.zoneminder.meta) description;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
meta.maintainers = with lib.maintainers; [ peterhoeg ];
|
|
|
|
}
|