git.iko.soy/typeable/nixpkgs
on master help sign in
Merge pull request #56565 from andrew-d/adunham/plex-fhs

plex: rewrite to use FHS userenv

commit857069293d4a5ac7babb5fa875879ca8a14b81ed authorGraham Christensen <graham@grahamc.com> dateWed 24 Apr 2019, 15:00 (8 years ago) parentf00135f9f7f367
4 files changed+204−134
nixos/modules/services/misc/plex.nix+41−63 pkgs/servers/plex/default.nix+92−71 pkgs/servers/plex/raw.nix+70 pkgs/top-level/all-packages.nix+1
nixos/modules/services/misc/plex.nix+41−63
@@ -10,35 +10,38 @@ in
1010 services.plex = {
1111 enable = mkEnableOption "Plex Media Server";
1212
13− # FIXME: In order for this config option to work, symlinks in the Plex
14− # package in the Nix store have to be changed to point to this directory.
1513 dataDir = mkOption {
1614 type = types.str;
1715 default = "/var/lib/plex";
18− description = "The directory where Plex stores its data files.";
16+ description = ''
17+ The directory where Plex stores its data files.
18+ '';
1919 };
2020
2121 openFirewall = mkOption {
2222 type = types.bool;
2323 default = false;
2424 description = ''
25− Open ports in the firewall for the media server
25+ Open ports in the firewall for the media server.
2626 '';
2727 };
2828
2929 user = mkOption {
3030 type = types.str;
3131 default = "plex";
32− description = "User account under which Plex runs.";
32+ description = ''
33+ User account under which Plex runs.
34+ '';
3335 };
3436
3537 group = mkOption {
3638 type = types.str;
3739 default = "plex";
38− description = "Group under which Plex runs.";
40+ description = ''
41+ Group under which Plex runs.
42+ '';
3943 };
4044
41−
4245 managePlugins = mkOption {
4346 type = types.bool;
4447 default = true;
@@ -80,73 +83,48 @@ in
8083 description = "Plex Media Server";
8184 after = [ "network.target" ];
8285 wantedBy = [ "multi-user.target" ];
83− preStart = ''
84− test -d "${cfg.dataDir}/Plex Media Server" || {
85− echo "Creating initial Plex data directory in \"${cfg.dataDir}\"."
86− mkdir -p "${cfg.dataDir}/Plex Media Server"
87− chown -R ${cfg.user}:${cfg.group} "${cfg.dataDir}"
88− }
89−
90− # Copy the database skeleton files to /var/lib/plex/.skeleton
91− # See the the Nix expression for Plex's package for more information on
92− # why this is done.
93− install --owner ${cfg.user} --group ${cfg.group} -d "${cfg.dataDir}/.skeleton"
94− for db in "com.plexapp.plugins.library.db"; do
95− if [ ! -e "${cfg.dataDir}/.skeleton/$db" ]; then
96− cp "${cfg.package}/usr/lib/plexmediaserver/Resources/base_$db" "${cfg.dataDir}/.skeleton/$db"
97− fi
98− chmod u+w "${cfg.dataDir}/.skeleton/$db"
99− chown ${cfg.user}:${cfg.group} "${cfg.dataDir}/.skeleton/$db"
100− done
101−
102− # If managePlugins is enabled, setup symlinks for plugins.
103− ${optionalString cfg.managePlugins ''
104− echo "Preparing plugin directory."
105− PLUGINDIR="${cfg.dataDir}/Plex Media Server/Plug-ins"
106− test -d "$PLUGINDIR" || {
107− mkdir -p "$PLUGINDIR";
108− chown ${cfg.user}:${cfg.group} "$PLUGINDIR";
109− }
110−
111− echo "Removing old symlinks."
112− # First, remove all of the symlinks in the directory.
113− for f in `ls "$PLUGINDIR/"`; do
114− if [[ -L "$PLUGINDIR/$f" ]]; then
115− echo "Removing plugin symlink $PLUGINDIR/$f."
116− rm "$PLUGINDIR/$f"
117− fi
118− done
119−
120− echo "Symlinking plugins."
121− for path in ${toString cfg.extraPlugins}; do
122− dest="$PLUGINDIR/$(basename $path)"
123− if [[ ! -d "$path" ]]; then
124− echo "Error symlinking plugin from $path: no such directory."
125− elif [[ -d "$dest" || -L "$dest" ]]; then
126− echo "Error symlinking plugin from $path to $dest: file or directory already exists."
127− else
128− echo "Symlinking plugin at $path..."
129− ln -s "$path" "$dest"
130− fi
131− done
132− ''}
133− '';
86+
13487 serviceConfig = {
13588 Type = "simple";
13689 User = cfg.user;
13790 Group = cfg.group;
138− PermissionsStartOnly = "true";
139− ExecStart = "\"${cfg.package}/usr/lib/plexmediaserver/Plex Media Server\"";
91+
92+ # Run the pre-start script with full permissions (the "!" prefix) so it
93+ # can create the data directory if necessary.
94+ ExecStartPre = let
95+ preStartScript = pkgs.writeScript "plex-run-prestart" ''
96+ #!${pkgs.bash}/bin/bash
97+
98+ # Create data directory if it doesn't exist
99+ if ! test -d "$PLEX_DATADIR"; then
100+ echo "Creating initial Plex data directory in: $PLEX_DATADIR"
101+ install -d -m 0755 -o "${cfg.user}" -g "${cfg.group}" "$PLEX_DATADIR"
102+ fi
103+ '';
104+ in
105+ "!${preStartScript}";
106+
107+ ExecStart = "${cfg.package}/bin/plexmediaserver";
140108 KillSignal = "SIGQUIT";
141109 Restart = "on-failure";
142110 };
111+
143112 environment = {
144− PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR=cfg.dataDir;
145− PLEX_MEDIA_SERVER_HOME="${cfg.package}/usr/lib/plexmediaserver";
113+ # Configuration for our FHS userenv script
114+ PLEX_DATADIR=cfg.dataDir;
115+ PLEX_PLUGINS=concatMapStringsSep ":" builtins.toString cfg.extraPlugins;
116+
117+ # The following variables should be set by the FHS userenv script:
118+ # PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR
119+ # PLEX_MEDIA_SERVER_HOME
120+
121+ # Allow access to GPU acceleration; the Plex LD_LIBRARY_PATH is added
122+ # by the FHS userenv script.
123+ LD_LIBRARY_PATH="/run/opengl-driver/lib";
124+
146125 PLEX_MEDIA_SERVER_MAX_PLUGIN_PROCS="6";
147126 PLEX_MEDIA_SERVER_TMPDIR="/tmp";
148127 PLEX_MEDIA_SERVER_USE_SYSLOG="true";
149− LD_LIBRARY_PATH="/run/opengl-driver/lib:${cfg.package}/usr/lib/plexmediaserver/lib";
150128 LC_ALL="en_US.UTF-8";
151129 LANG="en_US.UTF-8";
152130 };
pkgs/servers/plex/default.nix+92−71
@@ -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}
pkgs/servers/plex/raw.nixnew file+70
@@ -0,0 +1,70 @@
1+{ stdenv
2+, fetchurl
3+, rpmextract
4+}:
5+
6+# The raw package that fetches and extracts the Plex RPM. Override the source
7+# and version of this derivation if you want to use a Plex Pass version of the
8+# server, and the FHS userenv and corresponding NixOS module should
9+# automatically pick up the changes.
10+stdenv.mkDerivation rec {
11+ version = "1.15.3.876-ad6e39743";
12+ pname = "plexmediaserver";
13+ name = "${pname}-${version}";
14+
15+ # Fetch the source
16+ src = fetchurl {
17+ url = "https://downloads.plex.tv/plex-media-server-new/${version}/redhat/plexmediaserver-${version}.x86_64.rpm";
18+ sha256 = "01g7wccm01kg3nhf3qrmwcn20nkpv0bqz6zqv2gq5v03ps58h6g5";
19+ };
20+
21+ outputs = [ "out" "basedb" ];
22+
23+ nativeBuildInputs = [ rpmextract ];
24+
25+ phases = [ "unpackPhase" "installPhase" "fixupPhase" "distPhase" ];
26+
27+ unpackPhase = ''
28+ rpmextract $src
29+ '';
30+
31+ installPhase = ''
32+ mkdir -p "$out/lib"
33+ cp -dr --no-preserve='ownership' usr/lib/plexmediaserver $out/lib/
34+
35+ # Location of the initial Plex plugins database
36+ f=$out/lib/plexmediaserver/Resources/com.plexapp.plugins.library.db
37+
38+ # Store the base database in the 'basedb' output
39+ cat $f > $basedb
40+
41+ # Overwrite the base database in the Plex package with an absolute symlink
42+ # to the '/db' file; we create this path in the FHS userenv (see the "plex"
43+ # package).
44+ ln -fs /db $f
45+ '';
46+
47+ # We're running in a FHS userenv; don't patch anything
48+ dontPatchShebangs = true;
49+ dontStrip = true;
50+ dontPatchELF = true;
51+ dontAutoPatchelf = true;
52+
53+ meta = with stdenv.lib; {
54+ homepage = https://plex.tv/;
55+ license = licenses.unfree;
56+ platforms = platforms.linux;
57+ maintainers = with maintainers; [
58+ colemickens
59+ forkk
60+ lnl7
61+ pjones
62+ thoughtpolice
63+ ];
64+ description = "Media library streaming server";
65+ longDescription = ''
66+ Plex is a media server which allows you to store your media and play it
67+ back across many different devices.
68+ '';
69+ };
70+}
pkgs/top-level/all-packages.nix+1
@@ -5105,6 +5105,7 @@ in
51055105 playbar2 = libsForQt5.callPackage ../applications/audio/playbar2 { };
51065106
51075107 plex = callPackage ../servers/plex { };
5108+ plexRaw = callPackage ../servers/plex/raw.nix { };
51085109
51095110 tautulli = callPackage ../servers/tautulli { python = python2; };
51105111