From 4402fd9f469502d43a673ef2f30f8fa9ed303618 Mon Sep 17 00:00:00 2001 From: Sam <30577766+Samasaur1@users.noreply.github.com> Date: Sat, 6 Jul 2024 17:43:12 -0700 Subject: [PATCH] Set selected JDK in LaunchAgent --- modules/programs/jdks.nix | 44 +++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/modules/programs/jdks.nix b/modules/programs/jdks.nix index 2ff878cc..0e9fc5f9 100644 --- a/modules/programs/jdks.nix +++ b/modules/programs/jdks.nix @@ -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. ''; }; }; @@ -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; + }; + }; }; }