mirror of
https://github.com/thegeneralist01/config.git
synced 2026-12-04 18:56:56 +01:00
136 lines
5 KiB
Nix
136 lines
5 KiB
Nix
{ pkgs, ... }:
|
|
let
|
|
acmeDomain = "thegeneralist01.com";
|
|
domain = "plex.${acmeDomain}";
|
|
|
|
ssl = {
|
|
forceSSL = true;
|
|
quic = true;
|
|
useACMEHost = domain;
|
|
};
|
|
|
|
|
|
config = ssl // {
|
|
listen = [
|
|
{
|
|
addr = "100.108.125.63";
|
|
port = 443;
|
|
ssl = true;
|
|
}
|
|
{
|
|
addr = "100.108.125.63";
|
|
port = 80;
|
|
}
|
|
];
|
|
|
|
locations."/" = {
|
|
proxyPass = "http://127.0.0.1:32400";
|
|
proxyWebsockets = true;
|
|
# Disable per-location recommended headers so they don't overwrite
|
|
# the localhost X-Real-IP / X-Forwarded-For override below.
|
|
recommendedProxySettings = false;
|
|
# https://arne.me/blog/plex-on-nixos
|
|
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;
|
|
# This vhost only listens on Tailscale (100.108.125.63); present Tailscale
|
|
# clients as local loopback so Plex doesn't treat 100.64.0.0/10 as remote.
|
|
proxy_set_header X-Real-IP 127.0.0.1;
|
|
proxy_set_header X-Forwarded-For 127.0.0.1;
|
|
proxy_set_header X-Forwarded-Proto $scheme;
|
|
proxy_set_header X-Forwarded-Host $host;
|
|
proxy_set_header X-Forwarded-Server $hostname;
|
|
proxy_set_header Host $host;
|
|
'';
|
|
};
|
|
};
|
|
in
|
|
{
|
|
services.plex = {
|
|
enable = true;
|
|
package = pkgs.plex;
|
|
dataDir = "/var/lib/plex";
|
|
# openFirewall = true;
|
|
};
|
|
|
|
systemd.tmpfiles.rules = [
|
|
"d /var/lib/plex/Library/Application\\040Support/Plex\\ Media\\ Server 0755 plex plex -"
|
|
"f /var/lib/plex/Library/Application\\040Support/Plex\\ Media\\ Server/Preferences.xml 0644 plex plex -"
|
|
];
|
|
|
|
systemd.services.plex-fix-perms = {
|
|
description = "Fix Plex library permissions";
|
|
wants = [ "plex.service" ]; # Plex depends on this
|
|
serviceConfig = {
|
|
Type = "oneshot";
|
|
RemainAfterExit = true;
|
|
ExecStart = ''
|
|
mkdir -p "/var/lib/plex/Library/Application Support/Plex Media Server"
|
|
chown -R plex:plex "/var/lib/plex/Library/Application Support/Plex Media Server"
|
|
'';
|
|
};
|
|
};
|
|
|
|
networking.firewall.interfaces."tailscale0" = {
|
|
allowedTCPPorts = [ 3005 8324 32469 80 443 ];
|
|
allowedUDPPorts = [ 1900 5353 32410 32412 32413 32414 ];
|
|
};
|
|
|
|
# Allow Plex direct access from the home LAN subnet without exposing 32400 globally.
|
|
# Redirect Tailscale traffic targeting :32400 through a local TCP proxy so Plex
|
|
# sees 127.0.0.1 (Loopback) instead of the client's 100.64.0.0/10 Tailscale IP.
|
|
networking.firewall.extraCommands = ''
|
|
iptables -A nixos-fw -s 192.168.2.0/24 -p tcp --dport 32400 -j nixos-fw-accept
|
|
iptables -t nat -D PREROUTING -i tailscale0 -p tcp --dport 32400 -j REDIRECT --to-ports 32499 2>/dev/null || true
|
|
iptables -t nat -D PREROUTING -i tailscale0 -p tcp --dport 32400 -j DNAT --to-destination 100.108.125.63:32499 2>/dev/null || true
|
|
iptables -t nat -A PREROUTING -i tailscale0 -p tcp --dport 32400 -j DNAT --to-destination 100.108.125.63:32499
|
|
'';
|
|
networking.firewall.extraStopCommands = ''
|
|
iptables -t nat -D PREROUTING -i tailscale0 -p tcp --dport 32400 -j DNAT --to-destination 100.108.125.63:32499 2>/dev/null || true
|
|
'';
|
|
|
|
systemd.sockets.plex-tailscale-proxy = {
|
|
description = "Socket for Tailscale Plex loopback proxy";
|
|
wantedBy = [ "sockets.target" ];
|
|
listenStreams = [ "0.0.0.0:32499" ];
|
|
};
|
|
|
|
systemd.services.plex-tailscale-proxy = {
|
|
description = "Proxy Tailscale Plex traffic through loopback";
|
|
wants = [ "plex.service" ];
|
|
requires = [ "plex-tailscale-proxy.socket" ];
|
|
after = [ "plex.service" "plex-tailscale-proxy.socket" ];
|
|
serviceConfig = {
|
|
ExecStart = "${pkgs.systemd}/lib/systemd/systemd-socket-proxyd -c 1024 127.0.0.1:32400";
|
|
DynamicUser = true;
|
|
PrivateTmp = true;
|
|
NoNewPrivileges = true;
|
|
};
|
|
};
|
|
|
|
services.nginx.virtualHosts = {
|
|
${domain} = config;
|
|
"100.108.125.63" = config;
|
|
};
|
|
|
|
systemd.services.plex = {
|
|
wants = [ "network-online.target" "tailscaled.service" ];
|
|
after = [ "network-online.target" "tailscaled.service" ];
|
|
};
|
|
}
|