1 # The actual Plex package that we run is a FHS userenv of the "raw" package.
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"
13 name = "plexmediaserver";
15 inherit (plexRaw) meta;
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.
22 # This script is run when we start our Plex binary
23 runScript = writeScript "plex-run-script" ''
28 # The root path to our Plex installation
29 root=${plexRaw}/lib/plexmediaserver
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.
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"
43 if [[ ! -d "$PLEX_DATADIR" ]]; then
44 echo "Creating Plex data directory: $PLEX_DATADIR"
45 mkdir -p "$PLEX_DATADIR"
48 # The database is stored under the given directory
49 db="$PLEX_DATADIR/.skeleton/com.plexapp.plugins.library.db"
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"
59 # Set up symbolic link at '/db', which is linked to by our Plex package
60 # (see the 'plexRaw' package).
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"
68 pluginDir="$PLEX_DATADIR/Plex Media Server/Plug-ins"
69 test -d "$pluginDir" || mkdir -p "$pluginDir"
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"
75 done < <(find "$pluginDir" -type l -print0)
77 echo "Symlinking plugins"
78 IFS=':' read -ra pluginsArray <<< "$PLEX_PLUGINS"
79 for path in "''${pluginsArray[@]}"; do
80 dest="$pluginDir/$(basename "$path")"
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"
87 echo "Symlinking plugin at: $path"
93 if [[ -n "''${PLEX_SCANNERS:-}" ]]; then
94 for scannerType in Common Movies Music Series; do
95 echo "Preparing $scannerType scanners directory"
97 scannerDir="$PLEX_DATADIR/Plex Media Server/Scanners/$scannerType"
98 test -d "$scannerDir" || mkdir -p "$scannerDir"
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"
105 done < <(find "$scannerDir" -type l -print0)
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
112 subpath="$path/Scanners/$scannerType"
113 while IFS= read -r -d $'\0' file; do
114 dest="$scannerDir/$(basename "$file")"
116 if [[ -f "$dest" || -L "$dest" ]]; then
117 echo "Error symlinking scanner from $file to $dest: file or directory already exists"
119 echo "Symlinking scanner at: $file"
120 ln -s "$file" "$dest"
122 done < <(find "$subpath" -type f -print0)
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
130 export PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR="$PLEX_DATADIR"
132 # Tell Plex where the 'home' directory for itself is.
133 export PLEX_MEDIA_SERVER_HOME="${plexRaw}/lib/plexmediaserver"
135 # Actually run Plex, prepending LD_LIBRARY_PATH with the libraries from
137 LD_LIBRARY_PATH=$LD_LIBRARY_PATH''${LD_LIBRARY_PATH:+:}$root exec "$root/Plex Media Server"