at master 140 lines 5.2 kB view raw
1# The actual Plex package that we run is a FHS userenv of the "raw" package. 2{ 3 stdenv, 4 buildFHSEnv, 5 writeScript, 6 plexRaw, 7 8 # Old argument for overriding the Plex data directory; not used for this 9 # version of Plex, but still around for backwards-compatibility. 10 dataDir ? "/var/lib/plex", 11}: 12 13buildFHSEnv { 14 pname = "plexmediaserver"; 15 16 inherit (plexRaw) version meta; 17 18 # Plex does some magic to detect if it is already running. 19 # The separate PID namespace somehow breaks this and Plex is thinking it's already 20 # running and refuses to start. 21 unsharePid = false; 22 23 # This script is run when we start our Plex binary 24 runScript = writeScript "plex-run-script" '' 25 #!${stdenv.shell} 26 27 set -eu 28 29 # The root path to our Plex installation 30 root=${plexRaw}/lib/plexmediaserver 31 32 # Path to where we're storing our Plex data files. We default to storing 33 # them in the user's home directory under the XDG-compatible location, but 34 # allow overriding with an environment variable so the location can be 35 # configured in our NixOS module. 36 # 37 # NOTE: the old version of Plex used /var/lib/plex as the default location, 38 # but this package shouldn't assume that we're going to run Plex with the 39 # ability to write to /var/lib, so using a subdirectory of $HOME when none 40 # is specified feels less likely to have permission errors. 41 if [[ -z "''${PLEX_DATADIR:-}" ]]; then 42 PLEX_DATADIR="$HOME/.local/share/plex" 43 fi 44 if [[ ! -d "$PLEX_DATADIR" ]]; then 45 echo "Creating Plex data directory: $PLEX_DATADIR" 46 mkdir -p "$PLEX_DATADIR" 47 fi 48 49 # The database is stored under the given directory 50 db="$PLEX_DATADIR/.skeleton/com.plexapp.plugins.library.db" 51 52 # If we don't have a database in the expected path, then create one by 53 # copying our base database to that location. 54 if ! test -f "$db"; then 55 echo "Copying base database file to: $db" 56 mkdir -p "$(dirname "$db")" 57 cat "${plexRaw.basedb}" > "$db" 58 fi 59 60 # Set up symbolic link at '/db', which is linked to by our Plex package 61 # (see the 'plexRaw' package). 62 ln -s "$db" /db 63 64 # If we have a plugin list (set by our NixOS module), we create plugins in 65 # the data directory as expected. This is a colon-separated list of paths. 66 if [[ -n "''${PLEX_PLUGINS:-}" ]]; then 67 echo "Preparing plugin directory" 68 69 pluginDir="$PLEX_DATADIR/Plex Media Server/Plug-ins" 70 test -d "$pluginDir" || mkdir -p "$pluginDir" 71 72 # First, remove all of the symlinks in the plugins directory. 73 while IFS= read -r -d $'\0' f; do 74 echo "Removing plugin symlink: $f" 75 rm "$f" 76 done < <(find "$pluginDir" -type l -print0) 77 78 echo "Symlinking plugins" 79 IFS=':' read -ra pluginsArray <<< "$PLEX_PLUGINS" 80 for path in "''${pluginsArray[@]}"; do 81 dest="$pluginDir/$(basename "$path")" 82 83 if [[ ! -d "$path" ]]; then 84 echo "Error symlinking plugin from $path: no such directory" 85 elif [[ -d "$dest" || -L "$dest" ]]; then 86 echo "Error symlinking plugin from $path to $dest: file or directory already exists" 87 else 88 echo "Symlinking plugin at: $path" 89 ln -s "$path" "$dest" 90 fi 91 done 92 fi 93 94 if [[ -n "''${PLEX_SCANNERS:-}" ]]; then 95 for scannerType in Common Movies Music Series; do 96 echo "Preparing $scannerType scanners directory" 97 98 scannerDir="$PLEX_DATADIR/Plex Media Server/Scanners/$scannerType" 99 test -d "$scannerDir" || mkdir -p "$scannerDir" 100 101 # First, remove all of the symlinks in the scanners directory. 102 echo "Removing old symlinks" 103 while IFS= read -r -d $'\0' f; do 104 echo "Removing scanner symlink: $f" 105 rm "$f" 106 done < <(find "$scannerDir" -type l -print0) 107 108 echo "Symlinking scanners" 109 IFS=':' read -ra scannersArray <<< "$PLEX_SCANNERS" 110 for path in "''${scannersArray[@]}"; do 111 # The provided source should contain a 'Scanners' directory; symlink 112 # from inside that. 113 subpath="$path/Scanners/$scannerType" 114 while IFS= read -r -d $'\0' file; do 115 dest="$scannerDir/$(basename "$file")" 116 117 if [[ -f "$dest" || -L "$dest" ]]; then 118 echo "Error symlinking scanner from $file to $dest: file or directory already exists" 119 else 120 echo "Symlinking scanner at: $file" 121 ln -s "$file" "$dest" 122 fi 123 done < <(find "$subpath" -type f -print0) 124 done 125 done 126 fi 127 128 # Tell Plex to use the data directory as the "Application Support" 129 # directory, otherwise it tries to write things into the user's home 130 # directory. 131 export PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR="$PLEX_DATADIR" 132 133 # Tell Plex where the 'home' directory for itself is. 134 export PLEX_MEDIA_SERVER_HOME="${plexRaw}/lib/plexmediaserver" 135 136 # Actually run Plex, prepending LD_LIBRARY_PATH with the libraries from 137 # the Plex package. 138 LD_LIBRARY_PATH=$LD_LIBRARY_PATH''${LD_LIBRARY_PATH:+:}$root exec "$root/Plex Media Server" 139 ''; 140}