From 1a4b5ee8b284f5f181843f5de4b58e2ffde92cc3 Mon Sep 17 00:00:00 2001 From: arcnmx Date: Sun, 21 Jan 2024 11:34:19 -0800 Subject: [PATCH] refactor(plex): access --- nixos/access/plex.nix | 82 ++++++++++++++++++++++++++------------ nixos/plex.nix | 45 +-------------------- systems/mediabox/nixos.nix | 1 + 3 files changed, 60 insertions(+), 68 deletions(-) diff --git a/nixos/access/plex.nix b/nixos/access/plex.nix index 43f694da..eb07de08 100644 --- a/nixos/access/plex.nix +++ b/nixos/access/plex.nix @@ -1,32 +1,64 @@ { config, lib, - meta, ... -}: -with lib; { - services.nginx.virtualHosts."plex.${config.networking.domain}" = { - locations = { - "/" = { - proxyPass = meta.tailnet.yukari.pp 4 32400; - extraConfig = '' - proxy_set_header Upgrade $http_upgrade; - proxy_set_header Connection "upgrade"; - proxy_redirect off; - proxy_buffering off; - proxy_set_header X-Plex-Client-Identifier $http_x_plex_client_identifier; - proxy_set_header X-Plex-Device $http_x_plex_device; - proxy_set_header X-Plex-Device-Name $http_x_plex_device_name; - proxy_set_header X-Plex-Platform $http_x_plex_platform; - proxy_set_header X-Plex-Platform-Version $http_x_plex_platform_version; - proxy_set_header X-Plex-Product $http_x_plex_product; - proxy_set_header X-Plex-Token $http_x_plex_token; - proxy_set_header X-Plex-Version $http_x_plex_version; - proxy_set_header X-Plex-Nocache $http_x_plex_nocache; - proxy_set_header X-Plex-Provides $http_x_plex_provides; - proxy_set_header X-Plex-Device-Vendor $http_x_plex_device_vendor; - proxy_set_header X-Plex-Model $http_x_plex_model; - ''; +}: let + inherit (lib.options) mkOption; + inherit (lib.modules) mkIf; + cfg = config.services.plex; + access = config.services.nginx.access.plex; +in { + options.services.nginx.access.plex = with lib.types; { + url = mkOption { + type = str; + }; + domain = mkOption { + type = str; + default = "plex.${config.networking.domain}"; + }; + localDomain = mkOption { + type = str; + default = "plex.local.${config.networking.domain}"; + }; + }; + config.services.nginx = { + access.plex = mkIf cfg.enable { + url = "http://localhost:32400"; + }; + virtualHosts = let + extraConfig = '' + # Some players don't reopen a socket and playback stops totally instead of resuming after an extended pause + send_timeout 100m; + # Plex headers + proxy_set_header X-Plex-Client-Identifier $http_x_plex_client_identifier; + proxy_set_header X-Plex-Device $http_x_plex_device; + proxy_set_header X-Plex-Device-Name $http_x_plex_device_name; + proxy_set_header X-Plex-Platform $http_x_plex_platform; + proxy_set_header X-Plex-Platform-Version $http_x_plex_platform_version; + proxy_set_header X-Plex-Product $http_x_plex_product; + proxy_set_header X-Plex-Token $http_x_plex_token; + proxy_set_header X-Plex-Version $http_x_plex_version; + proxy_set_header X-Plex-Nocache $http_x_plex_nocache; + proxy_set_header X-Plex-Provides $http_x_plex_provides; + proxy_set_header X-Plex-Device-Vendor $http_x_plex_device_vendor; + proxy_set_header X-Plex-Model $http_x_plex_model; + # Buffering off send to the client as soon as the data is received from Plex. + proxy_redirect off; + proxy_buffering off; + ''; + in { + ${access.domain} = { + locations."/" = { + proxyPass = access.url; + }; + inherit extraConfig; + }; + ${access.localDomain} = { + local.enable = true; + locations."/" = { + proxyPass = access.url; + }; + inherit extraConfig; }; }; }; diff --git a/nixos/plex.nix b/nixos/plex.nix index 24f2f1df..fa607541 100644 --- a/nixos/plex.nix +++ b/nixos/plex.nix @@ -1,46 +1,5 @@ -{config, ...}: { - services = { - plex = { - enable = true; - }; - nginx.virtualHosts = let - extraConfig = '' - # Some players don't reopen a socket and playback stops totally instead of resuming after an extended pause - send_timeout 100m; - # Plex headers - proxy_set_header X-Plex-Client-Identifier $http_x_plex_client_identifier; - proxy_set_header X-Plex-Device $http_x_plex_device; - proxy_set_header X-Plex-Device-Name $http_x_plex_device_name; - proxy_set_header X-Plex-Platform $http_x_plex_platform; - proxy_set_header X-Plex-Platform-Version $http_x_plex_platform_version; - proxy_set_header X-Plex-Product $http_x_plex_product; - proxy_set_header X-Plex-Token $http_x_plex_token; - proxy_set_header X-Plex-Version $http_x_plex_version; - proxy_set_header X-Plex-Nocache $http_x_plex_nocache; - proxy_set_header X-Plex-Provides $http_x_plex_provides; - proxy_set_header X-Plex-Device-Vendor $http_x_plex_device_vendor; - proxy_set_header X-Plex-Model $http_x_plex_model; - # Buffering off send to the client as soon as the data is received from Plex. - proxy_redirect off; - proxy_buffering off; - ''; - proxyPass = "http://localhost:32400"; - in { - "plex.${config.networking.domain}" = { - locations."/" = { - inherit proxyPass; - }; - inherit extraConfig; - }; - "plex.local.${config.networking.domain}" = { - local.enable = true; - locations."/" = { - inherit proxyPass; - }; - inherit extraConfig; - }; - }; - }; +{...}: { + services.plex.enable = true; # Plex Media Server: # diff --git a/systems/mediabox/nixos.nix b/systems/mediabox/nixos.nix index c276e740..2df23a6f 100644 --- a/systems/mediabox/nixos.nix +++ b/systems/mediabox/nixos.nix @@ -9,6 +9,7 @@ nixos.reisen-ct nixos.sops nixos.nginx + nixos.access.plex nixos.cloudflared # media