Skip to content

Commit

Permalink
java: enable Lombok integration
Browse files Browse the repository at this point in the history
Additional JVM arguments can be specified through the
`JAVA_TOOL_OPTIONS` environment variable. For language
servers to be able to work with Lombok, the JVM must load
the `lombok.jar`. Lombok is packaged in nixpkgs, which provides
both a `java` wrapper for use with Lombok, as well as the JAR
itself on its own. This is what we care about. We can dynamically
make this accessible through the addition, whose path points to
the JAR that the Lombok package provides. Lombok is very useful, but can
be annoying to set-up when not working with a heavy IDE that abstracts
it all away. This eliminates the concern altogether, rather than having
to manually configure it for something like Neovim. It should just work
now.

It's a very lightweight addition that won't bloat. If the user
already is using the environment variable, the Lombok invocation will be
prepended rather than override the variable, hence why it is set in
`shellHook`, rather than simply as
`JAVA_TOOL_OPTIONS = "-javaagent:${pkgs.lombok}/share/java/lombok.jar"`.
The `lombok` package also has a gcroot tied to it, preventing it from
being garbage collected, just like everything else in `packages`.
  • Loading branch information
sebaszv committed Nov 6, 2024
1 parent dec0902 commit 4f1d381
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions java/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
jdk = prev."jdk${toString javaVersion}";
maven = prev.maven.override { jdk_headless = jdk; };
gradle = prev.gradle.override { java = jdk; };
lombok = prev.lombok.override { inherit jdk; };
};

devShells = forEachSupportedSystem ({ pkgs }: {
Expand All @@ -31,6 +32,15 @@
patchelf
zlib
];

shellHook =
let
loadLombok = "-javaagent:${pkgs.lombok}/share/java/lombok.jar";
prev = "\${JAVA_TOOL_OPTIONS:+ $JAVA_TOOL_OPTIONS}";
in
''
export JAVA_TOOL_OPTIONS="${loadLombok}${prev}"
'';
};
});
};
Expand Down

0 comments on commit 4f1d381

Please sign in to comment.