1 # This module provides JAVA_HOME, with a different way to install java
4 { config, lib, pkgs, ... }:
7 cfg = config.programs.java;
15 enable = lib.mkEnableOption "java" // {
17 Install and setup the Java development kit.
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
25 environment.variables.JAVA_HOME = ''${pkgs.jdk.home}/lib/openjdk;
26 environment.systemPackages = [ pkgs.jdk ];
31 package = lib.mkPackageOption pkgs "jdk" {
35 binfmt = lib.mkEnableOption "binfmt to execute java jar's and classes";
41 config = lib.mkIf cfg.enable {
43 boot.binfmt.registrations = lib.mkIf cfg.binfmt {
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}"
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 "$@"
64 environment.systemPackages = [ cfg.package ];
66 environment.shellInit = ''
67 test -e ${cfg.package}/nix-support/setup-hook && source ${cfg.package}/nix-support/setup-hook