2021-12-19 01:06:50 +00:00
|
|
|
{ config, lib, options, pkgs, ... }:
|
2020-09-25 04:45:31 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.services.epgstation;
|
2021-12-19 01:06:50 +00:00
|
|
|
opt = options.services.epgstation;
|
2020-09-25 04:45:31 +00:00
|
|
|
|
2022-03-30 09:31:56 +00:00
|
|
|
description = "EPGStation: DVR system for Mirakurun-managed TV tuners";
|
|
|
|
|
2020-09-25 04:45:31 +00:00
|
|
|
username = config.users.users.epgstation.name;
|
|
|
|
groupname = config.users.users.epgstation.group;
|
2022-03-30 09:31:56 +00:00
|
|
|
mirakurun = {
|
|
|
|
sock = config.services.mirakurun.unixSocket;
|
|
|
|
option = options.services.mirakurun.unixSocket;
|
|
|
|
};
|
2020-09-25 04:45:31 +00:00
|
|
|
|
2022-03-30 09:31:56 +00:00
|
|
|
yaml = pkgs.formats.yaml { };
|
|
|
|
settingsTemplate = yaml.generate "config.yml" cfg.settings;
|
2020-09-25 04:45:31 +00:00
|
|
|
preStartScript = pkgs.writeScript "epgstation-prestart" ''
|
|
|
|
#!${pkgs.runtimeShell}
|
|
|
|
|
2022-03-30 09:31:56 +00:00
|
|
|
DB_PASSWORD_FILE=${lib.escapeShellArg cfg.database.passwordFile}
|
|
|
|
|
|
|
|
if [[ ! -f "$DB_PASSWORD_FILE" ]]; then
|
|
|
|
printf "[FATAL] File containing the DB password was not found in '%s'. Double check the NixOS option '%s'." \
|
|
|
|
"$DB_PASSWORD_FILE" ${lib.escapeShellArg opt.database.passwordFile} >&2
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
DB_PASSWORD="$(head -n1 ${lib.escapeShellArg cfg.database.passwordFile})"
|
2020-09-25 04:45:31 +00:00
|
|
|
|
|
|
|
# setup configuration
|
2022-03-30 09:31:56 +00:00
|
|
|
touch /etc/epgstation/config.yml
|
|
|
|
chmod 640 /etc/epgstation/config.yml
|
2020-09-25 04:45:31 +00:00
|
|
|
sed \
|
|
|
|
-e "s,@dbPassword@,$DB_PASSWORD,g" \
|
2022-03-30 09:31:56 +00:00
|
|
|
${settingsTemplate} > /etc/epgstation/config.yml
|
|
|
|
chown "${username}:${groupname}" /etc/epgstation/config.yml
|
2020-09-25 04:45:31 +00:00
|
|
|
|
|
|
|
# NOTE: Use password authentication, since mysqljs does not yet support auth_socket
|
|
|
|
if [ ! -e /var/lib/epgstation/db-created ]; then
|
2021-06-06 07:54:09 +00:00
|
|
|
${pkgs.mariadb}/bin/mysql -e \
|
2020-09-25 04:45:31 +00:00
|
|
|
"GRANT ALL ON \`${cfg.database.name}\`.* TO '${username}'@'localhost' IDENTIFIED by '$DB_PASSWORD';"
|
|
|
|
touch /var/lib/epgstation/db-created
|
|
|
|
fi
|
|
|
|
'';
|
|
|
|
|
2021-12-06 16:07:01 +00:00
|
|
|
streamingConfig = lib.importJSON ./streaming.json;
|
2022-03-30 09:31:56 +00:00
|
|
|
logConfig = yaml.generate "logConfig.yml" {
|
2020-09-25 04:45:31 +00:00
|
|
|
appenders.stdout.type = "stdout";
|
|
|
|
categories = {
|
|
|
|
default = { appenders = [ "stdout" ]; level = "info"; };
|
|
|
|
system = { appenders = [ "stdout" ]; level = "info"; };
|
|
|
|
access = { appenders = [ "stdout" ]; level = "info"; };
|
|
|
|
stream = { appenders = [ "stdout" ]; level = "info"; };
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-03-30 09:31:56 +00:00
|
|
|
# Deprecate top level options that are redundant.
|
|
|
|
deprecateTopLevelOption = config:
|
|
|
|
lib.mkRenamedOptionModule
|
|
|
|
([ "services" "epgstation" ] ++ config)
|
|
|
|
([ "services" "epgstation" "settings" ] ++ config);
|
|
|
|
|
|
|
|
removeOption = config: instruction:
|
|
|
|
lib.mkRemovedOptionModule
|
|
|
|
([ "services" "epgstation" ] ++ config)
|
|
|
|
instruction;
|
2020-09-25 04:45:31 +00:00
|
|
|
in
|
|
|
|
{
|
2022-03-30 09:31:56 +00:00
|
|
|
meta.maintainers = with lib.maintainers; [ midchildan ];
|
2020-09-25 04:45:31 +00:00
|
|
|
|
2022-03-30 09:31:56 +00:00
|
|
|
imports = [
|
|
|
|
(deprecateTopLevelOption [ "port" ])
|
|
|
|
(deprecateTopLevelOption [ "socketioPort" ])
|
|
|
|
(deprecateTopLevelOption [ "clientSocketioPort" ])
|
|
|
|
(removeOption [ "basicAuth" ]
|
|
|
|
"Use a TLS-terminated reverse proxy with authentication instead.")
|
|
|
|
];
|
2020-09-25 04:45:31 +00:00
|
|
|
|
2022-03-30 09:31:56 +00:00
|
|
|
options.services.epgstation = {
|
2022-09-09 14:08:57 +00:00
|
|
|
enable = lib.mkEnableOption (lib.mdDoc description);
|
2020-09-25 04:45:31 +00:00
|
|
|
|
2023-05-24 13:37:59 +00:00
|
|
|
package = lib.mkPackageOptionMD pkgs "epgstation" { };
|
|
|
|
|
|
|
|
ffmpeg = lib.mkPackageOptionMD pkgs "ffmpeg" {
|
|
|
|
default = [ "ffmpeg-headless" ];
|
|
|
|
example = "pkgs.ffmpeg-full";
|
2020-09-25 04:45:31 +00:00
|
|
|
};
|
|
|
|
|
2022-03-30 09:31:56 +00:00
|
|
|
usePreconfiguredStreaming = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
|
|
|
default = true;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2022-03-30 09:31:56 +00:00
|
|
|
Use preconfigured default streaming options.
|
2020-09-25 04:45:31 +00:00
|
|
|
|
2022-03-30 09:31:56 +00:00
|
|
|
Upstream defaults:
|
2022-08-12 12:06:08 +00:00
|
|
|
<https://github.com/l3tnun/EPGStation/blob/master/config/config.yml.template>
|
2020-09-25 04:45:31 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2022-03-30 09:31:56 +00:00
|
|
|
openFirewall = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
2020-09-25 04:45:31 +00:00
|
|
|
default = false;
|
2022-09-09 14:08:57 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-09-25 04:45:31 +00:00
|
|
|
Open ports in the firewall for the EPGStation web interface.
|
|
|
|
|
2022-09-09 14:08:57 +00:00
|
|
|
::: {.warning}
|
|
|
|
Exposing EPGStation to the open internet is generally advised
|
|
|
|
against. Only use it inside a trusted local network, or consider
|
|
|
|
putting it behind a VPN if you want remote access.
|
|
|
|
:::
|
2020-09-25 04:45:31 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2022-03-30 09:31:56 +00:00
|
|
|
database = {
|
|
|
|
name = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
2020-09-25 04:45:31 +00:00
|
|
|
default = "epgstation";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-09-25 04:45:31 +00:00
|
|
|
Name of the MySQL database that holds EPGStation's data.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2022-03-30 09:31:56 +00:00
|
|
|
passwordFile = lib.mkOption {
|
|
|
|
type = lib.types.path;
|
2020-09-25 04:45:31 +00:00
|
|
|
example = "/run/keys/epgstation-db-password";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-09-25 04:45:31 +00:00
|
|
|
A file containing the password for the database named
|
2022-08-12 12:06:08 +00:00
|
|
|
{option}`database.name`.
|
2020-09-25 04:45:31 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-03-30 09:31:56 +00:00
|
|
|
# The defaults for some options come from the upstream template
|
|
|
|
# configuration, which is the one that users would get if they follow the
|
|
|
|
# upstream instructions. This is, in some cases, different from the
|
|
|
|
# application defaults. Some options like encodeProcessNum and
|
|
|
|
# concurrentEncodeNum doesn't have an optimal default value that works for
|
|
|
|
# all hardware setups and/or performance requirements. For those kind of
|
|
|
|
# options, the application default wouldn't always result in the expected
|
|
|
|
# out-of-the-box behavior because it's the responsibility of the user to
|
|
|
|
# configure them according to their needs. In these cases, the value in the
|
|
|
|
# upstream template configuration should serve as a "good enough" default.
|
|
|
|
settings = lib.mkOption {
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2022-03-30 09:31:56 +00:00
|
|
|
Options to add to config.yml.
|
2020-09-25 04:45:31 +00:00
|
|
|
|
|
|
|
Documentation:
|
2022-08-12 12:06:08 +00:00
|
|
|
<https://github.com/l3tnun/EPGStation/blob/master/doc/conf-manual.md>
|
2020-09-25 04:45:31 +00:00
|
|
|
'';
|
|
|
|
|
2022-03-30 09:31:56 +00:00
|
|
|
default = { };
|
2020-09-25 04:45:31 +00:00
|
|
|
example = {
|
|
|
|
recPriority = 20;
|
|
|
|
conflictPriority = 10;
|
|
|
|
};
|
|
|
|
|
2022-03-30 09:31:56 +00:00
|
|
|
type = lib.types.submodule {
|
|
|
|
freeformType = yaml.type;
|
|
|
|
|
|
|
|
options.port = lib.mkOption {
|
|
|
|
type = lib.types.port;
|
|
|
|
default = 20772;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2022-03-30 09:31:56 +00:00
|
|
|
HTTP port for EPGStation to listen on.
|
|
|
|
'';
|
|
|
|
};
|
2020-09-25 04:45:31 +00:00
|
|
|
|
2022-03-30 09:31:56 +00:00
|
|
|
options.socketioPort = lib.mkOption {
|
|
|
|
type = lib.types.port;
|
|
|
|
default = cfg.settings.port + 1;
|
|
|
|
defaultText = lib.literalExpression "config.${opt.settings}.port + 1";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2022-03-30 09:31:56 +00:00
|
|
|
Socket.io port for EPGStation to listen on. It is valid to share
|
2022-08-12 12:06:08 +00:00
|
|
|
ports with {option}`${opt.settings}.port`.
|
2022-03-30 09:31:56 +00:00
|
|
|
'';
|
2020-09-25 04:45:31 +00:00
|
|
|
};
|
|
|
|
|
2022-03-30 09:31:56 +00:00
|
|
|
options.clientSocketioPort = lib.mkOption {
|
|
|
|
type = lib.types.port;
|
|
|
|
default = cfg.settings.socketioPort;
|
|
|
|
defaultText = lib.literalExpression "config.${opt.settings}.socketioPort";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2022-03-30 09:31:56 +00:00
|
|
|
Socket.io port that the web client is going to connect to. This may
|
2022-08-12 12:06:08 +00:00
|
|
|
be different from {option}`${opt.settings}.socketioPort` if
|
2022-03-30 09:31:56 +00:00
|
|
|
EPGStation is hidden behind a reverse proxy.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
options.mirakurunPath = with mirakurun; lib.mkOption {
|
|
|
|
type = lib.types.str;
|
|
|
|
default = "http+unix://${lib.replaceStrings ["/"] ["%2F"] sock}";
|
|
|
|
defaultText = lib.literalExpression ''
|
|
|
|
"http+unix://''${lib.replaceStrings ["/"] ["%2F"] config.${option}}"
|
2021-12-19 01:06:50 +00:00
|
|
|
'';
|
2020-09-25 04:45:31 +00:00
|
|
|
example = "http://localhost:40772";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "URL to connect to Mirakurun.";
|
2022-03-30 09:31:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
options.encodeProcessNum = lib.mkOption {
|
|
|
|
type = lib.types.ints.positive;
|
|
|
|
default = 4;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2022-03-30 09:31:56 +00:00
|
|
|
The maximum number of processes that EPGStation would allow to run
|
|
|
|
at the same time for encoding or streaming videos.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
options.concurrentEncodeNum = lib.mkOption {
|
|
|
|
type = lib.types.ints.positive;
|
|
|
|
default = 1;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2022-03-30 09:31:56 +00:00
|
|
|
The maximum number of encoding jobs that EPGStation would run at the
|
|
|
|
same time.
|
|
|
|
'';
|
|
|
|
};
|
2020-09-25 04:45:31 +00:00
|
|
|
|
2022-03-30 09:31:56 +00:00
|
|
|
options.encode = lib.mkOption {
|
|
|
|
type = with lib.types; listOf attrs;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc "Encoding presets for recorded videos.";
|
2020-09-25 04:45:31 +00:00
|
|
|
default = [
|
2021-10-06 13:57:05 +00:00
|
|
|
{
|
2022-03-30 09:31:56 +00:00
|
|
|
name = "H.264";
|
|
|
|
cmd = "%NODE% ${cfg.package}/libexec/enc.js";
|
2020-09-25 04:45:31 +00:00
|
|
|
suffix = ".mp4";
|
2021-10-06 13:57:05 +00:00
|
|
|
}
|
2020-09-25 04:45:31 +00:00
|
|
|
];
|
2022-03-30 09:31:56 +00:00
|
|
|
defaultText = lib.literalExpression ''
|
2021-10-06 13:57:05 +00:00
|
|
|
[
|
|
|
|
{
|
2022-03-30 09:31:56 +00:00
|
|
|
name = "H.264";
|
|
|
|
cmd = "%NODE% config.${opt.package}/libexec/enc.js";
|
2021-10-06 13:57:05 +00:00
|
|
|
suffix = ".mp4";
|
|
|
|
}
|
|
|
|
]
|
|
|
|
'';
|
2020-09-25 04:45:31 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-03-30 09:31:56 +00:00
|
|
|
config = lib.mkIf cfg.enable {
|
|
|
|
assertions = [
|
|
|
|
{
|
|
|
|
assertion = !(lib.hasAttr "readOnlyOnce" cfg.settings);
|
|
|
|
message = ''
|
|
|
|
The option config.${opt.settings}.readOnlyOnce can no longer be used
|
|
|
|
since it's been removed. No replacements are available.
|
|
|
|
'';
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2020-09-25 04:45:31 +00:00
|
|
|
environment.etc = {
|
2022-03-30 09:31:56 +00:00
|
|
|
"epgstation/epgUpdaterLogConfig.yml".source = logConfig;
|
|
|
|
"epgstation/operatorLogConfig.yml".source = logConfig;
|
|
|
|
"epgstation/serviceLogConfig.yml".source = logConfig;
|
2020-09-25 04:45:31 +00:00
|
|
|
};
|
|
|
|
|
2022-03-30 09:31:56 +00:00
|
|
|
networking.firewall = lib.mkIf cfg.openFirewall {
|
|
|
|
allowedTCPPorts = with cfg.settings; [ port socketioPort ];
|
2020-09-25 04:45:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
users.users.epgstation = {
|
|
|
|
description = "EPGStation user";
|
|
|
|
group = config.users.groups.epgstation.name;
|
|
|
|
isSystemUser = true;
|
2023-05-24 13:37:59 +00:00
|
|
|
|
|
|
|
# NPM insists on creating ~/.npm
|
|
|
|
home = "/var/cache/epgstation";
|
2020-09-25 04:45:31 +00:00
|
|
|
};
|
|
|
|
|
2022-03-30 09:31:56 +00:00
|
|
|
users.groups.epgstation = { };
|
2020-09-25 04:45:31 +00:00
|
|
|
|
2022-03-30 09:31:56 +00:00
|
|
|
services.mirakurun.enable = lib.mkDefault true;
|
2020-09-25 04:45:31 +00:00
|
|
|
|
|
|
|
services.mysql = {
|
2022-03-30 09:31:56 +00:00
|
|
|
enable = lib.mkDefault true;
|
|
|
|
package = lib.mkDefault pkgs.mariadb;
|
2020-09-25 04:45:31 +00:00
|
|
|
ensureDatabases = [ cfg.database.name ];
|
|
|
|
# FIXME: enable once mysqljs supports auth_socket
|
2023-05-24 13:37:59 +00:00
|
|
|
# https://github.com/mysqljs/mysql/issues/1507
|
|
|
|
#
|
2020-09-25 04:45:31 +00:00
|
|
|
# ensureUsers = [ {
|
|
|
|
# name = username;
|
|
|
|
# ensurePermissions = { "${cfg.database.name}.*" = "ALL PRIVILEGES"; };
|
|
|
|
# } ];
|
|
|
|
};
|
|
|
|
|
2022-03-30 09:31:56 +00:00
|
|
|
services.epgstation.settings =
|
|
|
|
let
|
|
|
|
defaultSettings = {
|
|
|
|
dbtype = lib.mkDefault "mysql";
|
|
|
|
mysql = {
|
|
|
|
socketPath = lib.mkDefault "/run/mysqld/mysqld.sock";
|
|
|
|
user = username;
|
|
|
|
password = lib.mkDefault "@dbPassword@";
|
|
|
|
database = cfg.database.name;
|
|
|
|
};
|
|
|
|
|
2023-05-24 13:37:59 +00:00
|
|
|
ffmpeg = lib.mkDefault "${cfg.ffmpeg}/bin/ffmpeg";
|
|
|
|
ffprobe = lib.mkDefault "${cfg.ffmpeg}/bin/ffprobe";
|
2022-03-30 09:31:56 +00:00
|
|
|
|
|
|
|
# for disambiguation with TypeScript files
|
|
|
|
recordedFileExtension = lib.mkDefault ".m2ts";
|
2020-09-25 04:45:31 +00:00
|
|
|
};
|
2022-03-30 09:31:56 +00:00
|
|
|
in
|
|
|
|
lib.mkMerge [
|
|
|
|
defaultSettings
|
|
|
|
(lib.mkIf cfg.usePreconfiguredStreaming streamingConfig)
|
|
|
|
];
|
2020-09-25 04:45:31 +00:00
|
|
|
|
|
|
|
systemd.tmpfiles.rules = [
|
2023-05-24 13:37:59 +00:00
|
|
|
"d '/var/lib/epgstation/key' - ${username} ${groupname} - -"
|
2020-09-25 04:45:31 +00:00
|
|
|
"d '/var/lib/epgstation/streamfiles' - ${username} ${groupname} - -"
|
2023-05-24 13:37:59 +00:00
|
|
|
"d '/var/lib/epgstation/drop' - ${username} ${groupname} - -"
|
2020-09-25 04:45:31 +00:00
|
|
|
"d '/var/lib/epgstation/recorded' - ${username} ${groupname} - -"
|
|
|
|
"d '/var/lib/epgstation/thumbnail' - ${username} ${groupname} - -"
|
2023-05-24 13:37:59 +00:00
|
|
|
"d '/var/lib/epgstation/db/subscribers' - ${username} ${groupname} - -"
|
|
|
|
"d '/var/lib/epgstation/db/migrations/mysql' - ${username} ${groupname} - -"
|
|
|
|
"d '/var/lib/epgstation/db/migrations/postgres' - ${username} ${groupname} - -"
|
|
|
|
"d '/var/lib/epgstation/db/migrations/sqlite' - ${username} ${groupname} - -"
|
2020-09-25 04:45:31 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
systemd.services.epgstation = {
|
2022-03-30 09:31:56 +00:00
|
|
|
inherit description;
|
|
|
|
|
2020-09-25 04:45:31 +00:00
|
|
|
wantedBy = [ "multi-user.target" ];
|
2022-03-30 09:31:56 +00:00
|
|
|
after = [ "network.target" ]
|
|
|
|
++ lib.optional config.services.mirakurun.enable "mirakurun.service"
|
|
|
|
++ lib.optional config.services.mysql.enable "mysql.service";
|
2020-09-25 04:45:31 +00:00
|
|
|
|
2023-05-24 13:37:59 +00:00
|
|
|
environment.NODE_ENV = "production";
|
|
|
|
|
2020-09-25 04:45:31 +00:00
|
|
|
serviceConfig = {
|
2022-03-30 09:31:56 +00:00
|
|
|
ExecStart = "${cfg.package}/bin/epgstation start";
|
2020-09-25 04:45:31 +00:00
|
|
|
ExecStartPre = "+${preStartScript}";
|
|
|
|
User = username;
|
|
|
|
Group = groupname;
|
2023-05-24 13:37:59 +00:00
|
|
|
CacheDirectory = "epgstation";
|
2020-09-25 04:45:31 +00:00
|
|
|
StateDirectory = "epgstation";
|
|
|
|
LogsDirectory = "epgstation";
|
|
|
|
ConfigurationDirectory = "epgstation";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|