Skip to content

Commit

Permalink
Set selected JDK in LaunchAgent
Browse files Browse the repository at this point in the history
  • Loading branch information
Samasaur1 committed Jul 7, 2024
1 parent 2adfb8e commit 669dfef
Showing 1 changed file with 24 additions and 20 deletions.
44 changes: 24 additions & 20 deletions modules/programs/jdks.nix
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,16 @@ in
selected = lib.mkOption {
type = with lib.types; nullOr package;
default = null;
example = "";
example = lib.literalExpression
''
pkgs.zulu11
'';
description = ''
The JDK to set as active.
Tools such as `java`, `javac`, etc. will use this JDK.
Should it also be added to the list of installed JDKs if not there?
It will not be added to the list of installed JDKs.
'';
};
};
Expand Down Expand Up @@ -90,24 +93,25 @@ in
fi
fi
done
# if a JDK is selected, `launchctl setenv` it
${lib.optionalString (cfg.selected != null) ''
# prevent variables from propagating
(
# set locale to C for stable sorting
export LC_ALL=C
for jdk in ${cfg.selected}/*.jdk; do
export JAVA_HOME="''${jdk}/Contents/Home"
# ensure that this JDK is actually a macOS JDK bundle before using it
if test -e "$JAVA_HOME"; then
launchctl setenv JAVA_HOME "$JAVA_HOME"
# break out of the loop so that we only set JAVA_HOME once, even if there are multiple JDKs in this derivation
break
fi
done
)
''}
'';

launchd.agents.java_home = lib.mkIf (cfg.selected != null) {
script = ''
export LC_ALL=C
for jdk in ${cfg.selected}/*.jdk; do
export JAVA_HOME="''${jdk}/Contents/Home"
# ensure that this JDK is actually a macOS JDK bundle before using it
if test -e "$JAVA_HOME"; then
launchctl setenv JAVA_HOME "$JAVA_HOME"
# break out of the loop so that we only set JAVA_HOME once, even if there are multiple JDKs in this derivation
break
fi
done
'';

serviceConfig = {
RunAtLoad = true;
};
};
};
}

0 comments on commit 669dfef

Please sign in to comment.