| 
									
										
										
										
											2015-04-16 13:15:59 -05:00
										 |  |  | { config, pkgs, lib, ... }: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | with lib; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | let | 
					
						
							|  |  |  |   cfg = config.services.plex; | 
					
						
							|  |  |  |   plex = pkgs.plex; | 
					
						
							|  |  |  | in | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   options = { | 
					
						
							|  |  |  |     services.plex = { | 
					
						
							| 
									
										
										
										
											2015-06-21 18:19:46 +03:00
										 |  |  |       enable = mkEnableOption "Plex Media Server"; | 
					
						
							| 
									
										
										
										
											2015-04-16 13:15:59 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |       # FIXME: In order for this config option to work, symlinks in the Plex | 
					
						
							|  |  |  |       # package in the Nix store have to be changed to point to this directory. | 
					
						
							|  |  |  |       dataDir = mkOption { | 
					
						
							|  |  |  |         type = types.str; | 
					
						
							|  |  |  |         default = "/var/lib/plex"; | 
					
						
							|  |  |  |         description = "The directory where Plex stores its data files."; | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       user = mkOption { | 
					
						
							|  |  |  |         type = types.str; | 
					
						
							|  |  |  |         default = "plex"; | 
					
						
							|  |  |  |         description = "User account under which Plex runs."; | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       group = mkOption { | 
					
						
							|  |  |  |         type = types.str; | 
					
						
							|  |  |  |         default = "plex"; | 
					
						
							|  |  |  |         description = "Group under which Plex runs."; | 
					
						
							|  |  |  |       }; | 
					
						
							| 
									
										
										
										
											2015-04-17 14:45:10 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       managePlugins = mkOption { | 
					
						
							|  |  |  |         type = types.bool; | 
					
						
							|  |  |  |         default = true; | 
					
						
							|  |  |  |         description = ''
 | 
					
						
							|  |  |  |           If set to true, this option will cause all of the symlinks in Plex's | 
					
						
							|  |  |  |           plugin directory to be removed and symlinks for paths specified in | 
					
						
							|  |  |  |           <option>extraPlugins</option> to be added. | 
					
						
							|  |  |  |         '';
 | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       extraPlugins = mkOption { | 
					
						
							|  |  |  |         type = types.listOf types.path; | 
					
						
							|  |  |  |         default = []; | 
					
						
							|  |  |  |         description = ''
 | 
					
						
							|  |  |  |           A list of paths to extra plugin bundles to install in Plex's plugin | 
					
						
							|  |  |  |           directory. Every time the systemd unit for Plex starts up, all of the | 
					
						
							|  |  |  |           symlinks in Plex's plugin directory will be cleared and this module | 
					
						
							|  |  |  |           will symlink all of the paths specified here to that directory. If | 
					
						
							|  |  |  |           this behavior is undesired, set <option>managePlugins</option> to | 
					
						
							|  |  |  |           false. | 
					
						
							|  |  |  |         '';
 | 
					
						
							|  |  |  |       }; | 
					
						
							| 
									
										
										
										
											2015-12-30 20:19:04 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  |       package = mkOption { | 
					
						
							|  |  |  |         type = types.package; | 
					
						
							|  |  |  |         default = pkgs.plex; | 
					
						
							| 
									
										
										
										
											2016-02-17 20:22:56 +01:00
										 |  |  |         defaultText = "pkgs.plex"; | 
					
						
							| 
									
										
										
										
											2015-12-30 20:19:04 -06:00
										 |  |  |         description = ''
 | 
					
						
							|  |  |  |           The Plex package to use. Plex subscribers may wish to use their own | 
					
						
							|  |  |  |           package here, pointing to subscriber-only server versions. | 
					
						
							|  |  |  |         '';
 | 
					
						
							|  |  |  |       }; | 
					
						
							| 
									
										
										
										
											2015-04-16 13:15:59 -05:00
										 |  |  |     }; | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   config = mkIf cfg.enable { | 
					
						
							|  |  |  |     # Most of this is just copied from the RPM package's systemd service file. | 
					
						
							|  |  |  |     systemd.services.plex = { | 
					
						
							|  |  |  |       description = "Plex Media Server"; | 
					
						
							|  |  |  |       after = [ "network.target" ]; | 
					
						
							|  |  |  |       wantedBy = [ "multi-user.target" ]; | 
					
						
							|  |  |  |       preStart = ''
 | 
					
						
							|  |  |  |         test -d "${cfg.dataDir}" || { | 
					
						
							|  |  |  |           echo "Creating initial Plex data directory in \"${cfg.dataDir}\"." | 
					
						
							| 
									
										
										
										
											2016-01-17 12:34:54 +01:00
										 |  |  |           mkdir -p "${cfg.dataDir}/Plex Media Server" | 
					
						
							| 
									
										
										
										
											2015-04-16 13:15:59 -05:00
										 |  |  |           chown -R ${cfg.user}:${cfg.group} "${cfg.dataDir}" | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-04-17 14:45:10 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-16 13:15:59 -05:00
										 |  |  |         # Copy the database skeleton files to /var/lib/plex/.skeleton | 
					
						
							| 
									
										
										
										
											2015-04-17 14:45:10 -05:00
										 |  |  |         # See the the Nix expression for Plex's package for more information on | 
					
						
							|  |  |  |         # why this is done. | 
					
						
							| 
									
										
										
										
											2015-04-16 13:15:59 -05:00
										 |  |  |         test -d "${cfg.dataDir}/.skeleton" || mkdir "${cfg.dataDir}/.skeleton" | 
					
						
							|  |  |  |         for db in "com.plexapp.plugins.library.db"; do | 
					
						
							| 
									
										
										
										
											2015-12-30 20:19:04 -06:00
										 |  |  |             cp "${cfg.package}/usr/lib/plexmediaserver/Resources/base_$db" "${cfg.dataDir}/.skeleton/$db" | 
					
						
							| 
									
										
										
										
											2015-04-16 13:15:59 -05:00
										 |  |  |             chmod u+w "${cfg.dataDir}/.skeleton/$db" | 
					
						
							|  |  |  |             chown ${cfg.user}:${cfg.group} "${cfg.dataDir}/.skeleton/$db" | 
					
						
							|  |  |  |         done | 
					
						
							| 
									
										
										
										
											2015-04-17 14:45:10 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # If managePlugins is enabled, setup symlinks for plugins. | 
					
						
							|  |  |  |         ${optionalString cfg.managePlugins ''
 | 
					
						
							|  |  |  |           echo "Preparing plugin directory." | 
					
						
							|  |  |  |           PLUGINDIR="${cfg.dataDir}/Plex Media Server/Plug-ins" | 
					
						
							|  |  |  |           test -d "$PLUGINDIR" || { | 
					
						
							|  |  |  |             mkdir -p "$PLUGINDIR"; | 
					
						
							|  |  |  |             chown ${cfg.user}:${cfg.group} "$PLUGINDIR"; | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           echo "Removing old symlinks." | 
					
						
							|  |  |  |           # First, remove all of the symlinks in the directory. | 
					
						
							|  |  |  |           for f in `ls "$PLUGINDIR/"`; do | 
					
						
							|  |  |  |             if [[ -L "$PLUGINDIR/$f" ]]; then | 
					
						
							|  |  |  |               echo "Removing plugin symlink $PLUGINDIR/$f." | 
					
						
							|  |  |  |               rm "$PLUGINDIR/$f" | 
					
						
							|  |  |  |             fi | 
					
						
							|  |  |  |           done | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           echo "Symlinking plugins." | 
					
						
							|  |  |  |           for path in ${toString cfg.extraPlugins}; do | 
					
						
							|  |  |  |             dest="$PLUGINDIR/$(basename $path)" | 
					
						
							|  |  |  |             if [[ ! -d "$path" ]]; then | 
					
						
							|  |  |  |               echo "Error symlinking plugin from $path: no such directory." | 
					
						
							|  |  |  |             elif [[ -d "$dest" || -L "$dest" ]]; then | 
					
						
							|  |  |  |               echo "Error symlinking plugin from $path to $dest: file or directory already exists." | 
					
						
							|  |  |  |             else | 
					
						
							|  |  |  |               echo "Symlinking plugin at $path..." | 
					
						
							|  |  |  |               ln -s "$path" "$dest" | 
					
						
							|  |  |  |             fi | 
					
						
							|  |  |  |           done | 
					
						
							|  |  |  |         ''}
 | 
					
						
							| 
									
										
										
										
											2015-04-16 13:15:59 -05:00
										 |  |  |      '';
 | 
					
						
							|  |  |  |       serviceConfig = { | 
					
						
							|  |  |  |         Type = "simple"; | 
					
						
							|  |  |  |         User = cfg.user; | 
					
						
							|  |  |  |         Group = cfg.group; | 
					
						
							|  |  |  |         PermissionsStartOnly = "true"; | 
					
						
							| 
									
										
										
										
											2015-12-30 20:19:04 -06:00
										 |  |  |         ExecStart = "/bin/sh -c '${cfg.package}/usr/lib/plexmediaserver/Plex\\ Media\\ Server'"; | 
					
						
							| 
									
										
										
										
											2016-04-08 10:55:00 -07:00
										 |  |  |         Restart = "on-failure"; | 
					
						
							| 
									
										
										
										
											2015-04-16 13:15:59 -05:00
										 |  |  |       }; | 
					
						
							|  |  |  |       environment = { | 
					
						
							|  |  |  |         PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR=cfg.dataDir; | 
					
						
							| 
									
										
										
										
											2015-12-30 20:19:04 -06:00
										 |  |  |         PLEX_MEDIA_SERVER_HOME="${cfg.package}/usr/lib/plexmediaserver"; | 
					
						
							| 
									
										
										
										
											2015-04-16 13:15:59 -05:00
										 |  |  |         PLEX_MEDIA_SERVER_MAX_PLUGIN_PROCS="6"; | 
					
						
							|  |  |  |         PLEX_MEDIA_SERVER_TMPDIR="/tmp"; | 
					
						
							| 
									
										
										
										
											2015-12-30 20:19:04 -06:00
										 |  |  |         LD_LIBRARY_PATH="${cfg.package}/usr/lib/plexmediaserver"; | 
					
						
							| 
									
										
										
										
											2015-04-16 13:15:59 -05:00
										 |  |  |         LC_ALL="en_US.UTF-8"; | 
					
						
							|  |  |  |         LANG="en_US.UTF-8"; | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     users.extraUsers = mkIf (cfg.user == "plex") { | 
					
						
							|  |  |  |       plex = { | 
					
						
							|  |  |  |         group = cfg.group; | 
					
						
							|  |  |  |         uid = config.ids.uids.plex; | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     users.extraGroups = mkIf (cfg.group == "plex") { | 
					
						
							|  |  |  |       plex = { | 
					
						
							|  |  |  |         gid = config.ids.gids.plex; | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | } |