Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add toolchain detection for Nix package manager #29214

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

lorenzleutgeb
Copy link

Related to #16645.

Context

This change adds detection for JDKs installed via the Nix package manager (the Nix package repository contains numerous, see stable and unstable channels).

I've been thinking about implementing this at least since 2021-03-25, and recently there was another request for more liberal detection of JDKs in the Nix forums by @kravemir. Some downsides of this addition were mentioned by @raboof in #16645 (comment).

Status of this PR

I haven't implemented any tests and also have not changed any documentation yet. This is because I would like to get some feedback by Gradle maintainers because I spend more. In case I get positive feedback on the core changes (which are there already) I will add tests and documentation. I might need help to set up integration tests for this change.

Contributor Checklist

  • Reviewed Contribution Guidelines.
  • All commits are signed off to indicate that I agree to the terms of Developer Certificate of Origin.
  • Code can be distributed under the terms of the Apache License 2.0, and was written by myself.
  • Provide integration tests (under <subproject>/src/integTest) to verify changes from a user perspective.
  • Provide unit tests (under <subproject>/src/test) to verify logic.
  • Update User Guide, DSL Reference, and Javadoc for public-facing changes.
  • Ensure that tests pass sanity check: ./gradlew sanityCheck.
  • Ensure that tests pass locally: ./gradlew <changed-subproject>:quickTest.

Signed-off-by: Lorenz Leutgeb <lorenz@leutgeb.xyz>
Copy link

gitstream-cm bot commented May 18, 2024

⚠️ This PR appears to be lacking tests. Consider adding tests to cover the new functionality.

Copy link

gitstream-cm bot commented May 18, 2024

📊 Changes by Platform: this PR is 99% new code

3 platforms were affected

See details
Platform Added Lines % of Total Line Changes Deleted Lines % of Total Line Changes Files Changed % of Total Files Changed
jvm 145 97% 0 0% 2 50%
documentation 1 1% 1 1% 1 25%
core-runtime 2 1% 0 0% 1 25%

Copy link

@kravemir kravemir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I appreciate the initiative and efforts.

However, I'm don't agree, that implementation should try to collect every JDK it can find in the Nix store, which is a shared "cache", and this doesn't represent what user wants to have in the system or environment. Explained more in a code comment bellow.

// and the length of a hyphen (1), and only look at the suffix.
private static final int PREFIX_LENGTH = 33;

private static final Predicate<String> PATTERN = Pattern.compile("jdk", Pattern.CASE_INSENSITIVE).asPredicate();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't a reliable pattern, as some JDKs don't have jdk in the package name (path):

GRAALVM_HOME="/nix/store/hi519j33pfya2fdxpkg67qcf4pnqg2s1-graalvm-ce-22.0.0"
TEMURIN_17_HOME="/nix/store/sf0qsq60dz95mzz82sf32v6wrzp3h32l-temurin-bin-17.0.9"
TEMURIN_21_HOME="/nix/store/43wmv55gdv75i9jx8d8s2xi8mrnbcilb-temurin-bin-21.0.1"

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, it's just a heuristic. Graal and Temurin can be added to the pattern.

Comment on lines +121 to +131
final File[] list = store.listFiles(FILTER);
if (list == null) {
LOGGER.debug("Listing files within store path '{}' failed.");
return Collections.emptySet();
}

final HashSet<InstallationLocation> locations = new HashSet<InstallationLocation>(list.length);
for (final File file : list) {
locations.add(InstallationLocation.autoDetected(file, getSourceName()));
}
return locations;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure, that collecting JDKs from the Nix store is a correct approach.

IMO, the detection should be following what user wants to have in the system, which is:

  • currently running configuration (/run/current-system),
  • current nix-shell (or direnv's use nix), which could also provide JDK packages.

The Nix store contains all derivations, that were installed at some point whether they still in use or not (and not garbage collected, yet), which includes also derivations, that are not wanted for JDK detection:

  • temporary installs from experimental shells,
  • previous generations of a system with (possibly buggy and insecure) old JDK versions.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am aware:

But still there's requests for even easier integration. What I am proposing here is the most trivial integration.

Using Nix profiles is a better idea, I'll look into that.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But still there's requests for even easier integration. What I am proposing here is the most trivial integration.

Well, in this case, the trivial solution could cause harm/confusion to the user. As Nix store is a "cache" and very shared thing. This approach would make Gradle to auto-detect JVM(s), that user doesn't expect to be present ("installed") in the running system or the current nix shell.

@lorenzleutgeb
Copy link
Author

Marking this as draft until I have feedback on NixOS/nixpkgs#312887

@lorenzleutgeb lorenzleutgeb marked this pull request as draft May 19, 2024 13:04
@mlopatkin mlopatkin removed the request for review from a team May 21, 2024 06:40
@ljacomet ljacomet added in:toolchains Java Toolchains and removed to-triage labels May 21, 2024
@ljacomet
Copy link
Member

Thanks for the contribution!

Note that we have open conversations on what to do with additional detection mechanism in Gradle:

@big-guy big-guy added the 👋 team-triage Issues that need to be triaged by a specific team label May 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
from:contributor PR by an external contributor in:toolchains Java Toolchains 👋 team-triage Issues that need to be triaged by a specific team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants