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