1 { config, pkgs, lib, ... }:
6 cfg = config.services.plex;
10 (mkRemovedOptionModule [ "services" "plex" "managePlugins" ] "Please omit or define the option: `services.plex.extraPlugins' instead.")
15 enable = mkEnableOption (lib.mdDoc "Plex Media Server");
19 default = "/var/lib/plex";
20 description = lib.mdDoc ''
21 The directory where Plex stores its data files.
25 openFirewall = mkOption {
28 description = lib.mdDoc ''
29 Open ports in the firewall for the media server.
36 description = lib.mdDoc ''
37 User account under which Plex runs.
44 description = lib.mdDoc ''
45 Group under which Plex runs.
49 extraPlugins = mkOption {
50 type = types.listOf types.path;
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.
58 example = literalExpression ''
61 name = "Audnexus.bundle";
62 path = pkgs.fetchFromGitHub {
64 repo = "Audnexus.bundle";
66 sha256 = "sha256-IWOSz3vYL7zhdHan468xNc6C/eQ2C2BukQlaJNLXh7E=";
73 extraScanners = mkOption {
74 type = types.listOf types.path;
76 description = lib.mdDoc ''
77 A list of paths to extra scanners to install in Plex's scanners
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.
84 example = literalExpression ''
88 repo = "Absolute-Series-Scanner";
89 rev = "773a39f502a1204b0b0255903cee4ed02c46fde0";
90 sha256 = "4l+vpiDdC8L/EeJowUgYyB3JPNTZ1sauN8liFAcK+PY=";
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.
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" ];
120 # Run the pre-start script with full permissions (the "!" prefix) so it
121 # can create the data directory if necessary.
123 preStartScript = pkgs.writeScript "plex-run-prestart" ''
124 #!${pkgs.bash}/bin/bash
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"
133 "!${preStartScript}";
135 ExecStart = "${cfg.package}/bin/plexmediaserver";
136 KillSignal = "SIGQUIT";
137 PIDFile = "${cfg.dataDir}/Plex Media Server/plexmediaserver.pid";
138 Restart = "on-failure";
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;
147 # The following variables should be set by the FHS userenv script:
148 # PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR
149 # PLEX_MEDIA_SERVER_HOME
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";
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";
163 networking.firewall = mkIf cfg.openFirewall {
164 allowedTCPPorts = [ 32400 3005 8324 32469 ];
165 allowedUDPPorts = [ 1900 5353 32410 32412 32413 32414 ];
168 users.users = mkIf (cfg.user == "plex") {
171 uid = config.ids.uids.plex;
175 users.groups = mkIf (cfg.group == "plex") {
177 gid = config.ids.gids.plex;