Java: Difference between revisions
SnowSquire (talk | contribs) updated package names and versions |
→jdk11: alternatives to overriding cacert for jdk11+ |
||
(13 intermediate revisions by 7 users not shown) | |||
Line 9: | Line 9: | ||
== JDK options == | == JDK options == | ||
Your default choice should probably be to install <code>jdk</code>, which is an alias the latest LTS. If you're in a server environment, go for <code>jdk21_headless</code>. Java 21 is the currently-maintained LTS version of OpenJDK as of April 2024. | Your default choice should probably be to install <code>jdk</code>, which is an alias to the latest [https://en.wikipedia.org/wiki/Long-term_support LTS]. If you're in a server environment, go for <code>jdk21_headless</code>. Java 21 is the currently-maintained LTS version of OpenJDK as of April 2024. | ||
As you might expect, though, many flavors of Java are available in NixOS. | As you might expect, though, many flavors of Java are available in NixOS. | ||
Line 22: | Line 22: | ||
* JetBrains JDK (<code>jetbrains.jdk</code>), a fork of OpenJDK with modifications made by JetBrains | * JetBrains JDK (<code>jetbrains.jdk</code>), a fork of OpenJDK with modifications made by JetBrains | ||
* Oracle's JDK (<code>oraclejdk</code>), only version 8 is available. | * Oracle's JDK (<code>oraclejdk</code>), only version 8 is available. | ||
== VSCode + Language Support for Java (TM) by Red Hat extension == | |||
Unfortunately the extension contains and uses a version of the JRE which makes use of dynamically loaded libraries, which nix cannot accomodate out-of-the-box. Fortunately there's a simple solution in the use of [https://github.com/Mic92/nix-ld nix-ld]. Here's a simple <code>flake.nix</code> snippet to get you started (I'll focus on the <code>devShell</code> part for brevity): | |||
<syntaxhighlight lang="nix" line highlight="6,10" copy> | |||
# flake.nix | |||
devShell = pkgs.mkShell { | |||
buildInputs = [ | |||
pkgs.gradle | |||
pkgs.jdk17 | |||
]; | |||
NIX_LD_LIBRARY_PATH = pkgs.lib.makeLibraryPath [ | |||
pkgs.stdenv.cc.cc | |||
pkgs.openssl | |||
]; | |||
NIX_LD = pkgs.lib.fileContents "${pkgs.stdenv.cc}/nix-support/dynamic-linker"; | |||
# ^--- when using direnv, this line will require the 'use flake --impure' option. | |||
}; | |||
</syntaxhighlight> | |||
The important lines above are the two starting with <code>NIX_LD...</code>. They will let nix-ld wrap the required, dynamically loaded libraries so that they are found when building the <code>devShell</code>. | |||
Another solution is to use the <code>[https://github.com/redhat-developer/vscode-java?tab=readme-ov-file#supported-vs-code-settings java.jdt.ls.java.home]</code> VSCode setting to point to a nix-built Java 17. For example, using home-manager's settings: <syntaxhighlight lang="nix"> | |||
programs.vscode.enable = true; | |||
programs.vscode.extensions = [ pkgs.vscode-extensions.redhat.java ]; | |||
programs.vscode.userSettings = { | |||
"java.jdt.ls.java.home" = "${pkgs.jdk17}"; | |||
}; | |||
</syntaxhighlight>Note that this will still result in the extension downloading its own JRE, it just will not be used. | |||
== Using Oracle JDK instead of Open JDK == | == Using Oracle JDK instead of Open JDK == | ||
Almost all Java packages in nixpkgs use Open JDK in form of a '''jre''' dependency. If you use Oracle JDK and also want other applications to use it, you can simply tweak your <code> | Almost all Java packages in nixpkgs use Open JDK in form of a '''jre''' dependency. If you use Oracle JDK and also want other applications to use it, you can simply tweak your <code>nixpkgs/config.nix</code> so that your desired application uses Oracles JDK or JRE. | ||
Example with UMLet with JRE | Example with UMLet with JRE | ||
Line 39: | Line 70: | ||
To install the Oracle JRE system-wide, you need to explicitly accept the license in addition to allowing unfree modules: | To install the Oracle JRE system-wide, you need to explicitly accept the license in addition to allowing unfree modules: | ||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
# /etc/nixos/configuration.nix | # /etc/nixos/configuration.nix | ||
Line 47: | Line 79: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Working with | Working with <code>requireFile</code> (manual downloading the tarballs and manual adding in to the nix store) might be annoying and nixops-unfriendly, so it can be overridden in overlays | ||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
Line 76: | Line 108: | ||
== Better font rendering== | == Better font rendering== | ||
By default java does not enable | By default java does not enable anti-aliasing for font rendering. By exporting environment variables, this can be fixed: | ||
<syntaxhighlight lang="console"> | <syntaxhighlight lang="console"> | ||
Line 85: | Line 117: | ||
== Overriding java jks Certificate Store == | == Overriding java jks Certificate Store == | ||
Overriding the java certificate store may be required for adding your own Root certificates in case your company uses an internal PKI or the company utilizes an intercepting proxy. | Overriding the java certificate store may be required for adding your own Root certificates in case your company uses an internal PKI or the company utilizes an intercepting proxy. | ||
=== jdk8 === | === jdk8 === | ||
Overriding the jdk8 certificate store is possible by overriding the '''cacert''' parameter of the package: | Overriding the jdk8 certificate store is possible by overriding the '''cacert''' parameter of the package: | ||
Line 115: | Line 150: | ||
=== jdk11 === | === jdk11 === | ||
JDK11 does not provide the cacert overridable and therefore it is not possible to use the same technique to override the truststore. | |||
JDK11 does not provide the cacert overridable and therefore it is not possible to use the same technique to override the truststore. | |||
As an alternative solution you can either set an environment variable, <code>JAVAX_NET_SSL_TRUSTSTORE</code>, or pass an argument to your program, <code>-Djavax.net.ssl.trustStore</code>, with the location of your cacert. See [https://discourse.nixos.org/t/custom-ssl-certificates-for-jdk/18297/9 discussion]. | |||
== Building and Packaging == | == Building and Packaging == | ||
See the [https://nixos.org/manual/nixpkgs/#sec-language-java Java section in the Nixpkgs manual]. | |||
=== Maven === | === Maven === | ||
Line 127: | Line 165: | ||
<pre>mvn verify</pre> | <pre>mvn verify</pre> | ||
[https://github.com/fzakaria/mvn2nix mvn2nix] | [https://github.com/fzakaria/mvn2nix mvn2nix], [https://nixos.org/manual/nixpkgs/stable/#maven-buildmavenpackage buildMavenPackage] (recommended) can be used to build Maven projects with Nix | ||
See also: [https://fzakaria.com/2020/07/20/packaging-a-maven-application-with-nix.html Packaging a Maven application with Nix] | See also: [https://fzakaria.com/2020/07/20/packaging-a-maven-application-with-nix.html Packaging a Maven application with Nix] and [https://github.com/NixOS/nixpkgs/blob/master/pkgs/by-name/ma/maven/build-maven-package.nix buildMavenPackage source] | ||
=== Ant === | === Ant === | ||
Line 149: | Line 187: | ||
To fetch ivy sources in a fixed-output-derivation, see for example [https://github.com/milahu/nur-packages/blob/master/pkgs/yacy/yacy.nix yacy.nix] | To fetch ivy sources in a fixed-output-derivation, see for example [https://github.com/milahu/nur-packages/blob/master/pkgs/yacy/yacy.nix yacy.nix] | ||
== JavaFX and Webkit support == | |||
To include support for JavaFX and Webkit, use the <code>enableJavaFX</code> and <code>withWebKit</code> options: | |||
<syntaxHighlight lang=nix> | |||
{ pkgs, ... }: | |||
let | |||
jdkWithFX = pkgs.openjdk.override { | |||
enableJavaFX = true; # for JavaFX | |||
# include following line if JavaFX with Webkit is needed | |||
openjfx_jdk = pkgs.openjfx.override { withWebKit = true; }; | |||
}; | |||
in | |||
... | |||
</syntaxHighlight> | |||
== Further reading == | |||
{{Nixpkgs manual|sec-language-java}}<br /><br /><br /><br /><br />{{Wikipedia|Java_(programming_language)}} | |||
[[Category:Languages]] | [[Category:Languages]] |