at 22.05-pre 5.3 kB view raw
1{ config, pkgs, lib, ... }: 2 3with lib; 4 5let 6 cfg = config.services.plex; 7in 8{ 9 options = { 10 services.plex = { 11 enable = mkEnableOption "Plex Media Server"; 12 13 dataDir = mkOption { 14 type = types.str; 15 default = "/var/lib/plex"; 16 description = '' 17 The directory where Plex stores its data files. 18 ''; 19 }; 20 21 openFirewall = mkOption { 22 type = types.bool; 23 default = false; 24 description = '' 25 Open ports in the firewall for the media server. 26 ''; 27 }; 28 29 user = mkOption { 30 type = types.str; 31 default = "plex"; 32 description = '' 33 User account under which Plex runs. 34 ''; 35 }; 36 37 group = mkOption { 38 type = types.str; 39 default = "plex"; 40 description = '' 41 Group under which Plex runs. 42 ''; 43 }; 44 45 managePlugins = mkOption { 46 type = types.bool; 47 default = true; 48 description = '' 49 If set to true, this option will cause all of the symlinks in Plex's 50 plugin directory to be removed and symlinks for paths specified in 51 <option>extraPlugins</option> to be added. 52 ''; 53 }; 54 55 extraPlugins = mkOption { 56 type = types.listOf types.path; 57 default = []; 58 description = '' 59 A list of paths to extra plugin bundles to install in Plex's plugin 60 directory. Every time the systemd unit for Plex starts up, all of the 61 symlinks in Plex's plugin directory will be cleared and this module 62 will symlink all of the paths specified here to that directory. If 63 this behavior is undesired, set <option>managePlugins</option> to 64 false. 65 ''; 66 }; 67 68 extraScanners = mkOption { 69 type = types.listOf types.path; 70 default = []; 71 description = '' 72 A list of paths to extra scanners to install in Plex's scanners 73 directory. 74 75 Every time the systemd unit for Plex starts up, all of the symlinks 76 in Plex's scanners directory will be cleared and this module will 77 symlink all of the paths specified here to that directory. 78 ''; 79 example = literalExpression '' 80 [ 81 (fetchFromGitHub { 82 owner = "ZeroQI"; 83 repo = "Absolute-Series-Scanner"; 84 rev = "773a39f502a1204b0b0255903cee4ed02c46fde0"; 85 sha256 = "4l+vpiDdC8L/EeJowUgYyB3JPNTZ1sauN8liFAcK+PY="; 86 }) 87 ] 88 ''; 89 }; 90 91 package = mkOption { 92 type = types.package; 93 default = pkgs.plex; 94 defaultText = literalExpression "pkgs.plex"; 95 description = '' 96 The Plex package to use. Plex subscribers may wish to use their own 97 package here, pointing to subscriber-only server versions. 98 ''; 99 }; 100 }; 101 }; 102 103 config = mkIf cfg.enable { 104 # Most of this is just copied from the RPM package's systemd service file. 105 systemd.services.plex = { 106 description = "Plex Media Server"; 107 after = [ "network.target" ]; 108 wantedBy = [ "multi-user.target" ]; 109 110 serviceConfig = { 111 Type = "simple"; 112 User = cfg.user; 113 Group = cfg.group; 114 115 # Run the pre-start script with full permissions (the "!" prefix) so it 116 # can create the data directory if necessary. 117 ExecStartPre = let 118 preStartScript = pkgs.writeScript "plex-run-prestart" '' 119 #!${pkgs.bash}/bin/bash 120 121 # Create data directory if it doesn't exist 122 if ! test -d "$PLEX_DATADIR"; then 123 echo "Creating initial Plex data directory in: $PLEX_DATADIR" 124 install -d -m 0755 -o "${cfg.user}" -g "${cfg.group}" "$PLEX_DATADIR" 125 fi 126 ''; 127 in 128 "!${preStartScript}"; 129 130 ExecStart = "${cfg.package}/bin/plexmediaserver"; 131 KillSignal = "SIGQUIT"; 132 Restart = "on-failure"; 133 }; 134 135 environment = { 136 # Configuration for our FHS userenv script 137 PLEX_DATADIR=cfg.dataDir; 138 PLEX_PLUGINS=concatMapStringsSep ":" builtins.toString cfg.extraPlugins; 139 PLEX_SCANNERS=concatMapStringsSep ":" builtins.toString cfg.extraScanners; 140 141 # The following variables should be set by the FHS userenv script: 142 # PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR 143 # PLEX_MEDIA_SERVER_HOME 144 145 # Allow access to GPU acceleration; the Plex LD_LIBRARY_PATH is added 146 # by the FHS userenv script. 147 LD_LIBRARY_PATH="/run/opengl-driver/lib"; 148 149 PLEX_MEDIA_SERVER_MAX_PLUGIN_PROCS="6"; 150 PLEX_MEDIA_SERVER_TMPDIR="/tmp"; 151 PLEX_MEDIA_SERVER_USE_SYSLOG="true"; 152 LC_ALL="en_US.UTF-8"; 153 LANG="en_US.UTF-8"; 154 }; 155 }; 156 157 networking.firewall = mkIf cfg.openFirewall { 158 allowedTCPPorts = [ 32400 3005 8324 32469 ]; 159 allowedUDPPorts = [ 1900 5353 32410 32412 32413 32414 ]; 160 }; 161 162 users.users = mkIf (cfg.user == "plex") { 163 plex = { 164 group = cfg.group; 165 uid = config.ids.uids.plex; 166 }; 167 }; 168 169 users.groups = mkIf (cfg.group == "plex") { 170 plex = { 171 gid = config.ids.gids.plex; 172 }; 173 }; 174 }; 175}