| 
									
										
										
										
											2015-04-16 13:15:59 -05:00
										 |  |  | { config, pkgs, lib, ... }: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | with lib; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | let | 
					
						
							|  |  |  |   cfg = config.services.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
										 |  |  | 
 | 
					
						
							|  |  |  |       dataDir = mkOption { | 
					
						
							|  |  |  |         type = types.str; | 
					
						
							|  |  |  |         default = "/var/lib/plex"; | 
					
						
							| 
									
										
										
										
											2019-02-28 05:15:28 -05:00
										 |  |  |         description = ''
 | 
					
						
							|  |  |  |           The directory where Plex stores its data files. | 
					
						
							|  |  |  |         '';
 | 
					
						
							| 
									
										
										
										
											2015-04-16 13:15:59 -05:00
										 |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-18 21:59:06 -05:00
										 |  |  |       openFirewall = mkOption { | 
					
						
							|  |  |  |         type = types.bool; | 
					
						
							|  |  |  |         default = false; | 
					
						
							|  |  |  |         description = ''
 | 
					
						
							| 
									
										
										
										
											2019-02-28 05:15:28 -05:00
										 |  |  |           Open ports in the firewall for the media server. | 
					
						
							| 
									
										
										
										
											2016-12-18 21:59:06 -05:00
										 |  |  |         '';
 | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-16 13:15:59 -05:00
										 |  |  |       user = mkOption { | 
					
						
							|  |  |  |         type = types.str; | 
					
						
							|  |  |  |         default = "plex"; | 
					
						
							| 
									
										
										
										
											2019-02-28 05:15:28 -05:00
										 |  |  |         description = ''
 | 
					
						
							|  |  |  |           User account under which Plex runs. | 
					
						
							|  |  |  |         '';
 | 
					
						
							| 
									
										
										
										
											2015-04-16 13:15:59 -05:00
										 |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       group = mkOption { | 
					
						
							|  |  |  |         type = types.str; | 
					
						
							|  |  |  |         default = "plex"; | 
					
						
							| 
									
										
										
										
											2019-02-28 05:15:28 -05:00
										 |  |  |         description = ''
 | 
					
						
							|  |  |  |           Group under which Plex runs. | 
					
						
							|  |  |  |         '';
 | 
					
						
							| 
									
										
										
										
											2015-04-16 13:15:59 -05:00
										 |  |  |       }; | 
					
						
							| 
									
										
										
										
											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" ]; | 
					
						
							| 
									
										
										
										
											2019-02-28 05:15:28 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-16 13:15:59 -05:00
										 |  |  |       serviceConfig = { | 
					
						
							|  |  |  |         Type = "simple"; | 
					
						
							|  |  |  |         User = cfg.user; | 
					
						
							|  |  |  |         Group = cfg.group; | 
					
						
							| 
									
										
										
										
											2019-02-28 05:15:28 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # Run the pre-start script with full permissions (the "!" prefix) so it | 
					
						
							|  |  |  |         # can create the data directory if necessary. | 
					
						
							|  |  |  |         ExecStartPre = let | 
					
						
							|  |  |  |           preStartScript = pkgs.writeScript "plex-run-prestart" ''
 | 
					
						
							|  |  |  |             #!${pkgs.bash}/bin/bash | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             # Create data directory if it doesn't exist | 
					
						
							|  |  |  |             if ! test -d "$PLEX_DATADIR"; then | 
					
						
							|  |  |  |               echo "Creating initial Plex data directory in: $PLEX_DATADIR" | 
					
						
							|  |  |  |               install -d -m 0755 -o "${cfg.user}" -g "${cfg.group}" "$PLEX_DATADIR" | 
					
						
							|  |  |  |             fi | 
					
						
							|  |  |  |          '';
 | 
					
						
							|  |  |  |         in | 
					
						
							|  |  |  |           "!${preStartScript}"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         ExecStart = "${cfg.package}/bin/plexmediaserver"; | 
					
						
							| 
									
										
										
										
											2017-04-14 11:19:29 -07:00
										 |  |  |         KillSignal = "SIGQUIT"; | 
					
						
							| 
									
										
										
										
											2016-04-08 10:55:00 -07:00
										 |  |  |         Restart = "on-failure"; | 
					
						
							| 
									
										
										
										
											2015-04-16 13:15:59 -05:00
										 |  |  |       }; | 
					
						
							| 
									
										
										
										
											2019-02-28 05:15:28 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-16 13:15:59 -05:00
										 |  |  |       environment = { | 
					
						
							| 
									
										
										
										
											2019-02-28 05:15:28 -05:00
										 |  |  |         # Configuration for our FHS userenv script | 
					
						
							|  |  |  |         PLEX_DATADIR=cfg.dataDir; | 
					
						
							|  |  |  |         PLEX_PLUGINS=concatMapStringsSep ":" builtins.toString cfg.extraPlugins; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # The following variables should be set by the FHS userenv script: | 
					
						
							|  |  |  |         #   PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR | 
					
						
							|  |  |  |         #   PLEX_MEDIA_SERVER_HOME | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Allow access to GPU acceleration; the Plex LD_LIBRARY_PATH is added | 
					
						
							|  |  |  |         # by the FHS userenv script. | 
					
						
							|  |  |  |         LD_LIBRARY_PATH="/run/opengl-driver/lib"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-16 13:15:59 -05:00
										 |  |  |         PLEX_MEDIA_SERVER_MAX_PLUGIN_PROCS="6"; | 
					
						
							|  |  |  |         PLEX_MEDIA_SERVER_TMPDIR="/tmp"; | 
					
						
							| 
									
										
										
										
											2019-02-11 10:47:21 +08:00
										 |  |  |         PLEX_MEDIA_SERVER_USE_SYSLOG="true"; | 
					
						
							| 
									
										
										
										
											2015-04-16 13:15:59 -05:00
										 |  |  |         LC_ALL="en_US.UTF-8"; | 
					
						
							|  |  |  |         LANG="en_US.UTF-8"; | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-18 21:59:06 -05:00
										 |  |  |     networking.firewall = mkIf cfg.openFirewall { | 
					
						
							|  |  |  |       allowedTCPPorts = [ 32400 3005 8324 32469 ]; | 
					
						
							|  |  |  |       allowedUDPPorts = [ 1900 5353 32410 32412 32413 32414 ]; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-30 01:58:35 +02:00
										 |  |  |     users.users = mkIf (cfg.user == "plex") { | 
					
						
							| 
									
										
										
										
											2015-04-16 13:15:59 -05:00
										 |  |  |       plex = { | 
					
						
							|  |  |  |         group = cfg.group; | 
					
						
							|  |  |  |         uid = config.ids.uids.plex; | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-30 01:58:35 +02:00
										 |  |  |     users.groups = mkIf (cfg.group == "plex") { | 
					
						
							| 
									
										
										
										
											2015-04-16 13:15:59 -05:00
										 |  |  |       plex = { | 
					
						
							|  |  |  |         gid = config.ids.gids.plex; | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | } |