diff --git a/flake.nix b/flake.nix index eb78af3..6f08a88 100644 --- a/flake.nix +++ b/flake.nix @@ -73,7 +73,7 @@ }; }; - outputs = inputs: let + outputs = inputs @ { self, ... }: let inherit (inputs.nixpkgs) lib; inherit (import ./lib/system.nix inputs) mkSystem; eachSystem = lib.genAttrs ["aarch64-darwin" "aarch64-linux" "x86_64-darwin" "x86_64-linux"]; @@ -82,6 +82,7 @@ inputs.nvim-plugins.overlays.default inputs.android-sdk.overlays.default inputs.language-servers.overlays.default + self.overlays.default ]; in { darwinConfigurations."sprite" = mkSystem { @@ -114,10 +115,23 @@ hostname = "ramune"; }; - packages = eachSystem (system: { + overlays = { + default = import ./lib/pkgs; + }; + + packages = eachSystem (system: let + pkgs = import inputs.nixpkgs { + inherit system; + overlays = [ self.overlays.default ]; + }; + in { inherit (inputs.agenix.packages.${system}) agenix; inherit (inputs.darwin.packages.${system}) darwin-rebuild; - } // (import ./lib/pkgs inputs.nixpkgs.legacyPackages.${system})); + } // { + inherit (pkgs) + steamworks-sdk-redist + systemd-transparent-udp-forwarderd; + }); apps = eachSystem (system: import ./lib/apps { inherit lib; diff --git a/lib/pkgs/default.nix b/lib/pkgs/default.nix index b4d64fb..2403e2e 100644 --- a/lib/pkgs/default.nix +++ b/lib/pkgs/default.nix @@ -1,3 +1,8 @@ -pkgs: { - yeetmouse = import ./yeetmouse.nix pkgs; +self: super: { + fetchSteam = import ./fetch-steam.nix self super; + mkSteamPackage = import ./mk-steam-package.nix self super; + mkSteamWrapper = import ./mk-steam-wrapper.nix self super; + systemd-transparent-udp-forwarderd = import ./systemd-transparent-udp-forwarderd.nix self super; + steamworks-sdk-redist = import ./steamworks-sdk-redist.nix self super; + palworld-server = import ./palworld-server.nix self super; } diff --git a/modules/games/lib/fetchSteam.nix b/lib/pkgs/fetch-steam.nix similarity index 81% rename from modules/games/lib/fetchSteam.nix rename to lib/pkgs/fetch-steam.nix index add5df1..a1a2f89 100644 --- a/modules/games/lib/fetchSteam.nix +++ b/lib/pkgs/fetch-steam.nix @@ -1,6 +1,13 @@ -{ lib, pkgs, ... }: - -with lib; makeOverridable ( +self: pkgs @ { + lib, + runCommand, + depotdownloader, + cacert, + ... +}: + +with lib; +makeOverridable ( { name ? "steamapp-${appId}-${depotId}-${manifestId}", appId, @@ -31,7 +38,7 @@ with lib; makeOverridable ( ++ optionals debug [ "-debug" ]; drvArgs = { - depsBuildBuild = [ pkgs.depotdownloader ]; + depsBuildBuild = [ depotdownloader ]; strictDeps = true; @@ -39,14 +46,14 @@ with lib; makeOverridable ( outputHashMode = "recursive"; outputHash = if hash != "" then hash else fakeHash; - env.SSL_CERT_FILE = "${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt"; + env.SSL_CERT_FILE = "${cacert}/etc/ssl/certs/ca-bundle.crt"; pos = builtins.unsafeGetAttrPos "manifestId" args; inherit passthru; } // optionalAttrs (args ? meta) { inherit meta; }; in - pkgs.runCommand name drvArgs '' + runCommand name drvArgs '' HOME=$PWD DepotDownloader -dir "$out" ${escapeShellArgs downloadArgs} rm -r "$out"/.DepotDownloader '' diff --git a/lib/pkgs/mk-steam-package.nix b/lib/pkgs/mk-steam-package.nix new file mode 100644 index 0000000..00e6b45 --- /dev/null +++ b/lib/pkgs/mk-steam-package.nix @@ -0,0 +1,56 @@ +self: pkgs @ { + lib, + stdenv, + autoPatchelfHook, + ... +}: + +with lib; +let + inherit (stdenv.hostPlatform) isAarch64; +in +{ + name, + hash ? "", + version, + appId, + depotId, + manifestId, + ... +} @ args: let + derivationArgs = builtins.removeAttrs args [ "name" "appId" "depotId" "manifestId" "hash" ]; +in +stdenv.mkDerivation (rec { + pname = name; + src = self.fetchSteam { + inherit name appId depotId manifestId hash; + }; + + dontBuild = true; + dontConfigure = true; + dontFixup = isAarch64; + + nativeBuildInputs = optionals (!isAarch64) [ autoPatchelfHook ]; + appendRunpaths = with self; makeLibraryPath [ + steamworks-sdk-redist + glibc + libxcrypt + libGL + libdrm + mesa # for libgbm + udev + libudev0-shim + libva + vulkan-loader + ]; + + installPhase = '' + runHook preInstall + + mkdir -p $out + mv ./* $out + chmod 755 -R $out + + runHook postInstall + ''; +} // derivationArgs) diff --git a/lib/pkgs/mk-steam-wrapper.nix b/lib/pkgs/mk-steam-wrapper.nix new file mode 100644 index 0000000..5710d73 --- /dev/null +++ b/lib/pkgs/mk-steam-wrapper.nix @@ -0,0 +1,74 @@ +self: pkgs @ { + lib, + box64, + makeWrapper, + pkgsCross, + stdenv, + ... +}: + +with lib; +let + useBox64 = stdenv.hostPlatform.isAarch64; + + defaultLibs = with self; [ + steamworks-sdk-redist + glibc + libxcrypt + libGL + libdrm + mesa # for libgbm + udev + libudev0-shim + libva + vulkan-loader + ]; + + defaultNativeLibs = optionals useBox64 [ pkgsCross.gnu64.libgcc ]; + + mkSteamWrapper = makeOverridable ( + { + logLevel ? 0, + env ? {}, + libs ? defaultLibs, + nativeLibs ? defaultNativeLibs, + extraWrapperArgs ? [], + }: let + runpaths = libs ++ optionals useBox64 nativeLibs; + combinedEnv = optionalAttrs useBox64 { + BOX64_LOG = logLevel; + BOX64_DYNAREC_STRONGMEM = 1; + BOX64_DYNAREC_BIGBLOCK = 1; + BOX64_DYNAREC_SAFEFLAGS = 1; + BOX64_DYNAREC_FASTROUND = 1; + BOX64_DYNAREC_FASTNAN = 1; + BOX64_DYNAREC_X87DOUBLE = 0; + } // env; + in bin: + stdenv.mkDerivation rec { + name = "box64-wrapper"; + meta.mainProgram = name; + + dontUnpack = true; + dontConfigure = true; + dontBuild = true; + + nativeBuildInputs = [ makeWrapper ]; + buildInputs = runpaths; + + installPhase = let + outBin = if useBox64 then "${box64}/bin/box64" else bin; + targetBinFlag = if useBox64 then "--add-flags ${escapeShellArg bin}" else ""; + in '' + runHook preInstall + makeWrapper "${outBin}" "$out/bin/${name}" \ + ${concatStrings (mapAttrsToList (name: value: "--set ${name} '${toString value}' ") combinedEnv)} \ + --prefix LD_LIBRARY_PATH : ${makeLibraryPath runpaths} \ + ${targetBinFlag} \ + ${lib.strings.concatStringsSep " " extraWrapperArgs} + runHook postInstall + ''; + } + ); +in + mkSteamWrapper { } diff --git a/lib/pkgs/palworld-server.nix b/lib/pkgs/palworld-server.nix new file mode 100644 index 0000000..13e7757 --- /dev/null +++ b/lib/pkgs/palworld-server.nix @@ -0,0 +1,20 @@ +self: { + lib, + ... +}: + +with lib; +self.mkSteamPackage { + name = "palworld-server"; + version = "17082920"; + appId = "2394010"; + depotId = "2394012"; + manifestId = "2423583208459052375"; + hash = "sha256-gAFEDf/rKPQ5zTH8EJ93e4KKHUGi8uiYlPS7G2lWGWk="; + meta = { + description = "Palworld Dedicated Server"; + homepage = "https://steamdb.info/app/2394010/"; + changelog = "https://store.steampowered.com/news/app/1623730?updates=true"; + sourceProvenance = with sourceTypes; [ sourceTypes.binaryNativeCode ]; + }; +} diff --git a/lib/pkgs/steamworks-sdk-redist.nix b/lib/pkgs/steamworks-sdk-redist.nix new file mode 100644 index 0000000..88c2786 --- /dev/null +++ b/lib/pkgs/steamworks-sdk-redist.nix @@ -0,0 +1,42 @@ +self: pkgs @ { + lib, + stdenv, + ... +}: + +with lib; +stdenv.mkDerivation { + pname = "steamworks-sdk-redist"; + version = "unstable-2024-05-30"; + + # Steamworks SDK Redist with steamclient.so. + # https://steamdb.info/app/1007/depots + src = self.fetchSteam { + appId = "1007"; + depotId = "1006"; + manifestId = "7138471031118904166"; + hash = "sha256-OtPI1kAx6+9G09IEr2kYchyvxlPl3rzx/ai/xEVG4oM="; + }; + + dontConfigure = true; + dontBuild = true; + + installPhase = '' + runHook preInstall + + mkdir -p $out/lib + cp linux64/steamclient.so $out/lib + chmod +x $out/lib/steamclient.so + + runHook postInstall + ''; + + meta = { + description = "Steamworks SDK Redist"; + sourceProvenance = [ sourceTypes.binaryNativeCode ]; + license = licenses.unfree; + badPlatforms = [ + { hasSharedLibraries = false; } + ]; + }; +} diff --git a/lib/pkgs/systemd-transparent-udp-forwarderd.nix b/lib/pkgs/systemd-transparent-udp-forwarderd.nix new file mode 100644 index 0000000..95a0b56 --- /dev/null +++ b/lib/pkgs/systemd-transparent-udp-forwarderd.nix @@ -0,0 +1,27 @@ +pkgs @ { + stdenv, + cmake, + pkg-config, + fetchFromGitHub, + systemdLibs, + ... +}: + +stdenv.mkDerivation { + pname = "systemd-transparent-udp-forwarderd"; + version = "0.0.1-add-activity-timeout-shutdown"; + nativeBuildInputs = [ cmake pkg-config ]; + buildInputs = [ systemdLibs ]; + src = fetchFromGitHub { + owner = "cecton"; + repo = "systemd-transparent-udp-forwarderd"; + rev = "69072ef1271f013cefa91c239455d197f68d3f8e"; + sha256 = "sha256-c4pui1aHw35Jw1pjCBgLQTHrjt5nYI2IUD4FqkyEd/M="; + }; + + installPhase = '' + runHook preInstall + install -Dm755 systemd-transparent-udp-forwarderd "$out/bin/$pname" + runHook postInstall + ''; +} diff --git a/lib/pkgs/yeetmouse.nix b/lib/pkgs/yeetmouse.nix deleted file mode 100644 index 5eea3e0..0000000 --- a/lib/pkgs/yeetmouse.nix +++ /dev/null @@ -1,65 +0,0 @@ -pkgs @ { - lib, - bash, - stdenv, - coreutils, - fetchFromGitHub, - kernel ? pkgs.linuxPackages_latest.kernel, - ... -}: - -stdenv.mkDerivation rec { - pname = "yeetmouse"; - version = "ed39ee0457d330c751b689fe6e410acae8c40d4f"; - - src = fetchFromGitHub { - owner = "andyfilter"; - repo = "yeetmouse"; - rev = version; - sha256 = "sha256-8cr4WYX09ej81tRJOfEytnPGj51voDQA9tEH91ukC+s="; - }; - - setSourceRoot = "export sourceRoot=$(pwd)/source"; - nativeBuildInputs = kernel.moduleBuildDependencies ++ [ pkgs.makeWrapper ]; - buildInputs = [ pkgs.glfw3 ]; - - makeFlags = kernel.makeFlags ++ [ - "-C" - "${kernel.dev}/lib/modules/${kernel.modDirVersion}/build" - "M=$(sourceRoot)/driver" - ]; - - postUnpack = '' - cp -r $src/gui $sourceRoot/gui - chmod 755 -R $sourceRoot/gui - substituteInPlace $sourceRoot/driver/util.c \ - --replace "#define NUM_USAGES 32" "#define NUM_USAGES 90" - ''; - - preBuild = '' - cp $sourceRoot/driver/config.sample.h $sourceRoot/driver/config.h - ''; - - LD_LIBRARY_PATH = "/run/opengl-driver/lib:${lib.makeLibraryPath buildInputs}"; - - postBuild = '' - make "-j$NIX_BUILD_CORES" -C $sourceRoot/gui "M=$sourceRoot/gui" "LIBS=-lglfw -lGL" - ''; - - postInstall = '' - install -Dm755 $sourceRoot/gui/YeetMouseGui $out/bin/yeetmouse - install -D $src/install_files/udev/99-leetmouse.rules $out/lib/udev/rules.d/98-leetmouse.rules - install -Dm755 $src/install_files/udev/leetmouse_bind $out/lib/udev/rules.d/leetmouse_bind - substituteInPlace $out/lib/udev/rules.d/98-leetmouse.rules \ - --replace "PATH='/sbin:/bin:/usr/sbin:/usr/bin'" "" - patchShebangs $out/lib/udev/rules.d/leetmouse_bind - wrapProgram $out/lib/udev/rules.d/leetmouse_bind \ - --prefix PATH : ${lib.makeBinPath [ bash coreutils ]} - substituteInPlace $out/lib/udev/rules.d/98-leetmouse.rules \ - --replace "leetmouse_bind" "$out/lib/udev/rules.d/leetmouse_bind" - ''; - - buildFlags = [ "modules" ]; - installFlags = [ "INSTALL_MOD_PATH=${placeholder "out"}" ]; - installTargets = [ "modules_install" ]; -} diff --git a/modules/games/lib/mkSteamPackage.nix b/modules/games/lib/mkSteamPackage.nix deleted file mode 100644 index 04666eb..0000000 --- a/modules/games/lib/mkSteamPackage.nix +++ /dev/null @@ -1,54 +0,0 @@ -{ lib, helpers, pkgs, ... } @ inputs: - -with lib; -let - fetchSteam = (import ./fetchSteam.nix) inputs; - inherit ((import ./steamworks.nix) inputs) steamworks-sdk-redist; -in -{ - name, - hash ? "", - version, - appId, - depotId, - manifestId, - ... -} @ args: let - mkDerivationArgs = builtins.removeAttrs args [ "name" "appId" "depotId" "manifestId" "hash" ]; -in pkgs.stdenv.mkDerivation (rec { - pname = name; - src = fetchSteam { - inherit name appId depotId manifestId hash; - }; - - dontBuild = true; - dontConfigure = true; - dontFixup = helpers.system == "aarch64-linux"; - - appendRunpaths = with pkgs; makeLibraryPath [ - steamworks-sdk-redist - glibc - libxcrypt - libGL - libdrm - mesa # for libgbm - udev - libudev0-shim - libva - vulkan-loader - ]; - - nativeBuildInputs = optionals (helpers.system == "aarch64-linux") [ - pkgs.autoPatchelfHook - ]; - - installPhase = '' - runHook preInstall - - mkdir -p $out - mv ./* $out - chmod 755 -R $out - - runHook postInstall - ''; - } // mkDerivationArgs) diff --git a/modules/games/lib/mkWrappedBox64.nix b/modules/games/lib/mkWrappedBox64.nix deleted file mode 100644 index baedf2e..0000000 --- a/modules/games/lib/mkWrappedBox64.nix +++ /dev/null @@ -1,65 +0,0 @@ -{ lib, helpers, pkgs, ... } @ inputs: - -with lib; -let - inherit ((import ./steamworks.nix) inputs) steamworks-sdk-redist; - - defaultLibs = with pkgs; [ - steamworks-sdk-redist - glibc - libxcrypt - libGL - libdrm - mesa # for libgbm - udev - libudev0-shim - libva - vulkan-loader - ]; - - defaultNativeLibs = [ pkgs.pkgsCross.gnu64.libgcc ]; - - makeWrappedBox64 = { - logLevel ? 0, - env ? {}, - libs ? defaultLibs, - nativeLibs ? defaultNativeLibs, - extraWrapperArgs ? [], - }: let - box64Bin = "${pkgs.box64}/bin/box64"; - runpaths = libs ++ nativeLibs; - combinedEnv = { - BOX64_DYNAREC_STRONGMEM = 1; - BOX64_DYNAREC_BIGBLOCK = 1; - BOX64_DYNAREC_SAFEFLAGS = 1; - BOX64_DYNAREC_FASTROUND = 1; - BOX64_DYNAREC_FASTNAN = 1; - BOX64_DYNAREC_X87DOUBLE = 0; - } // env; - in bin: - pkgs.stdenv.mkDerivation rec { - name = "box64-wrapped"; - - dontUnpack = true; - dontConfigure = true; - dontBuild = true; - - nativeBuildInputs = [ pkgs.makeWrapper ]; - buildInputs = runpaths ++ libs; - - installPhase = '' - runHook preInstall - makeWrapper "${box64Bin}" "$out/bin/box64" \ - ${concatStrings (mapAttrsToList (name: value: "--set ${name} '${toString value}' ") env)} \ - --set BOX64_LOG "${toString logLevel}" \ - --prefix LD_LIBRARY_PATH : ${lib.makeLibraryPath buildInputs} \ - ${lib.strings.concatStringsSep " " extraWrapperArgs} \ - --add-flags "$bin" - runHook postInstall - ''; - }; - - box64 = makeWrappedBox64 { }; - identity = bin: bin; -in - if helpers.system == "aarch64-linux" then box64 else identity diff --git a/modules/games/lib/serverScripts.nix b/modules/games/lib/scripts.nix similarity index 100% rename from modules/games/lib/serverScripts.nix rename to modules/games/lib/scripts.nix diff --git a/modules/games/lib/steamworks.nix b/modules/games/lib/steamworks.nix deleted file mode 100644 index 85fc861..0000000 --- a/modules/games/lib/steamworks.nix +++ /dev/null @@ -1,42 +0,0 @@ -{ lib, pkgs, ... } @ inputs: - -with lib; -let - fetchSteam = (import ./fetchSteam.nix) inputs; -in { - steamworks-sdk-redist = pkgs.stdenv.mkDerivation { - pname = "steamworks-sdk-redist"; - version = "unstable-2024-05-30"; - - # Steamworks SDK Redist with steamclient.so. - # https://steamdb.info/app/1007/depots - src = fetchSteam { - appId = "1007"; - depotId = "1006"; - manifestId = "7138471031118904166"; - hash = "sha256-OtPI1kAx6+9G09IEr2kYchyvxlPl3rzx/ai/xEVG4oM="; - }; - - dontConfigure = true; - dontBuild = true; - - installPhase = '' - runHook preInstall - - mkdir -p $out/lib - cp linux64/steamclient.so $out/lib - chmod +x $out/lib/steamclient.so - - runHook postInstall - ''; - - meta = { - description = "Steamworks SDK Redist"; - sourceProvenance = [ sourceTypes.binaryNativeCode ]; - license = licenses.unfree; - badPlatforms = [ - { hasSharedLibraries = false; } - ]; - }; - }; -} diff --git a/modules/games/palworld.nix b/modules/games/palworld.nix index 0c8cca1..b547249 100644 --- a/modules/games/palworld.nix +++ b/modules/games/palworld.nix @@ -7,10 +7,7 @@ let cfg = config.modules.games.palworld; name = "palworld-server"; - serverScripts = (import ./lib/serverScripts.nix) args; - mkWrappedBox64 = (import ./lib/mkWrappedBox64.nix) args; - mkSteamPackage = (import ./lib/mkSteamPackage.nix) args; - inherit ((import ./lib/steamworks.nix) args) steamworks-sdk-redist; + scripts = (import ./lib/scripts.nix) args; generateSettings = name: value: let optionSettings = @@ -34,21 +31,6 @@ let OptionSettings=(${concatStringsSep "," optionSettings}) ''; - palworld-server = mkSteamPackage { - name = "palworld-server"; - version = "17082920"; - appId = "2394010"; - depotId = "2394012"; - manifestId = "2423583208459052375"; - hash = "sha256-gAFEDf/rKPQ5zTH8EJ93e4KKHUGi8uiYlPS7G2lWGWk="; - meta = { - description = "Palworld Dedicated Server"; - homepage = "https://steamdb.info/app/2394010/"; - changelog = "https://store.steampowered.com/news/app/1623730?updates=true"; - sourceProvenance = with sourceTypes; [ sourceTypes.binaryNativeCode ]; - }; - }; - baseSettings = { ServerName = "Unnamed Server"; AllowConnectPlatform = "Steam"; @@ -157,7 +139,7 @@ in package = mkOption { type = types.package; - default = palworld-server; + default = pkgs.palworld-server; }; public = mkOption { @@ -219,7 +201,7 @@ in files = let settings = baseSettings // cfg.settings; in { - "Pal/Binaries/Linux/steamclient.so" = "${steamworks-sdk-redist}/lib/steamclient.so"; + "Pal/Binaries/Linux/steamclient.so" = "${pkgs.steamworks-sdk-redist}/lib/steamclient.so"; "Pal/Saved/Config/LinuxServer/PalWorldSettings.ini" = generateSettings "PalWorldSettings.ini" settings; "Pal/Saved/Config/LinuxServer/Engine.ini" = builtins.toFile "Engine.ini" engineSettings; }; @@ -236,7 +218,7 @@ in ] ++ optionals (cfg.ip != null) [ "-publicip=${cfg.ip}" ] ++ optionals cfg.public [ "-publiclobby" ]; - bin = getBin (mkWrappedBox64 "${cfg.datadir}/Pal/Binaries/Linux/PalServer-Linux-Shipping"); + bin = getExe (pkgs.mkSteamWrapper "${cfg.datadir}/Pal/Binaries/Linux/PalServer-Linux-Shipping"); in "${bin} ${concatStringsSep " " args}"; in { wantedBy = mkIf cfg.autostart [ "multi-user.target" ]; @@ -245,8 +227,8 @@ in inherit script; preStart = '' - ${serverScripts.mkDirs name dirs} - ${serverScripts.mkFiles name files} + ${scripts.mkDirs name dirs} + ${scripts.mkFiles name files} ''; serviceConfig = {