Nix flake template for Java and JVM projects. The dev shell provides GraalVM CE as the JDK, Maven, and Gradle. Maven and Gradle are configured to use the GraalVM JDK. The flake is built with purr and uses git-hooks.nix for pre-commit hooks.
Part of the develop-templates collection.
Create the project files in the current directory:
nix flake init -t "github:nixcafe/develop-templates#java" --refreshRegister the collection once to use the shorter name:
nix registry add beans "github:nixcafe/develop-templates"
nix flake init -t beans#javaWith cattery-modules, beans is already registered.
You can also create a repository from the template repository:
gh repo create my-java-project --template nixcafe-develop/java --cloneEnter the dev shell:
nix developIf direnv is installed, direnv allow loads the shell when you enter the directory. The template includes an .envrc that calls use flake.
The template does not include a flake.lock. The first nix develop resolves the inputs and writes one.
| Tool | Purpose |
|---|---|
| GraalVM CE | JDK used by Maven and Gradle |
| Maven | Build and dependency management |
| Gradle | Build and dependency management |
The dev shell has a shell hook that installs the hooks enabled in develop/checks/git-hooks/default.nix. The hooks set in this template is empty, so no hooks run until you enable some.
nix develop # enter the dev shell
direnv allow # load the shell through direnv
mvn --version # print the Maven and Java versions
gradle --version # print the Gradle and JVM versionsdevelop/shells/default/default.nix sets:
jdk = pkgs.graalvmPackages.graalvm-ce;Replace it with another JDK package from nixpkgs, for example:
jdk = pkgs.jdk23;Maven and Gradle are defined in terms of jdk, so both use the new JDK.
develop/checks/git-hooks/default.nix starts with an empty hooks set. Add entries to enable hooks:
hooks = {
nixfmt.enable = true;
deadnix.enable = true;
statix.enable = true;
};See git-hooks.nix for the available hooks. The shell hook installs enabled hooks when you enter the dev shell.
.
├── flake.nix # Flake entry point
├── .envrc # direnv: use flake
├── .gitignore
└── develop/
├── shells/default/default.nix # Dev shell (GraalVM, Maven, Gradle)
└── checks/git-hooks/default.nix # Pre-commit hook configuration