Set JDK Path Programmatically in a Gradle Init Script

As mentioned in the Gradle documentation, init scripts can be used for “Defining machine-specific details (e.g., JDK locations).” So, if I understand correctly, this means that init scripts can be used to programmatically specify the JDK path (for example, based on the OS), similar to using the org.gradle.java.home parameter in the gradle.properties file.

What is the correct way to achieve this?

Thanks in advance for any guidance.

I think that point is maybe a bit misleading.
The init scripts already run in the Daemon JVM.
org.gradle.java.home would specify the Daemon JVM.
So an init script is too late to change that.
If you for example use some property to a JDK that is then used in the configuration of some tasks, you could for example use an init script to set those, or directly configure those tasks or similar.

Your use-case sounds more like you should use the JDK toolchains feature.
With that you could for example simply use system properties to define JDK locations that are not auto-discovered.
If it is really about the Java used to run Gradle, then you could use the experimental Daemon VM criteria which also uses the JDK toolchains discovery just not yet with supporting auto-provisioning.
But if you properly use the JDK toolchains feature to decouple the JVM used to run Gradle from the JVM used to build and run your production code, this should usually not be necessary.