at 16.09-beta 5.3 kB view raw
1{ config, pkgs, lib, ... }: 2 3with lib; 4 5let 6 cfg = config.services.plex; 7 plex = pkgs.plex; 8in 9{ 10 options = { 11 services.plex = { 12 enable = mkEnableOption "Plex Media Server"; 13 14 # FIXME: In order for this config option to work, symlinks in the Plex 15 # package in the Nix store have to be changed to point to this directory. 16 dataDir = mkOption { 17 type = types.str; 18 default = "/var/lib/plex"; 19 description = "The directory where Plex stores its data files."; 20 }; 21 22 user = mkOption { 23 type = types.str; 24 default = "plex"; 25 description = "User account under which Plex runs."; 26 }; 27 28 group = mkOption { 29 type = types.str; 30 default = "plex"; 31 description = "Group under which Plex runs."; 32 }; 33 34 35 managePlugins = mkOption { 36 type = types.bool; 37 default = true; 38 description = '' 39 If set to true, this option will cause all of the symlinks in Plex's 40 plugin directory to be removed and symlinks for paths specified in 41 <option>extraPlugins</option> to be added. 42 ''; 43 }; 44 45 extraPlugins = mkOption { 46 type = types.listOf types.path; 47 default = []; 48 description = '' 49 A list of paths to extra plugin bundles to install in Plex's plugin 50 directory. Every time the systemd unit for Plex starts up, all of the 51 symlinks in Plex's plugin directory will be cleared and this module 52 will symlink all of the paths specified here to that directory. If 53 this behavior is undesired, set <option>managePlugins</option> to 54 false. 55 ''; 56 }; 57 58 package = mkOption { 59 type = types.package; 60 default = pkgs.plex; 61 defaultText = "pkgs.plex"; 62 description = '' 63 The Plex package to use. Plex subscribers may wish to use their own 64 package here, pointing to subscriber-only server versions. 65 ''; 66 }; 67 }; 68 }; 69 70 config = mkIf cfg.enable { 71 # Most of this is just copied from the RPM package's systemd service file. 72 systemd.services.plex = { 73 description = "Plex Media Server"; 74 after = [ "network.target" ]; 75 wantedBy = [ "multi-user.target" ]; 76 preStart = '' 77 test -d "${cfg.dataDir}" || { 78 echo "Creating initial Plex data directory in \"${cfg.dataDir}\"." 79 mkdir -p "${cfg.dataDir}/Plex Media Server" 80 chown -R ${cfg.user}:${cfg.group} "${cfg.dataDir}" 81 } 82 83 # Copy the database skeleton files to /var/lib/plex/.skeleton 84 # See the the Nix expression for Plex's package for more information on 85 # why this is done. 86 test -d "${cfg.dataDir}/.skeleton" || mkdir "${cfg.dataDir}/.skeleton" 87 for db in "com.plexapp.plugins.library.db"; do 88 cp "${cfg.package}/usr/lib/plexmediaserver/Resources/base_$db" "${cfg.dataDir}/.skeleton/$db" 89 chmod u+w "${cfg.dataDir}/.skeleton/$db" 90 chown ${cfg.user}:${cfg.group} "${cfg.dataDir}/.skeleton/$db" 91 done 92 93 # If managePlugins is enabled, setup symlinks for plugins. 94 ${optionalString cfg.managePlugins '' 95 echo "Preparing plugin directory." 96 PLUGINDIR="${cfg.dataDir}/Plex Media Server/Plug-ins" 97 test -d "$PLUGINDIR" || { 98 mkdir -p "$PLUGINDIR"; 99 chown ${cfg.user}:${cfg.group} "$PLUGINDIR"; 100 } 101 102 echo "Removing old symlinks." 103 # First, remove all of the symlinks in the directory. 104 for f in `ls "$PLUGINDIR/"`; do 105 if [[ -L "$PLUGINDIR/$f" ]]; then 106 echo "Removing plugin symlink $PLUGINDIR/$f." 107 rm "$PLUGINDIR/$f" 108 fi 109 done 110 111 echo "Symlinking plugins." 112 for path in ${toString cfg.extraPlugins}; do 113 dest="$PLUGINDIR/$(basename $path)" 114 if [[ ! -d "$path" ]]; then 115 echo "Error symlinking plugin from $path: no such directory." 116 elif [[ -d "$dest" || -L "$dest" ]]; then 117 echo "Error symlinking plugin from $path to $dest: file or directory already exists." 118 else 119 echo "Symlinking plugin at $path..." 120 ln -s "$path" "$dest" 121 fi 122 done 123 ''} 124 ''; 125 serviceConfig = { 126 Type = "simple"; 127 User = cfg.user; 128 Group = cfg.group; 129 PermissionsStartOnly = "true"; 130 ExecStart = "/bin/sh -c '${cfg.package}/usr/lib/plexmediaserver/Plex\\ Media\\ Server'"; 131 Restart = "on-failure"; 132 }; 133 environment = { 134 PLEX_MEDIA_SERVER_APPLICATION_SUPPORT_DIR=cfg.dataDir; 135 PLEX_MEDIA_SERVER_HOME="${cfg.package}/usr/lib/plexmediaserver"; 136 PLEX_MEDIA_SERVER_MAX_PLUGIN_PROCS="6"; 137 PLEX_MEDIA_SERVER_TMPDIR="/tmp"; 138 LD_LIBRARY_PATH="${cfg.package}/usr/lib/plexmediaserver"; 139 LC_ALL="en_US.UTF-8"; 140 LANG="en_US.UTF-8"; 141 }; 142 }; 143 144 users.extraUsers = mkIf (cfg.user == "plex") { 145 plex = { 146 group = cfg.group; 147 uid = config.ids.uids.plex; 148 }; 149 }; 150 151 users.extraGroups = mkIf (cfg.group == "plex") { 152 plex = { 153 gid = config.ids.gids.plex; 154 }; 155 }; 156 }; 157}