{ config, pkgs, ... }: let user = "sunshine"; resolutionScript = pkgs.writeTextFile { name = "sunshine-resolution-script"; text = '' #!${pkgs.bash}/bin/bash width=''${1:-1280} height=''${2:-720} refresh_rate=''${3:-120} # Get the modeline info from the 2nd row in the cvt output modeline=$(${pkgs.xorg.libxcvt}/bin/cvt ''${width} ''${height} ''${refresh_rate} | ${pkgs.gawk}/bin/gawk 'FNR == 2') xrandr_mode_str=''${modeline//Modeline \"*\" /} mode_alias="''${width}x''${height}" echo "xrandr setting new mode ''${mode_alias} ''${xrandr_mode_str}" # Check if mode exists before trying to remove it if ${pkgs.xorg.xrandr}/bin/xrandr --listmodes | grep -q "^''${mode_alias}"; then ${pkgs.xorg.xrandr}/bin/xrandr --rmmode ''${mode_alias} || echo "Failed to remove existing mode" fi ${pkgs.xorg.xrandr}/bin/xrandr --newmode ''${mode_alias} ''${xrandr_mode_str} || echo "Failed to create new mode" ${pkgs.xorg.xrandr}/bin/xrandr --addmode DP-0 ''${mode_alias} || echo "Failed to add mode to output" # Apply new xrandr mode ${pkgs.xorg.xrandr}/bin/xrandr --output DP-0 --primary --mode ''${mode_alias} --pos 0x0 --rotate normal || echo "Failed to apply mode" ${config.boot.kernelPackages.nvidia_x11.settings}/bin/nvidia-settings -a 'SyncToVBlank=0' || echo "Failed to disable VSync" ${config.boot.kernelPackages.nvidia_x11.bin}/bin/nvidia-smi --persistence-mode=ENABLED || echo "Failed to enable persistence mode" ''; executable = true; destination = "/bin/resolution.sh"; }; sunshinePkg = (pkgs.unstablePkgs.sunshine.override { cudaSupport = true; }); in { boot.kernelModules = [ "uinput" ]; environment.systemPackages = with pkgs.unstablePkgs; [ gamemode heroic ]; hardware = { pulseaudio.enable = false; nvidia = { modesetting.enable = true; powerManagement.enable = false; powerManagement.finegrained = false; open = false; nvidiaSettings = true; package = config.boot.kernelPackages.nvidiaPackages.latest; }; }; systemd.services.NetworkManager-wait-online.enable = pkgs.lib.mkForce false; programs.steam = { enable = true; gamescopeSession.enable = true; }; security = { polkit.extraConfig = '' polkit.addRule(function(action, subject) { if (action.id == "org.freedesktop.login1.suspend" || action.id == "org.freedesktop.login1.suspend-multiple-sessions" || action.id == "org.freedesktop.login1.hibernate" || action.id == "org.freedesktop.login1.hibernate-multiple-sessions") { return polkit.Result.NO; } }); ''; rtkit.enable = true; }; systemd.user.services.sunshine = { serviceConfig = { Restart = pkgs.lib.mkForce "always"; }; }; services = { sunshine = { enable = true; autoStart = true; package = sunshinePkg; settings = { sunshine_name = "The Architect"; capture = "nvfbc"; encoder = "nvenc"; wan_encryption_mode = 0; lan_encryption_mode = 0; origin_web_ui_allowed = "lan"; min_threads = 12; log_path = "sunshine.log"; back_button_timeout = 2500; }; applications = { env = { VDPAU_DRIVER = "nvidia"; LIBVA_DRIVER_NAME = "nvidia"; NVD_BACKEND = "direct"; __GL_SYNC_TO_VBLANK = "0"; __GL_VRR_ALLOWED = "0"; DXVK_ASYNC = "1"; }; apps = [ { name = "Steam w/ Hue Lights"; cmd = ''${pkgs.bash}/bin/bash -c "${pkgs.gamescope}/bin/gamescope --adaptive-sync --force-composition --immediate-flips --rt -C 3000 -f -e -W ''${SUNSHINE_CLIENT_WIDTH} -H ''${SUNSHINE_CLIENT_HEIGHT} -r ''${SUNSHINE_CLIENT_FPS} -- ${pkgs.steam}/bin/steam -pipewire"''; detached = [ "${pkgs.pepePkgs.huenicorn}/bin/huenicorn" ]; prep-cmd = [ { do = ''${pkgs.bash}/bin/bash -c "${resolutionScript}/bin/resolution.sh ''${SUNSHINE_CLIENT_WIDTH} ''${SUNSHINE_CLIENT_HEIGHT}" ''${SUNSHINE_CLIENT_FPS}"''; undo = ''${pkgs.bash}/bin/bash -c "${pkgs.procps}/bin/pkill gamescope; ${pkgs.procps}/bin/pkill sunshine; ${pkgs.procps}/bin/pkill -KILL huenicorn"''; } ]; } { name = "Steam"; cmd = ''${pkgs.bash}/bin/bash -c "${pkgs.gamescope}/bin/gamescope --adaptive-sync --force-composition --immediate-flips --rt -C 3000 -f -e -W ''${SUNSHINE_CLIENT_WIDTH} -H ''${SUNSHINE_CLIENT_HEIGHT} -r ''${SUNSHINE_CLIENT_FPS} -- ${pkgs.steam}/bin/steam -pipewire"''; prep-cmd = [ { do = ''${pkgs.bash}/bin/bash -c "${resolutionScript}/bin/resolution.sh ''${SUNSHINE_CLIENT_WIDTH} ''${SUNSHINE_CLIENT_HEIGHT}" ''${SUNSHINE_CLIENT_FPS}"''; undo = ''${pkgs.bash}/bin/bash -c "${pkgs.procps}/bin/pkill gamescope; ${pkgs.procps}/bin/pkill sunshine"''; } ]; } { name = "Heroic"; cmd = ''${pkgs.bash}/bin/bash -c "${pkgs.unstablePkgs.heroic}/bin/heroic"''; prep-cmd = [ { do = ''${pkgs.bash}/bin/bash -c "${resolutionScript}/bin/resolution.sh ''${SUNSHINE_CLIENT_WIDTH} ''${SUNSHINE_CLIENT_HEIGHT}" ''${SUNSHINE_CLIENT_FPS}"''; undo = ''${pkgs.bash}/bin/bash -c "${pkgs.procps}/bin/pkill heroic; ${pkgs.procps}/bin/pkill sunshine"''; } ]; } ]; }; }; displayManager = { autoLogin = { inherit user; enable = true; }; sddm = { enable = true; wayland.enable = false; }; }; xserver = { enable = true; videoDrivers = [ "nvidia" ]; desktopManager.xfce.enable = true; monitorSection = '' HorizSync 5.0 - 1000.0 VertRefresh 5.0 - 1000.0 Option "DPMS" ''; deviceSection = '' VendorName "NVIDIA Corporation" Option "CustomEDID" "DFP-1:/etc/X11/120edid.bin" Option "ConnectedMonitor" "DFP-1" ''; screenSection = '' Monitor "Configured Monitor" DefaultDepth 24 Option "ModeValidation" "NoVertRefreshCheck, NoHorizSyncCheck, NoMaxSizeCheck, NoMaxPClkCheck, AllowNonEdidModes, NoEdidMaxPClkCheck" Option "UseEdidfreqs" "False" Option "TripleBuffer" "False" SubSection "Display" Depth 24 EndSubSection ''; }; }; systemd.targets = { sleep.enable = false; suspend.enable = false; hibernate.enable = false; hybrid-sleep.enable = false; }; users = { users.${user} = { isNormalUser = true; home = "/home/${user}"; description = "Sunshine Server"; extraGroups = [ "wheel" "networkmanager" "input" "video" "sound" ]; openssh.authorizedKeys.keys = [ "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQC1we38/N+t8Ah5yrLof8QUwhrob7/VXFKIddaJeOVBLuDVnW7ljiAtdtEiL69D/DV4Ohmt5wMvkAAjfuHmim6FD9A6lzPbSU4KH9W2dcckszKbbI636kuDwem/xui6BW3wJa6P+0xW5ksygEAkzcK2PXuC2b4B9uwhuUdKahiGMKDxISG/WianqAe72cGMfNkYvion3Y1VsMLUdm48d2ABnxNpr7NI9B5iJ8dziOft9gpgfz13CCQRlReo75gk/4xI+vSNrQp7eR+wzJy2/dZg/T8jtyA9Q6jVxrxBpqQ1LNXkAKaJkGo9OabF6Wgpzp+YTAurL4nwR2NaJxwFuyoKvACQy0ai4jrS3206gC6JXZv8ktZMZrwUN+jPqCwfgh5qObFkAqKCxbp52ioDek2MQLdOvzQBX//DBhGEp5rzHGLZ3vhRIiiQiaof5sF5zWiYDW5mqezSPNxJPX/BrTP/Wbs/jpwTLBh3wytiia0S1WXQmya89bqzTPFiDWvTRA62EVKB/JaQtPQQOFAxWwg799DMycPeZ81xttZOyMtI/MZSddyqx2S8fWGwvToZQvuZ38mSIpFseLM1IkgabRIrAmat5SBNGGy9Dqa0eMEa7bwIY/4CMB1y6HMTnaoMXA6cnQfHMoB/zyTZ6oTXIeqeOyiZsK+RN0Mvahj8mXi7dw== giulio@giulio-X230" ]; }; groups.media.members = [ user ]; }; }