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