Setup JavaFX on NixOS
Setup JavaFX on NixOS
December 11, 2025
Dev Shell
Refer to NixOS Wiki, to enable JavaFX support, an override is required:
jdkWithFX = pkgs.openjdk.override {
enableJavaFX = true;
};The minimal flake.nix looks like:
{
description = "Devshell for JavaFX.";
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
outputs = {
self,
nixpkgs,
}: let
system = "x86_64-linux";
pkgs = import nixpkgs {inherit system;};
jdkWithFX = pkgs.openjdk.override {
enableJavaFX = true;
};
in {
devShells.${system}.default = pkgs.mkShell {
buildInputs = [ jdkWithFX ];
};
};
}After creating this file, run nix develop to enter the dev shell.
Setup LSP in nixvim
Firstly, we need to enable nvim-lspconfig in nixvim as a framework for LSP.
plugins.lsp.enable = true;Then, I choose to use jdtls as LSP of Java. Add the following code to enable it in nixvim:
plugins.lsp.servers.jdtls.enable = true;Now, try to edit a Java file in the dev shell environment, but jdtls can not find JavaFX. To solve this issue, I use Gradle.
In the same level directory of flake.nix, create a file named build.gradle, then add the following content:
plugins {
id 'application'
id 'org.openjfx.javafxplugin' version '0.1.0'
}
repositories {
mavenCentral()
}
javafx {
version = "21"
modules = [ 'javafx.controls', 'javafx.fxml' ]
}Please ensure the
versionofjavafxis compatible with the JDK.
Finally, restart the LSP, jdtls will find JavaFX and provide code completions.
References
Last updated on