@@ -1,81 +1,102 @@
1−{ config, stdenv, fetchurl, rpmextract, glibc
2−, dataDir ? "/var/lib/plex" # Plex's data directory must be baked into the package due to symlinks.
3−, enablePlexPass ? config.plex.enablePlexPass or false
1+# The actual Plex package that we run is a FHS userenv of the "raw" package.
2+{ stdenv
3+, buildFHSUserEnv
4+, writeScript
5+, plexRaw
6+
7+# Old argument for overriding the Plex data directory; isn't necessary for this
8+# version of Plex to function, but still around for backwards-compatibility.
9+, dataDir ? "/var/lib/plex"
410}:
511
6−let
7− plexPass = throw "Plex pass has been removed at upstream's request; please unset nixpkgs.config.plex.pass";
8− plexpkg = if enablePlexPass then plexPass else {
9− version = "1.15.3.876";
10− vsnHash = "ad6e39743";
11− sha256 = "01g7wccm01kg3nhf3qrmwcn20nkpv0bqz6zqv2gq5v03ps58h6g5";
12− };
12+buildFHSUserEnv rec {
13+ name = "plexmediaserver";
14+ inherit (plexRaw) meta;
1315
14−in stdenv.mkDerivation rec {
15− name = "plex-${version}";
16− version = plexpkg.version;
17− vsnHash = plexpkg.vsnHash;
18− sha256 = plexpkg.sha256;
16+ # This script is run when we start our Plex binary
17+ runScript = writeScript "plex-run-script" ''
18+ #!${stdenv.shell}
1919
20− src = fetchurl {
21− url = "https://downloads.plex.tv/plex-media-server-new/${version}-${vsnHash}/redhat/plexmediaserver-${version}-${vsnHash}.x86_64.rpm";
22− inherit sha256;
23− };
20+ set -eu
2421
25− buildInputs = [ rpmextract glibc ];
22+ # The root path to our Plex installation
23+ root=${plexRaw}/lib/plexmediaserver
2624
27− phases = [ "unpackPhase" "installPhase" "fixupPhase" "distPhase" ];
25+ # Path to where we're storing our Plex data files. We default to storing
26+ # them in the user's home directory under the XDG-compatible location, but
27+ # allow overriding with an environment variable so the location can be
28+ # configured in our NixOS module.
29+ #
30+ # NOTE: the old version of Plex used /var/lib/plex as the default location,
31+ # but this package shouldn't assume that we're going to run Plex with the
32+ # ability to write to /var/lib, so using a subdirectory of $HOME when none
33+ # is specified feels less likely to have permission errors.
34+ if [[ -z "''${PLEX_DATADIR:-}" ]]; then
35+ PLEX_DATADIR="$HOME/.local/share/plex"
36+ fi
37+ if [[ ! -d "$PLEX_DATADIR" ]]; then
38+ echo "Creating Plex data directory: $PLEX_DATADIR"
39+ mkdir -p "$PLEX_DATADIR"
40+ fi
2841
29− unpackPhase = ''
30− rpmextract $src
31− '';
42+ # The database is stored under the given directory
43+ db="$PLEX_DATADIR/.skeleton/com.plexapp.plugins.library.db"
3244
33− installPhase = ''
34− install -d $out/usr/lib
35− cp -dr --no-preserve='ownership' usr/lib/plexmediaserver $out/usr/lib/
36−
37− # Now we need to patch up the executables and libraries to work on Nix.
38− # Side note: PLEASE don't put spaces in your binary names. This is stupid.
39− for bin in "Plex Media Server" \
40− "Plex Commercial Skipper" \
41− "Plex DLNA Server" \
42− "Plex Media Scanner" \
43− "Plex Relay" \
44− "Plex Script Host" \
45− "Plex Transcoder" \
46− "Plex Tuner Service" ; do
47− patchelf --set-interpreter "${glibc.out}/lib/ld-linux-x86-64.so.2" "$out/usr/lib/plexmediaserver/$bin"
48− patchelf --set-rpath "$out/usr/lib/plexmediaserver/lib" "$out/usr/lib/plexmediaserver/$bin"
49− done
50−
51− find $out/usr/lib/plexmediaserver/Resources -type f -a -perm -0100 \
52− -print -exec patchelf --set-interpreter "${glibc.out}/lib/ld-linux-x86-64.so.2" '{}' \;
53−
54− # Our next problem is the "Resources" directory in /usr/lib/plexmediaserver.
55− # This is ostensibly a skeleton directory, which contains files that Plex
56− # copies into its folder in /var. Unfortunately, there are some SQLite
57− # databases in the directory that are opened at startup. Since these
58− # database files are read-only, SQLite chokes and Plex fails to start. To
59− # solve this, we keep the resources directory in the Nix store, but we
60− # rename the database files and replace the originals with symlinks to
61− # /var/lib/plex. Then, in the systemd unit, the base database files are
62− # copied to /var/lib/plex before starting Plex.
63− RSC=$out/usr/lib/plexmediaserver/Resources
64− for db in "com.plexapp.plugins.library.db"; do
65− mv $RSC/$db $RSC/base_$db
66− ln -s "${dataDir}/.skeleton/$db" $RSC/$db
67− done
68− '';
45+ # If we don't have a database in the expected path, then create one by
46+ # copying our base database to that location.
47+ if ! test -f "$db"; then
48+ echo "Copying base database file to: $db"
49+ mkdir -p "$(dirname "$db")"
50+ cat "${plexRaw.basedb}" > "$db"
51+ fi
52+
53+ # Set up symbolic link at '/db', which is linked to by our Plex package
54+ # (see the 'plexRaw' package).
55+ ln -s "$db" /db
56+
57+ # If we have a plugin list (set by our NixOS module), we create plugins in
58+ # the data directory as expected. This is a colon-separated list of paths.
59+ if [[ -n "''${PLEX_PLUGINS:-}" ]]; then
60+ echo "Preparing plugin directory"
61+
62+ pluginDir="$PLEX_DATADIR/Plex Media Server/Plug-ins"
63+ test -d "$pluginDir" || mkdir -p "$pluginDir"
6964
70− meta = with stdenv.lib; {
71− homepage = https://plex.tv/;
72− license = licenses.unfree;
73− platforms = platforms.linux;
74− maintainers = with stdenv.lib.maintainers; [ colemickens forkk thoughtpolice pjones lnl7 ];
75− description = "Media / DLNA server";
76− longDescription = ''
77− Plex is a media server which allows you to store your media and play it
78− back across many different devices.
79− '';
80− };
65+ # First, remove all of the symlinks in the plugins directory.
66+ echo "Removing old symlinks"
67+ for f in $(ls "$pluginDir/"); do
68+ if [[ -L "$pluginDir/$f" ]]; then
69+ echo "Removing plugin symlink: $pluginDir/$f"
70+ rm "$pluginDir/$f"
71+ fi
72+ done
73+
74+ echo "Symlinking plugins"
75+ IFS=':' read -ra pluginsArray <<< "$PLEX_PLUGINS"
76+ for path in "''${pluginsArray[@]}"; do
77+ dest="$pluginDir/$(basename "$path")"
78+
79+ if [[ ! -d "$path" ]]; then
80+ echo "Error symlinking plugin from $path: no such directory"
81+ elif [[ -d "$dest" || -L "$dest" ]]; then
82+ echo "Error symlinking plugin from $path to $dest: file or directory already exists"
83+ else
84+ echo "Symlinking plugin at: $path"
85+ ln -s "$path" "$dest"
86+ fi
87+ done
88+ fi
89+
90+ # Tell Plex to use the data directory as the "Application Support"
91+ # directory, otherwise it tries to write things into the user's home
92+ # directory.
93+ export PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR="$PLEX_DATADIR"
94+
95+ # Tell Plex where the 'home' directory for itself is.
96+ export PLEX_MEDIA_SERVER_HOME="${plexRaw}/lib/plexmediaserver"
97+
98+ # Actually run Plex, prepending LD_LIBRARY_PATH with the libraries from
99+ # the Plex package.
100+ LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$root exec "$root/Plex Media Server"
101+ '';
81102}