2020-04-24 23:36:52 +00:00
|
|
|
# This module provides JAVA_HOME, with a different way to install java
|
|
|
|
# system-wide.
|
|
|
|
|
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.programs.java;
|
|
|
|
in
|
|
|
|
{
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
|
|
|
programs.java = {
|
|
|
|
|
2024-05-15 15:35:15 +00:00
|
|
|
enable = lib.mkEnableOption "java" // {
|
2024-04-21 15:54:59 +00:00
|
|
|
description = ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Install and setup the Java development kit.
|
2022-09-09 14:08:57 +00:00
|
|
|
|
|
|
|
::: {.note}
|
|
|
|
This adds JAVA_HOME to the global environment, by sourcing the
|
|
|
|
jdk's setup-hook on shell init. It is equivalent to starting a shell
|
|
|
|
through 'nix-shell -p jdk', or roughly the following system-wide
|
|
|
|
configuration:
|
|
|
|
|
|
|
|
environment.variables.JAVA_HOME = ''${pkgs.jdk.home}/lib/openjdk;
|
|
|
|
environment.systemPackages = [ pkgs.jdk ];
|
|
|
|
:::
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2024-05-15 15:35:15 +00:00
|
|
|
package = lib.mkPackageOption pkgs "jdk" {
|
2024-01-02 11:29:13 +00:00
|
|
|
example = "jre";
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
2024-05-15 15:35:15 +00:00
|
|
|
binfmt = lib.mkEnableOption "binfmt to execute java jar's and classes";
|
2023-03-15 16:39:30 +00:00
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2024-05-15 15:35:15 +00:00
|
|
|
config = lib.mkIf cfg.enable {
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2024-05-15 15:35:15 +00:00
|
|
|
boot.binfmt.registrations = lib.mkIf cfg.binfmt {
|
2023-03-15 16:39:30 +00:00
|
|
|
java-class = {
|
|
|
|
recognitionType = "extension";
|
|
|
|
magicOrExtension = "class";
|
|
|
|
interpreter = pkgs.writeShellScript "java-class-wrapper" ''
|
|
|
|
test -e ${cfg.package}/nix-support/setup-hook && source ${cfg.package}/nix-support/setup-hook
|
|
|
|
classpath=$(dirname "$1")
|
|
|
|
class=$(basename "''${1%%.class}")
|
|
|
|
$JAVA_HOME/bin/java -classpath "$classpath" "$class" "''${@:2}"
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
java-jar = {
|
|
|
|
recognitionType = "extension";
|
|
|
|
magicOrExtension = "jar";
|
|
|
|
interpreter = pkgs.writeShellScript "java-jar-wrapper" ''
|
|
|
|
test -e ${cfg.package}/nix-support/setup-hook && source ${cfg.package}/nix-support/setup-hook
|
|
|
|
$JAVA_HOME/bin/java -jar "$@"
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
environment.systemPackages = [ cfg.package ];
|
|
|
|
|
|
|
|
environment.shellInit = ''
|
|
|
|
test -e ${cfg.package}/nix-support/setup-hook && source ${cfg.package}/nix-support/setup-hook
|
|
|
|
'';
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|