1
0
Fork 1
mirror of https://github.com/NixOS/nixpkgs.git synced 2024-11-22 13:41:26 +00:00

openjdk: add derivation to generate bespoke minimal JRE's

Co-Authored-By: Robert Hensing <robert@roberthensing.nl>
This commit is contained in:
Arnout Engelen 2020-09-19 13:30:09 +02:00
parent f6ee70d3ea
commit d4efa08b53
No known key found for this signature in database
GPG key ID: 061107B0F74A6DAA
3 changed files with 45 additions and 3 deletions

View file

@ -32,7 +32,7 @@ nativeBuildInputs = [ jdk ];
</para>
<para>
If your Java package provides a program, you need to generate a wrapper script to run it using the OpenJRE. You can use <literal>makeWrapper</literal> for this:
If your Java package provides a program, you need to generate a wrapper script to run it using a JRE. You can use <literal>makeWrapper</literal> for this:
<programlisting>
nativeBuildInputs = [ makeWrapper ];
@ -43,7 +43,21 @@ installPhase =
--add-flags "-cp $out/share/java/foo.jar org.foo.Main"
'';
</programlisting>
Note the use of <literal>jre</literal>, which is the part of the OpenJDK package that contains the Java Runtime Environment. By using <literal>${jre}/bin/java</literal> instead of <literal>${jdk}/bin/java</literal>, you prevent your package from depending on the JDK at runtime.
Since the introduction of the Java Platform Module System in Java 9, Java distributions typically no longer ship with a general-purpose JRE: instead, they allow generating a JRE with only the modules required for your application(s). Because we can't predict what modules will be needed on a general-purpose system, the default <package>jre</package> package is the full JDK. When building a minimal system/image, you can override the <literal>modules</literal> parameter on <literal>jre_minimal</literal> to build a JRE with only the modules relevant for you:
<programlisting>
let
my_jre = pkgs.jre_minimal.override {
modules = [
# The modules used by 'something' and 'other' combined:
"java.base"
"java.logging"
];
};
something = (pkgs.something.override { jre = my_jre; });
other = (pkgs.other.override { jre = my_jre; });
in
...
</programlisting>
</para>
<para>

View file

@ -0,0 +1,19 @@
{ jdk
, runCommand
, patchelf
, lib
, modules ? [ "java.base" ]
}:
let
jre = runCommand "${jdk.name}-jre" {
nativeBuildInputs = [ patchelf ];
buildInputs = [ jdk ];
passthru = {
home = "${jre}";
};
} ''
jlink --module-path ${jdk}/lib/openjdk/jmods --add-modules ${lib.concatStringsSep "," modules} --output $out
patchelf --shrink-rpath $out/bin/* $out/lib/jexec $out/lib/jspawnhelper $out/lib/*.so $out/lib/*/*.so
'';
in jre

View file

@ -9267,10 +9267,19 @@ in
/* default JDK */
jdk = jdk14;
# jdk14 lacks a jre output. See https://github.com/NixOS/nixpkgs/issues/79490
# Since the introduction of the Java Platform Module System in Java 9, Java
# no longer ships a separate JRE package.
#
# If you are building a 'minimal' system/image, you are encouraged to use
# 'jre_minimal' to build a bespoke JRE containing only the modules you need.
#
# For a general-purpose system, 'jre' defaults to the full JDK:
jre = jdk14;
jre_headless = jdk14_headless;
jre_minimal = callPackage ../development/compilers/openjdk/jre.nix { };
openjdk = openjdk14;
openjdk_headless = openjdk14_headless;