Merge pull request #56565 from andrew-d/adunham/plex-fhs
plex: rewrite to use FHS userenv
This commit is contained in:
commit
857069293d
@ -10,35 +10,38 @@ in
|
|||||||
services.plex = {
|
services.plex = {
|
||||||
enable = mkEnableOption "Plex Media Server";
|
enable = mkEnableOption "Plex Media Server";
|
||||||
|
|
||||||
# FIXME: In order for this config option to work, symlinks in the Plex
|
|
||||||
# package in the Nix store have to be changed to point to this directory.
|
|
||||||
dataDir = mkOption {
|
dataDir = mkOption {
|
||||||
type = types.str;
|
type = types.str;
|
||||||
default = "/var/lib/plex";
|
default = "/var/lib/plex";
|
||||||
description = "The directory where Plex stores its data files.";
|
description = ''
|
||||||
|
The directory where Plex stores its data files.
|
||||||
|
'';
|
||||||
};
|
};
|
||||||
|
|
||||||
openFirewall = mkOption {
|
openFirewall = mkOption {
|
||||||
type = types.bool;
|
type = types.bool;
|
||||||
default = false;
|
default = false;
|
||||||
description = ''
|
description = ''
|
||||||
Open ports in the firewall for the media server
|
Open ports in the firewall for the media server.
|
||||||
'';
|
'';
|
||||||
};
|
};
|
||||||
|
|
||||||
user = mkOption {
|
user = mkOption {
|
||||||
type = types.str;
|
type = types.str;
|
||||||
default = "plex";
|
default = "plex";
|
||||||
description = "User account under which Plex runs.";
|
description = ''
|
||||||
|
User account under which Plex runs.
|
||||||
|
'';
|
||||||
};
|
};
|
||||||
|
|
||||||
group = mkOption {
|
group = mkOption {
|
||||||
type = types.str;
|
type = types.str;
|
||||||
default = "plex";
|
default = "plex";
|
||||||
description = "Group under which Plex runs.";
|
description = ''
|
||||||
|
Group under which Plex runs.
|
||||||
|
'';
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
managePlugins = mkOption {
|
managePlugins = mkOption {
|
||||||
type = types.bool;
|
type = types.bool;
|
||||||
default = true;
|
default = true;
|
||||||
@ -80,73 +83,48 @@ in
|
|||||||
description = "Plex Media Server";
|
description = "Plex Media Server";
|
||||||
after = [ "network.target" ];
|
after = [ "network.target" ];
|
||||||
wantedBy = [ "multi-user.target" ];
|
wantedBy = [ "multi-user.target" ];
|
||||||
preStart = ''
|
|
||||||
test -d "${cfg.dataDir}/Plex Media Server" || {
|
|
||||||
echo "Creating initial Plex data directory in \"${cfg.dataDir}\"."
|
|
||||||
mkdir -p "${cfg.dataDir}/Plex Media Server"
|
|
||||||
chown -R ${cfg.user}:${cfg.group} "${cfg.dataDir}"
|
|
||||||
}
|
|
||||||
|
|
||||||
# Copy the database skeleton files to /var/lib/plex/.skeleton
|
|
||||||
# See the the Nix expression for Plex's package for more information on
|
|
||||||
# why this is done.
|
|
||||||
install --owner ${cfg.user} --group ${cfg.group} -d "${cfg.dataDir}/.skeleton"
|
|
||||||
for db in "com.plexapp.plugins.library.db"; do
|
|
||||||
if [ ! -e "${cfg.dataDir}/.skeleton/$db" ]; then
|
|
||||||
cp "${cfg.package}/usr/lib/plexmediaserver/Resources/base_$db" "${cfg.dataDir}/.skeleton/$db"
|
|
||||||
fi
|
|
||||||
chmod u+w "${cfg.dataDir}/.skeleton/$db"
|
|
||||||
chown ${cfg.user}:${cfg.group} "${cfg.dataDir}/.skeleton/$db"
|
|
||||||
done
|
|
||||||
|
|
||||||
# If managePlugins is enabled, setup symlinks for plugins.
|
|
||||||
${optionalString cfg.managePlugins ''
|
|
||||||
echo "Preparing plugin directory."
|
|
||||||
PLUGINDIR="${cfg.dataDir}/Plex Media Server/Plug-ins"
|
|
||||||
test -d "$PLUGINDIR" || {
|
|
||||||
mkdir -p "$PLUGINDIR";
|
|
||||||
chown ${cfg.user}:${cfg.group} "$PLUGINDIR";
|
|
||||||
}
|
|
||||||
|
|
||||||
echo "Removing old symlinks."
|
|
||||||
# First, remove all of the symlinks in the directory.
|
|
||||||
for f in `ls "$PLUGINDIR/"`; do
|
|
||||||
if [[ -L "$PLUGINDIR/$f" ]]; then
|
|
||||||
echo "Removing plugin symlink $PLUGINDIR/$f."
|
|
||||||
rm "$PLUGINDIR/$f"
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
|
|
||||||
echo "Symlinking plugins."
|
|
||||||
for path in ${toString cfg.extraPlugins}; do
|
|
||||||
dest="$PLUGINDIR/$(basename $path)"
|
|
||||||
if [[ ! -d "$path" ]]; then
|
|
||||||
echo "Error symlinking plugin from $path: no such directory."
|
|
||||||
elif [[ -d "$dest" || -L "$dest" ]]; then
|
|
||||||
echo "Error symlinking plugin from $path to $dest: file or directory already exists."
|
|
||||||
else
|
|
||||||
echo "Symlinking plugin at $path..."
|
|
||||||
ln -s "$path" "$dest"
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
''}
|
|
||||||
'';
|
|
||||||
serviceConfig = {
|
serviceConfig = {
|
||||||
Type = "simple";
|
Type = "simple";
|
||||||
User = cfg.user;
|
User = cfg.user;
|
||||||
Group = cfg.group;
|
Group = cfg.group;
|
||||||
PermissionsStartOnly = "true";
|
|
||||||
ExecStart = "\"${cfg.package}/usr/lib/plexmediaserver/Plex Media Server\"";
|
# 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
|
||||||
|
|
||||||
|
# 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";
|
||||||
KillSignal = "SIGQUIT";
|
KillSignal = "SIGQUIT";
|
||||||
Restart = "on-failure";
|
Restart = "on-failure";
|
||||||
};
|
};
|
||||||
|
|
||||||
environment = {
|
environment = {
|
||||||
PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR=cfg.dataDir;
|
# Configuration for our FHS userenv script
|
||||||
PLEX_MEDIA_SERVER_HOME="${cfg.package}/usr/lib/plexmediaserver";
|
PLEX_DATADIR=cfg.dataDir;
|
||||||
|
PLEX_PLUGINS=concatMapStringsSep ":" builtins.toString cfg.extraPlugins;
|
||||||
|
|
||||||
|
# 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";
|
||||||
|
|
||||||
PLEX_MEDIA_SERVER_MAX_PLUGIN_PROCS="6";
|
PLEX_MEDIA_SERVER_MAX_PLUGIN_PROCS="6";
|
||||||
PLEX_MEDIA_SERVER_TMPDIR="/tmp";
|
PLEX_MEDIA_SERVER_TMPDIR="/tmp";
|
||||||
PLEX_MEDIA_SERVER_USE_SYSLOG="true";
|
PLEX_MEDIA_SERVER_USE_SYSLOG="true";
|
||||||
LD_LIBRARY_PATH="/run/opengl-driver/lib:${cfg.package}/usr/lib/plexmediaserver/lib";
|
|
||||||
LC_ALL="en_US.UTF-8";
|
LC_ALL="en_US.UTF-8";
|
||||||
LANG="en_US.UTF-8";
|
LANG="en_US.UTF-8";
|
||||||
};
|
};
|
||||||
|
@ -1,81 +1,102 @@
|
|||||||
{ config, stdenv, fetchurl, rpmextract, glibc
|
# The actual Plex package that we run is a FHS userenv of the "raw" package.
|
||||||
, dataDir ? "/var/lib/plex" # Plex's data directory must be baked into the package due to symlinks.
|
{ stdenv
|
||||||
, enablePlexPass ? config.plex.enablePlexPass or false
|
, buildFHSUserEnv
|
||||||
|
, writeScript
|
||||||
|
, plexRaw
|
||||||
|
|
||||||
|
# Old argument for overriding the Plex data directory; isn't necessary for this
|
||||||
|
# version of Plex to function, but still around for backwards-compatibility.
|
||||||
|
, dataDir ? "/var/lib/plex"
|
||||||
}:
|
}:
|
||||||
|
|
||||||
let
|
buildFHSUserEnv rec {
|
||||||
plexPass = throw "Plex pass has been removed at upstream's request; please unset nixpkgs.config.plex.pass";
|
name = "plexmediaserver";
|
||||||
plexpkg = if enablePlexPass then plexPass else {
|
inherit (plexRaw) meta;
|
||||||
version = "1.15.3.876";
|
|
||||||
vsnHash = "ad6e39743";
|
|
||||||
sha256 = "01g7wccm01kg3nhf3qrmwcn20nkpv0bqz6zqv2gq5v03ps58h6g5";
|
|
||||||
};
|
|
||||||
|
|
||||||
in stdenv.mkDerivation rec {
|
# This script is run when we start our Plex binary
|
||||||
name = "plex-${version}";
|
runScript = writeScript "plex-run-script" ''
|
||||||
version = plexpkg.version;
|
#!${stdenv.shell}
|
||||||
vsnHash = plexpkg.vsnHash;
|
|
||||||
sha256 = plexpkg.sha256;
|
|
||||||
|
|
||||||
src = fetchurl {
|
set -eu
|
||||||
url = "https://downloads.plex.tv/plex-media-server-new/${version}-${vsnHash}/redhat/plexmediaserver-${version}-${vsnHash}.x86_64.rpm";
|
|
||||||
inherit sha256;
|
|
||||||
};
|
|
||||||
|
|
||||||
buildInputs = [ rpmextract glibc ];
|
# The root path to our Plex installation
|
||||||
|
root=${plexRaw}/lib/plexmediaserver
|
||||||
|
|
||||||
phases = [ "unpackPhase" "installPhase" "fixupPhase" "distPhase" ];
|
# Path to where we're storing our Plex data files. We default to storing
|
||||||
|
# them in the user's home directory under the XDG-compatible location, but
|
||||||
|
# allow overriding with an environment variable so the location can be
|
||||||
|
# configured in our NixOS module.
|
||||||
|
#
|
||||||
|
# NOTE: the old version of Plex used /var/lib/plex as the default location,
|
||||||
|
# but this package shouldn't assume that we're going to run Plex with the
|
||||||
|
# ability to write to /var/lib, so using a subdirectory of $HOME when none
|
||||||
|
# is specified feels less likely to have permission errors.
|
||||||
|
if [[ -z "''${PLEX_DATADIR:-}" ]]; then
|
||||||
|
PLEX_DATADIR="$HOME/.local/share/plex"
|
||||||
|
fi
|
||||||
|
if [[ ! -d "$PLEX_DATADIR" ]]; then
|
||||||
|
echo "Creating Plex data directory: $PLEX_DATADIR"
|
||||||
|
mkdir -p "$PLEX_DATADIR"
|
||||||
|
fi
|
||||||
|
|
||||||
unpackPhase = ''
|
# The database is stored under the given directory
|
||||||
rpmextract $src
|
db="$PLEX_DATADIR/.skeleton/com.plexapp.plugins.library.db"
|
||||||
'';
|
|
||||||
|
|
||||||
installPhase = ''
|
# If we don't have a database in the expected path, then create one by
|
||||||
install -d $out/usr/lib
|
# copying our base database to that location.
|
||||||
cp -dr --no-preserve='ownership' usr/lib/plexmediaserver $out/usr/lib/
|
if ! test -f "$db"; then
|
||||||
|
echo "Copying base database file to: $db"
|
||||||
|
mkdir -p "$(dirname "$db")"
|
||||||
|
cat "${plexRaw.basedb}" > "$db"
|
||||||
|
fi
|
||||||
|
|
||||||
# Now we need to patch up the executables and libraries to work on Nix.
|
# Set up symbolic link at '/db', which is linked to by our Plex package
|
||||||
# Side note: PLEASE don't put spaces in your binary names. This is stupid.
|
# (see the 'plexRaw' package).
|
||||||
for bin in "Plex Media Server" \
|
ln -s "$db" /db
|
||||||
"Plex Commercial Skipper" \
|
|
||||||
"Plex DLNA Server" \
|
# If we have a plugin list (set by our NixOS module), we create plugins in
|
||||||
"Plex Media Scanner" \
|
# the data directory as expected. This is a colon-separated list of paths.
|
||||||
"Plex Relay" \
|
if [[ -n "''${PLEX_PLUGINS:-}" ]]; then
|
||||||
"Plex Script Host" \
|
echo "Preparing plugin directory"
|
||||||
"Plex Transcoder" \
|
|
||||||
"Plex Tuner Service" ; do
|
pluginDir="$PLEX_DATADIR/Plex Media Server/Plug-ins"
|
||||||
patchelf --set-interpreter "${glibc.out}/lib/ld-linux-x86-64.so.2" "$out/usr/lib/plexmediaserver/$bin"
|
test -d "$pluginDir" || mkdir -p "$pluginDir"
|
||||||
patchelf --set-rpath "$out/usr/lib/plexmediaserver/lib" "$out/usr/lib/plexmediaserver/$bin"
|
|
||||||
|
# First, remove all of the symlinks in the plugins directory.
|
||||||
|
echo "Removing old symlinks"
|
||||||
|
for f in $(ls "$pluginDir/"); do
|
||||||
|
if [[ -L "$pluginDir/$f" ]]; then
|
||||||
|
echo "Removing plugin symlink: $pluginDir/$f"
|
||||||
|
rm "$pluginDir/$f"
|
||||||
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
find $out/usr/lib/plexmediaserver/Resources -type f -a -perm -0100 \
|
echo "Symlinking plugins"
|
||||||
-print -exec patchelf --set-interpreter "${glibc.out}/lib/ld-linux-x86-64.so.2" '{}' \;
|
IFS=':' read -ra pluginsArray <<< "$PLEX_PLUGINS"
|
||||||
|
for path in "''${pluginsArray[@]}"; do
|
||||||
|
dest="$pluginDir/$(basename "$path")"
|
||||||
|
|
||||||
# Our next problem is the "Resources" directory in /usr/lib/plexmediaserver.
|
if [[ ! -d "$path" ]]; then
|
||||||
# This is ostensibly a skeleton directory, which contains files that Plex
|
echo "Error symlinking plugin from $path: no such directory"
|
||||||
# copies into its folder in /var. Unfortunately, there are some SQLite
|
elif [[ -d "$dest" || -L "$dest" ]]; then
|
||||||
# databases in the directory that are opened at startup. Since these
|
echo "Error symlinking plugin from $path to $dest: file or directory already exists"
|
||||||
# database files are read-only, SQLite chokes and Plex fails to start. To
|
else
|
||||||
# solve this, we keep the resources directory in the Nix store, but we
|
echo "Symlinking plugin at: $path"
|
||||||
# rename the database files and replace the originals with symlinks to
|
ln -s "$path" "$dest"
|
||||||
# /var/lib/plex. Then, in the systemd unit, the base database files are
|
fi
|
||||||
# copied to /var/lib/plex before starting Plex.
|
|
||||||
RSC=$out/usr/lib/plexmediaserver/Resources
|
|
||||||
for db in "com.plexapp.plugins.library.db"; do
|
|
||||||
mv $RSC/$db $RSC/base_$db
|
|
||||||
ln -s "${dataDir}/.skeleton/$db" $RSC/$db
|
|
||||||
done
|
done
|
||||||
'';
|
fi
|
||||||
|
|
||||||
meta = with stdenv.lib; {
|
# Tell Plex to use the data directory as the "Application Support"
|
||||||
homepage = https://plex.tv/;
|
# directory, otherwise it tries to write things into the user's home
|
||||||
license = licenses.unfree;
|
# directory.
|
||||||
platforms = platforms.linux;
|
export PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR="$PLEX_DATADIR"
|
||||||
maintainers = with stdenv.lib.maintainers; [ colemickens forkk thoughtpolice pjones lnl7 ];
|
|
||||||
description = "Media / DLNA server";
|
# Tell Plex where the 'home' directory for itself is.
|
||||||
longDescription = ''
|
export PLEX_MEDIA_SERVER_HOME="${plexRaw}/lib/plexmediaserver"
|
||||||
Plex is a media server which allows you to store your media and play it
|
|
||||||
back across many different devices.
|
# Actually run Plex, prepending LD_LIBRARY_PATH with the libraries from
|
||||||
|
# the Plex package.
|
||||||
|
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$root exec "$root/Plex Media Server"
|
||||||
'';
|
'';
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
70
pkgs/servers/plex/raw.nix
Normal file
70
pkgs/servers/plex/raw.nix
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
{ stdenv
|
||||||
|
, fetchurl
|
||||||
|
, rpmextract
|
||||||
|
}:
|
||||||
|
|
||||||
|
# The raw package that fetches and extracts the Plex RPM. Override the source
|
||||||
|
# and version of this derivation if you want to use a Plex Pass version of the
|
||||||
|
# server, and the FHS userenv and corresponding NixOS module should
|
||||||
|
# automatically pick up the changes.
|
||||||
|
stdenv.mkDerivation rec {
|
||||||
|
version = "1.15.3.876-ad6e39743";
|
||||||
|
pname = "plexmediaserver";
|
||||||
|
name = "${pname}-${version}";
|
||||||
|
|
||||||
|
# Fetch the source
|
||||||
|
src = fetchurl {
|
||||||
|
url = "https://downloads.plex.tv/plex-media-server-new/${version}/redhat/plexmediaserver-${version}.x86_64.rpm";
|
||||||
|
sha256 = "01g7wccm01kg3nhf3qrmwcn20nkpv0bqz6zqv2gq5v03ps58h6g5";
|
||||||
|
};
|
||||||
|
|
||||||
|
outputs = [ "out" "basedb" ];
|
||||||
|
|
||||||
|
nativeBuildInputs = [ rpmextract ];
|
||||||
|
|
||||||
|
phases = [ "unpackPhase" "installPhase" "fixupPhase" "distPhase" ];
|
||||||
|
|
||||||
|
unpackPhase = ''
|
||||||
|
rpmextract $src
|
||||||
|
'';
|
||||||
|
|
||||||
|
installPhase = ''
|
||||||
|
mkdir -p "$out/lib"
|
||||||
|
cp -dr --no-preserve='ownership' usr/lib/plexmediaserver $out/lib/
|
||||||
|
|
||||||
|
# Location of the initial Plex plugins database
|
||||||
|
f=$out/lib/plexmediaserver/Resources/com.plexapp.plugins.library.db
|
||||||
|
|
||||||
|
# Store the base database in the 'basedb' output
|
||||||
|
cat $f > $basedb
|
||||||
|
|
||||||
|
# Overwrite the base database in the Plex package with an absolute symlink
|
||||||
|
# to the '/db' file; we create this path in the FHS userenv (see the "plex"
|
||||||
|
# package).
|
||||||
|
ln -fs /db $f
|
||||||
|
'';
|
||||||
|
|
||||||
|
# We're running in a FHS userenv; don't patch anything
|
||||||
|
dontPatchShebangs = true;
|
||||||
|
dontStrip = true;
|
||||||
|
dontPatchELF = true;
|
||||||
|
dontAutoPatchelf = true;
|
||||||
|
|
||||||
|
meta = with stdenv.lib; {
|
||||||
|
homepage = https://plex.tv/;
|
||||||
|
license = licenses.unfree;
|
||||||
|
platforms = platforms.linux;
|
||||||
|
maintainers = with maintainers; [
|
||||||
|
colemickens
|
||||||
|
forkk
|
||||||
|
lnl7
|
||||||
|
pjones
|
||||||
|
thoughtpolice
|
||||||
|
];
|
||||||
|
description = "Media library streaming server";
|
||||||
|
longDescription = ''
|
||||||
|
Plex is a media server which allows you to store your media and play it
|
||||||
|
back across many different devices.
|
||||||
|
'';
|
||||||
|
};
|
||||||
|
}
|
@ -5105,6 +5105,7 @@ in
|
|||||||
playbar2 = libsForQt5.callPackage ../applications/audio/playbar2 { };
|
playbar2 = libsForQt5.callPackage ../applications/audio/playbar2 { };
|
||||||
|
|
||||||
plex = callPackage ../servers/plex { };
|
plex = callPackage ../servers/plex { };
|
||||||
|
plexRaw = callPackage ../servers/plex/raw.nix { };
|
||||||
|
|
||||||
tautulli = callPackage ../servers/tautulli { python = python2; };
|
tautulli = callPackage ../servers/tautulli { python = python2; };
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user