Howto: Automatically run minecraft on the dedicated GPU

Running Minecraft on the dedicated GPU requires launching the internal java command with primusrun prefixed to it. (this command may depend on your setup, alternatives include optirun, nvidia-offload or prime-offload).

Just running primusrun minecraft-client doesn’t seem to work.

It is possible to get this working by setting a custom java runtime command inside the minecraft client, but it requires hard-linking to the package, which is a bit annoying to set-up and maintain.

Instead of doing that I have created an overlay for the Minecraft Package in which I switch out the java runtime and wrap the java command with one that executes the required script first.

This is what I have come up with:

  primecraft = pkgs.minecraft.override rec {
    jre = pkgs.jre.overrideAttrs (old@{installPhase ? "", ...}: {
      nativeBuildInputs = old.nativeBuildInputs ++ [ pkgs.makeWrapper pkgs.primus ];
      installPhase = installPhase + ''
        mv $out/bin/java $out/bin/.java-wrapped
        echo "#! ${pkgs.bash}/bin/bash" >> $out/bin/java
        echo "script_dir=\$(dirname \$(readlink --canonicalize-existing \$0))" >> $out/bin/java
        echo "export vblank_mode=0" >> $out/bin/java
        echo 'primusrun $script_dir/.java-wrapped "$@"' >> $out/bin/java
        chmod +x $out/bin/java
      '';
    });
  };

This currently requires rebuilding the entire java runtime which takes quite a bit of time. Luckily I have a build machine so I don’t really need to worry about that. That being said, it’s still not ideal but the only solution I can think of would require copying the entire package to a new one and I am not sure how to do that properly.

1 Like