1{ stdenv, fetchurl, rpmextract, glibc
2, dataDir ? "/var/lib/plex" # Plex's data directory must be baked into the package due to symlinks.
3}:
4
5stdenv.mkDerivation rec {
6 name = "plex-${version}";
7 version = "0.9.11.16.958";
8 vsnHash = "80f1748";
9
10 src = fetchurl {
11 url = "https://downloads.plex.tv/plex-media-server/${version}-${vsnHash}/plexmediaserver-${version}-${vsnHash}.x86_64.rpm";
12 sha256 = "1wrl654nk10i9p01cgy9fqiqalxyl718qhp4kjnxvcwafayxkp26";
13 };
14
15 buildInputs = [ rpmextract glibc ];
16
17 phases = [ "unpackPhase" "installPhase" "fixupPhase" "distPhase" ];
18
19 unpackPhase = ''
20 rpmextract $src
21 '';
22
23 installPhase = ''
24 install -d $out/usr/lib
25 cp -dr --no-preserve='ownership' usr/lib/plexmediaserver $out/usr/lib/
26
27 # Now we need to patch up the executables and libraries to work on Nix.
28 # Side note: PLEASE don't put spaces in your binary names. This is stupid.
29 for bin in "Plex Media Server" "Plex DLNA Server" "Plex Media Scanner"; do
30 patchelf --set-interpreter "${glibc}/lib/ld-linux-x86-64.so.2" "$out/usr/lib/plexmediaserver/$bin"
31 patchelf --set-rpath "$out/usr/lib/plexmediaserver" "$out/usr/lib/plexmediaserver/$bin"
32 done
33
34 find $out/usr/lib/plexmediaserver/Resources -type f -a -perm /0100 \
35 -print -exec patchelf --set-interpreter "${glibc}/lib/ld-linux-x86-64.so.2" '{}' \;
36
37
38 # Our next problem is the "Resources" directory in /usr/lib/plexmediaserver.
39 # This is ostensibly a skeleton directory, which contains files that Plex
40 # copies into its folder in /var. Unfortunately, there are some SQLite
41 # databases in the directory that are opened at startup. Since these
42 # database files are read-only, SQLite chokes and Plex fails to start. To
43 # solve this, we keep the resources directory in the Nix store, but we
44 # rename the database files and replace the originals with symlinks to
45 # /var/lib/plex. Then, in the systemd unit, the base database files are
46 # copied to /var/lib/plex before starting Plex.
47 RSC=$out/usr/lib/plexmediaserver/Resources
48 for db in "com.plexapp.plugins.library.db"; do
49 mv $RSC/$db $RSC/base_$db
50 ln -s ${dataDir}/.skeleton/$db $RSC/$db
51 done
52 '';
53
54 meta = with stdenv.lib; {
55 homepage = http://plex.tv/;
56 license = licenses.unfree;
57 platforms = platforms.linux;
58 maintainers = with stdenv.lib.maintainers; [ forkk ];
59 description = "Media / DLNA server";
60 longDescription = ''
61 Plex is a media server which allows you to store your media and play it
62 back across many different devices.
63 '';
64 };
65}