Some applications need to discover Java installations.
NixOS already populates JAVA_HOME and adds java/javac to the PATH, which is a great start.
However, some applications want to discover not just the ‘current’ Java, but all installed/available JRE/JDK’s. Unfortunately, there’s no nice convention on how to make their JAVA_HOME’s discoverable. The result of this is that applications are resorting to terriblehacks
In the long run, I would like those terrible hacks to also discover JDK’s installed on NixOS.
Perhaps we should think about providing a not-too-terrible way to discover JRE/JDK’s in NixOS, and then add support for that approach to those tools?
One way I could imagine would be to introduce a JAVA_HOMES environment variable to hold a colon-separated list, and have each JDK package contribute its JAVA_HOME to that list (just like it contributes its bin directory to the PATH). Would that make sense?
Build on JDK8, but test that the resulting artifact also works on JDK11
Build on JDK11 but target JDK8, and also test the artifacts indeed work with JDK8
Build part of your artifact targeting JDK8, but another part targeting JDK11
I agree these are ‘niche’ use cases, and we shouldn’t support them when this comes at a prohibitive cost. But exposing them through a JAVA_HOMES environment variable seems fairly non-intrusive, and would be rather useful at least to me ;).
Not ideal, but gets the job done. I would still love to see a nice convention so we can solve this ‘upstream’ (in nix/sbt) instead of for each local build.
They expose two properties to register JDKs: org.gradle.java.installations.{fromEnv,paths} with slightly different semantics. Please consult the docs referenced above.
@raboof could use o.g.j.i.fromEnv=JAVA_8_HOME,JAVA_11_HOME.
I thought about adding an argument to gradleGen so that we could pass e.g. a list of JDKs that should be “forwarded” to Gradle. Sadly I couldn’t find a way to make that happen, because AFAIK the properties mentioned above cannot be set via environment variables (they are not in org.gradle.project.*) and I couldn’t make passing via GRADLE_OPTS work.
Then I thought, that maybe we could implement yet another detection mechanism, directly in Gradle, and ended up here. Any progress or new findings with regards to auto-detecting JDKs on top of Nix?
I would - though to be clear there’s nothing wrong with using dirty hacks when no more reliable mechanism is available - it just indicates there’s a need for a more reliable mechanism
Not really. I made an incomplete prototype of providing JAVA_x_HOME environment variables (#76699), but since I received no encouraging feedback I abandoned that.
I still like that approach (or at least I don’t see any particularly viable alternative). The main criticism seems to be it’s no “generally accepted standard”, which is fair enough - so perhaps the next step is to lobby for it to become a “generally accepted standard” outside of NixOS first .
I would love to see (have) auto-detection of JDK(s) in Gradle working also on NixOS.
Although, it’s possible to solve it via method mentioned before, it’s still a custom solution / workaround, than needs to be done manually.
However, I don’t think the solution in the PR is the best design, because it sets single environment variable per major JDK version. And, it is possible to install multiple different JDKs for the same major version. Gradle should be able to pick them all, and then select appropriate one for the project.
Gradle being able to pick up all JDKs from environment variables in a standard way - without a need to manually set custom gradle properties (and env-vars, too) - is an useful standard, IMO.
I would opt for something like this. It can store an arbitrary number of (different) JDKs.
Trying to come up with a standard, that would differentiate JDKs from each other would be a complicated thing. And, IMO, doesn’t add much value - Gradle can inspect and choose a correct JDK on its own - it needs only paths to be able to locate them.
This way, I could setup IDE to always use java17-openjdk17 and don’t care about specific version, but make sure it’s not Oracle JDK (or else I don’t want for given project) .
I doubt that this would make it into NixOS. If you have an application that you cannot patch, look into pkgs.buildFHSEnv. Gradle also registers the Java runtime that it was invoked with as a toolchain. So I’d think that just installing pkgs.jdk17 might already be sufficient. You can also just download a JDK and point Gradle at it.