Nix flake template for Java projects, built with purr and git-hooks.nix. Part of develop-templates.
Initialize in the current directory:
nix flake init -t "github:nixcafe/develop-templates#java" --refreshOr add the registry alias once:
nix registry add beans "github:nixcafe/develop-templates"
nix flake init -t beans#javabeans is already registered if you use cattery-modules.
Or create a GitHub repo directly from this template:
gh repo create my-java-project --template nixcafe-develop/java --cloneEnter the dev shell:
nix developIf you use direnv, run direnv allow instead. .envrc runs use flake.
No flake.lock is checked into the template. The first nix develop generates one.
graalvm-ce: JDK used by Maven and Gradlemaven: overridden to use the GraalVM JDKgradle: overridden to use the GraalVM JDKnixfmt,deadnix,statix: enabled as git pre-commit hooks and available in the shellnix fmt: formats Nix files withnixfmt-tree
nix develop # open the dev shell
direnv allow # load the shell with direnv
nix fmt # format Nix files
nix flake check # run pre-commit checks
mvn --version # check Maven and Java versions
gradle --version # check Gradle and JVM versionsEdit jdk in develop/shells/default/default.nix:
jdk = pkgs.jdk21;Maven and Gradle both derive from jdk, so they switch with it.
Edit develop/checks/git-hooks/default.nix. nixfmt, deadnix, and statix are enabled by default. Uncomment google-java-format or add other hooks from git-hooks.nix:
hooks = {
nixfmt.enable = true;
deadnix.enable = true;
statix.enable = true;
google-java-format.enable = true;
};The shell hook installs active hooks whenever you enter the dev shell.
.
├── flake.nix
├── .envrc
├── .gitignore
├── statix.toml
└── develop/
├── checks/git-hooks/default.nix
└── shells/default/default.nix