1 # This module provides JAVA_HOME, with a different way to install java
4 { config, lib, pkgs, ... }:
9 cfg = config.programs.java;
17 enable = mkEnableOption (lib.mdDoc "java") // {
18 description = lib.mdDoc ''
19 Install and setup the Java development kit.
22 This adds JAVA_HOME to the global environment, by sourcing the
23 jdk's setup-hook on shell init. It is equivalent to starting a shell
24 through 'nix-shell -p jdk', or roughly the following system-wide
27 environment.variables.JAVA_HOME = ''${pkgs.jdk.home}/lib/openjdk;
28 environment.systemPackages = [ pkgs.jdk ];
35 defaultText = literalExpression "pkgs.jdk";
36 description = lib.mdDoc ''
37 Java package to install. Typical values are pkgs.jdk or pkgs.jre.
42 binfmt = mkEnableOption (lib.mdDoc "binfmt to execute java jar's and classes");
48 config = mkIf cfg.enable {
50 boot.binfmt.registrations = mkIf cfg.binfmt {
52 recognitionType = "extension";
53 magicOrExtension = "class";
54 interpreter = pkgs.writeShellScript "java-class-wrapper" ''
55 test -e ${cfg.package}/nix-support/setup-hook && source ${cfg.package}/nix-support/setup-hook
56 classpath=$(dirname "$1")
57 class=$(basename "''${1%%.class}")
58 $JAVA_HOME/bin/java -classpath "$classpath" "$class" "''${@:2}"
62 recognitionType = "extension";
63 magicOrExtension = "jar";
64 interpreter = pkgs.writeShellScript "java-jar-wrapper" ''
65 test -e ${cfg.package}/nix-support/setup-hook && source ${cfg.package}/nix-support/setup-hook
66 $JAVA_HOME/bin/java -jar "$@"
71 environment.systemPackages = [ cfg.package ];
73 environment.shellInit = ''
74 test -e ${cfg.package}/nix-support/setup-hook && source ${cfg.package}/nix-support/setup-hook