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