2015-04-16 13:15:59 -05:00
|
|
|
{
|
|
|
|
config,
|
|
|
|
pkgs,
|
|
|
|
lib,
|
|
|
|
...
|
|
|
|
}:
|
|
|
|
let
|
|
|
|
cfg = config.services.plex;
|
|
|
|
in
|
|
|
|
{
|
2022-02-07 10:57:40 +01:00
|
|
|
imports = [
|
2024-08-30 00:46:41 +02:00
|
|
|
(lib.mkRemovedOptionModule [
|
|
|
|
"services"
|
|
|
|
"plex"
|
|
|
|
"managePlugins"
|
|
|
|
] "Please omit or define the option: `services.plex.extraPlugins' instead.")
|
2022-02-07 10:57:40 +01:00
|
|
|
];
|
|
|
|
|
2015-04-16 13:15:59 -05:00
|
|
|
options = {
|
|
|
|
services.plex = {
|
2024-08-30 00:46:41 +02:00
|
|
|
enable = lib.mkEnableOption "Plex Media Server";
|
2015-04-16 13:15:59 -05:00
|
|
|
|
2024-08-30 00:46:41 +02:00
|
|
|
dataDir = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
2015-04-16 13:15:59 -05:00
|
|
|
default = "/var/lib/plex";
|
2019-02-28 05:15:28 -05:00
|
|
|
description = ''
|
|
|
|
The directory where Plex stores its data files.
|
|
|
|
'';
|
2015-04-16 13:15:59 -05:00
|
|
|
};
|
|
|
|
|
2024-08-30 00:46:41 +02:00
|
|
|
openFirewall = lib.mkOption {
|
|
|
|
type = lib.types.bool;
|
2016-12-18 21:59:06 -05:00
|
|
|
default = false;
|
|
|
|
description = ''
|
2019-02-28 05:15:28 -05:00
|
|
|
Open ports in the firewall for the media server.
|
2016-12-18 21:59:06 -05:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-08-30 00:46:41 +02:00
|
|
|
user = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
2015-04-16 13:15:59 -05:00
|
|
|
default = "plex";
|
2019-02-28 05:15:28 -05:00
|
|
|
description = ''
|
|
|
|
User account under which Plex runs.
|
|
|
|
'';
|
2015-04-16 13:15:59 -05:00
|
|
|
};
|
|
|
|
|
2024-08-30 00:46:41 +02:00
|
|
|
group = lib.mkOption {
|
|
|
|
type = lib.types.str;
|
2015-04-16 13:15:59 -05:00
|
|
|
default = "plex";
|
2019-02-28 05:15:28 -05:00
|
|
|
description = ''
|
|
|
|
Group under which Plex runs.
|
|
|
|
'';
|
2015-04-16 13:15:59 -05:00
|
|
|
};
|
2015-04-17 14:45:10 -05:00
|
|
|
|
2024-08-30 00:46:41 +02:00
|
|
|
extraPlugins = lib.mkOption {
|
|
|
|
type = lib.types.listOf lib.types.path;
|
2015-04-17 14:45:10 -05:00
|
|
|
default = [ ];
|
|
|
|
description = ''
|
|
|
|
A list of paths to extra plugin bundles to install in Plex's plugin
|
|
|
|
directory. Every time the systemd unit for Plex starts up, all of the
|
|
|
|
symlinks in Plex's plugin directory will be cleared and this module
|
2022-02-07 10:57:40 +01:00
|
|
|
will symlink all of the paths specified here to that directory.
|
2015-04-17 14:45:10 -05:00
|
|
|
'';
|
2024-08-30 00:46:41 +02:00
|
|
|
example = lib.literalExpression ''
|
2022-02-06 18:35:58 +01:00
|
|
|
[
|
|
|
|
(builtins.path {
|
|
|
|
name = "Audnexus.bundle";
|
|
|
|
path = pkgs.fetchFromGitHub {
|
|
|
|
owner = "djdembeck";
|
|
|
|
repo = "Audnexus.bundle";
|
|
|
|
rev = "v0.2.8";
|
|
|
|
sha256 = "sha256-IWOSz3vYL7zhdHan468xNc6C/eQ2C2BukQlaJNLXh7E=";
|
|
|
|
};
|
|
|
|
})
|
|
|
|
]
|
|
|
|
'';
|
2015-04-17 14:45:10 -05:00
|
|
|
};
|
2015-12-30 20:19:04 -06:00
|
|
|
|
2024-08-30 00:46:41 +02:00
|
|
|
extraScanners = lib.mkOption {
|
|
|
|
type = lib.types.listOf lib.types.path;
|
2021-09-17 21:12:20 -04:00
|
|
|
default = [ ];
|
|
|
|
description = ''
|
|
|
|
A list of paths to extra scanners to install in Plex's scanners
|
|
|
|
directory.
|
|
|
|
|
|
|
|
Every time the systemd unit for Plex starts up, all of the symlinks
|
|
|
|
in Plex's scanners directory will be cleared and this module will
|
|
|
|
symlink all of the paths specified here to that directory.
|
|
|
|
'';
|
2024-08-30 00:46:41 +02:00
|
|
|
example = lib.literalExpression ''
|
2021-09-17 21:12:20 -04:00
|
|
|
[
|
|
|
|
(fetchFromGitHub {
|
|
|
|
owner = "ZeroQI";
|
|
|
|
repo = "Absolute-Series-Scanner";
|
|
|
|
rev = "773a39f502a1204b0b0255903cee4ed02c46fde0";
|
|
|
|
sha256 = "4l+vpiDdC8L/EeJowUgYyB3JPNTZ1sauN8liFAcK+PY=";
|
|
|
|
})
|
|
|
|
]
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-08-30 00:46:41 +02:00
|
|
|
accelerationDevices = lib.mkOption {
|
|
|
|
type = lib.types.listOf lib.types.str;
|
2024-03-03 19:21:08 -05:00
|
|
|
default = [ "*" ];
|
|
|
|
example = [ "/dev/dri/renderD128" ];
|
|
|
|
description = ''
|
|
|
|
A list of device paths to hardware acceleration devices that Plex should
|
|
|
|
have access to. This is useful when transcoding media files.
|
|
|
|
The special value `"*"` will allow all devices.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-08-30 00:46:41 +02:00
|
|
|
package = lib.mkPackageOption pkgs "plex" {
|
2023-11-27 01:19:27 +01:00
|
|
|
extraDescription = ''
|
|
|
|
Plex subscribers may wish to use their own package here,
|
|
|
|
pointing to subscriber-only server versions.
|
2015-12-30 20:19:04 -06:00
|
|
|
'';
|
|
|
|
};
|
2015-04-16 13:15:59 -05:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-08-30 00:46:41 +02:00
|
|
|
config = lib.mkIf cfg.enable {
|
2015-04-16 13:15:59 -05:00
|
|
|
# Most of this is just copied from the RPM package's systemd service file.
|
|
|
|
systemd.services.plex = {
|
|
|
|
description = "Plex Media Server";
|
|
|
|
after = [ "network.target" ];
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
2019-02-28 05:15:28 -05:00
|
|
|
|
2015-04-16 13:15:59 -05:00
|
|
|
serviceConfig = {
|
|
|
|
Type = "simple";
|
|
|
|
User = cfg.user;
|
|
|
|
Group = cfg.group;
|
2019-02-28 05:15:28 -05:00
|
|
|
|
|
|
|
# Run the pre-start script with full permissions (the "!" prefix) so it
|
|
|
|
# can create the data directory if necessary.
|
|
|
|
ExecStartPre =
|
|
|
|
let
|
|
|
|
preStartScript = pkgs.writeScript "plex-run-prestart" ''
|
|
|
|
#!${pkgs.bash}/bin/bash
|
2024-12-10 20:26:33 +01:00
|
|
|
|
2019-02-28 05:15:28 -05:00
|
|
|
# Create data directory if it doesn't exist
|
|
|
|
if ! test -d "$PLEX_DATADIR"; then
|
|
|
|
echo "Creating initial Plex data directory in: $PLEX_DATADIR"
|
|
|
|
install -d -m 0755 -o "${cfg.user}" -g "${cfg.group}" "$PLEX_DATADIR"
|
|
|
|
fi
|
|
|
|
'';
|
|
|
|
in
|
|
|
|
"!${preStartScript}";
|
|
|
|
|
|
|
|
ExecStart = "${cfg.package}/bin/plexmediaserver";
|
2017-04-14 11:19:29 -07:00
|
|
|
KillSignal = "SIGQUIT";
|
2022-08-27 17:21:19 +01:00
|
|
|
PIDFile = "${cfg.dataDir}/Plex Media Server/plexmediaserver.pid";
|
2016-04-08 10:55:00 -07:00
|
|
|
Restart = "on-failure";
|
2024-03-03 19:21:08 -05:00
|
|
|
|
|
|
|
# Hardening
|
|
|
|
NoNewPrivileges = true;
|
|
|
|
PrivateTmp = true;
|
|
|
|
PrivateDevices = cfg.accelerationDevices == [ ];
|
2024-08-30 00:46:41 +02:00
|
|
|
DeviceAllow = lib.mkIf (
|
|
|
|
cfg.accelerationDevices != [ ] && !lib.elem "*" cfg.accelerationDevices
|
|
|
|
) cfg.accelerationDevices;
|
2024-03-03 19:21:08 -05:00
|
|
|
ProtectSystem = true;
|
|
|
|
ProtectHome = true;
|
|
|
|
ProtectControlGroups = true;
|
|
|
|
ProtectKernelModules = true;
|
|
|
|
ProtectKernelTunables = true;
|
|
|
|
RestrictAddressFamilies = [
|
|
|
|
"AF_UNIX"
|
|
|
|
"AF_INET"
|
|
|
|
"AF_INET6"
|
|
|
|
"AF_NETLINK"
|
|
|
|
];
|
|
|
|
# This could be made to work if the namespaces needed were known
|
|
|
|
# RestrictNamespaces = true;
|
|
|
|
RestrictRealtime = true;
|
|
|
|
RestrictSUIDSGID = true;
|
|
|
|
MemoryDenyWriteExecute = true;
|
|
|
|
LockPersonality = true;
|
2015-04-16 13:15:59 -05:00
|
|
|
};
|
2019-02-28 05:15:28 -05:00
|
|
|
|
2015-04-16 13:15:59 -05:00
|
|
|
environment = {
|
2019-02-28 05:15:28 -05:00
|
|
|
# Configuration for our FHS userenv script
|
|
|
|
PLEX_DATADIR = cfg.dataDir;
|
2024-08-30 00:46:41 +02:00
|
|
|
PLEX_PLUGINS = lib.concatMapStringsSep ":" builtins.toString cfg.extraPlugins;
|
|
|
|
PLEX_SCANNERS = lib.concatMapStringsSep ":" builtins.toString cfg.extraScanners;
|
2019-02-28 05:15:28 -05:00
|
|
|
|
|
|
|
# The following variables should be set by the FHS userenv script:
|
|
|
|
# PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR
|
|
|
|
# PLEX_MEDIA_SERVER_HOME
|
|
|
|
|
|
|
|
# Allow access to GPU acceleration; the Plex LD_LIBRARY_PATH is added
|
|
|
|
# by the FHS userenv script.
|
|
|
|
LD_LIBRARY_PATH = "/run/opengl-driver/lib";
|
|
|
|
|
2015-04-16 13:15:59 -05:00
|
|
|
PLEX_MEDIA_SERVER_MAX_PLUGIN_PROCS = "6";
|
|
|
|
PLEX_MEDIA_SERVER_TMPDIR = "/tmp";
|
2019-02-11 10:47:21 +08:00
|
|
|
PLEX_MEDIA_SERVER_USE_SYSLOG = "true";
|
2015-04-16 13:15:59 -05:00
|
|
|
LC_ALL = "en_US.UTF-8";
|
|
|
|
LANG = "en_US.UTF-8";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-08-30 00:46:41 +02:00
|
|
|
networking.firewall = lib.mkIf cfg.openFirewall {
|
2016-12-18 21:59:06 -05:00
|
|
|
allowedTCPPorts = [
|
|
|
|
32400
|
|
|
|
3005
|
|
|
|
8324
|
|
|
|
32469
|
|
|
|
];
|
|
|
|
allowedUDPPorts = [
|
|
|
|
1900
|
|
|
|
5353
|
|
|
|
32410
|
|
|
|
32412
|
|
|
|
32413
|
|
|
|
32414
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
2024-08-30 00:46:41 +02:00
|
|
|
users.users = lib.mkIf (cfg.user == "plex") {
|
2015-04-16 13:15:59 -05:00
|
|
|
plex = {
|
|
|
|
group = cfg.group;
|
|
|
|
uid = config.ids.uids.plex;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2024-08-30 00:46:41 +02:00
|
|
|
users.groups = lib.mkIf (cfg.group == "plex") {
|
2015-04-16 13:15:59 -05:00
|
|
|
plex = {
|
|
|
|
gid = config.ids.gids.plex;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|