1# This module provides JAVA_HOME, with a different way to install java
2# system-wide.
3
4{
5 config,
6 lib,
7 pkgs,
8 ...
9}:
10
11let
12 cfg = config.programs.java;
13in
14{
15
16 options = {
17
18 programs.java = {
19
20 enable = lib.mkEnableOption "java" // {
21 description = ''
22 Install and setup the Java development kit.
23
24 ::: {.note}
25 This adds JAVA_HOME to the global environment, by sourcing the
26 jdk's setup-hook on shell init. It is equivalent to starting a shell
27 through 'nix-shell -p jdk', or roughly the following system-wide
28 configuration:
29
30 environment.variables.JAVA_HOME = ''${pkgs.jdk.home}/lib/openjdk;
31 environment.systemPackages = [ pkgs.jdk ];
32 :::
33 '';
34 };
35
36 package = lib.mkPackageOption pkgs "jdk" { example = "jre"; };
37
38 binfmt = lib.mkEnableOption "binfmt to execute java jar's and classes";
39
40 };
41
42 };
43
44 config = lib.mkIf cfg.enable {
45
46 boot.binfmt.registrations = lib.mkIf cfg.binfmt {
47 java-class = {
48 recognitionType = "extension";
49 magicOrExtension = "class";
50 interpreter = pkgs.writeShellScript "java-class-wrapper" ''
51 test -e ${cfg.package}/nix-support/setup-hook && source ${cfg.package}/nix-support/setup-hook
52 classpath=$(dirname "$1")
53 class=$(basename "''${1%%.class}")
54 $JAVA_HOME/bin/java -classpath "$classpath" "$class" "''${@:2}"
55 '';
56 };
57 java-jar = {
58 recognitionType = "extension";
59 magicOrExtension = "jar";
60 interpreter = pkgs.writeShellScript "java-jar-wrapper" ''
61 test -e ${cfg.package}/nix-support/setup-hook && source ${cfg.package}/nix-support/setup-hook
62 $JAVA_HOME/bin/java -jar "$@"
63 '';
64 };
65 };
66
67 environment.systemPackages = [ cfg.package ];
68
69 environment.shellInit = ''
70 test -e ${cfg.package}/nix-support/setup-hook && . ${cfg.package}/nix-support/setup-hook
71 '';
72
73 };
74
75}