Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: NixOS/nixpkgs
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 7cd06d3ceba5
Choose a base ref
...
head repository: NixOS/nixpkgs
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: d24360033de6
Choose a head ref
  • 2 commits
  • 3 files changed
  • 3 contributors

Commits on Nov 4, 2020

  1. openjdk: add derivation to generate bespoke minimal JRE's

    Co-Authored-By: Robert Hensing <robert@roberthensing.nl>
    raboof and roberth committed Nov 4, 2020
    Copy the full SHA
    d4efa08 View commit details

Commits on Nov 5, 2020

  1. Merge pull request #98383 from raboof/document-jre

    openjdk: add derivation to generate bespoke minimal JRE's
    roberth authored Nov 5, 2020
    Copy the full SHA
    d243600 View commit details
Showing with 45 additions and 3 deletions.
  1. +16 −2 doc/languages-frameworks/java.xml
  2. +19 −0 pkgs/development/compilers/openjdk/jre.nix
  3. +10 −1 pkgs/top-level/all-packages.nix
18 changes: 16 additions & 2 deletions doc/languages-frameworks/java.xml
Original file line number Diff line number Diff line change
@@ -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>
19 changes: 19 additions & 0 deletions pkgs/development/compilers/openjdk/jre.nix
Original file line number Diff line number Diff line change
@@ -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
11 changes: 10 additions & 1 deletion pkgs/top-level/all-packages.nix
Original file line number Diff line number Diff line change
@@ -9384,10 +9384,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;