Skip to content

Latest commit

 

History

History
95 lines (67 loc) · 2.35 KB

README.md

File metadata and controls

95 lines (67 loc) · 2.35 KB

Java template

Nix flake template for Java projects, built with purr and git-hooks.nix. Part of develop-templates.

Setup

Initialize in the current directory:

nix flake init -t "github:nixcafe/develop-templates#java" --refresh

Or add the registry alias once:

nix registry add beans "github:nixcafe/develop-templates"
nix flake init -t beans#java

beans 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 --clone

Enter the dev shell:

nix develop

If 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.

Tools

  • graalvm-ce: JDK used by Maven and Gradle
  • maven: overridden to use the GraalVM JDK
  • gradle: overridden to use the GraalVM JDK
  • nixfmt, deadnix, statix: enabled as git pre-commit hooks and available in the shell
  • nix fmt: formats Nix files with nixfmt-tree

Commands

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 versions

Customization

Change the JDK

Edit jdk in develop/shells/default/default.nix:

jdk = pkgs.jdk21;

Maven and Gradle both derive from jdk, so they switch with it.

Add pre-commit hooks

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.

Layout

.
├── flake.nix
├── .envrc
├── .gitignore
├── statix.toml
└── develop/
    ├── checks/git-hooks/default.nix
    └── shells/default/default.nix