Skip to content

Commit 9fdd972

Browse files
author
Tristan Ross
committed
nixos/java: add binfmt option
1 parent cccb11d commit 9fdd972

1 file changed

Lines changed: 23 additions & 1 deletion

File tree

nixos/modules/programs/java.nix

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ with lib;
88
let
99
cfg = config.programs.java;
1010
in
11-
1211
{
1312

1413
options = {
@@ -40,12 +39,35 @@ in
4039
type = types.package;
4140
};
4241

42+
binfmt = mkEnableOption (lib.mdDoc "binfmt to execute java jar's and classes");
43+
4344
};
4445

4546
};
4647

4748
config = mkIf cfg.enable {
4849

50+
boot.binfmt.registrations = mkIf cfg.binfmt {
51+
java-class = {
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}"
59+
'';
60+
};
61+
java-jar = {
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 "$@"
67+
'';
68+
};
69+
};
70+
4971
environment.systemPackages = [ cfg.package ];
5072

5173
environment.shellInit = ''

0 commit comments

Comments
 (0)