Overriding the JDK for Gradle in a Nix Flake

I’m trying to override the JDK used for Gradle in a flake that combines Gradle and GraalVM for development. It’d be nice if I could make Gradle use GraalVM, but that’s not strictly necessary – jdk21 is enough. Please let me know if there is a way though, as that would make the derivation smaller.

However, I’m having trouble getting this to work at all. Documentation on these features seems scarce and confusing and the error messages I’ve encountered have not been very helpful. When the flake’s dev shell does build successfully (using the code below), gradle -version shows JDK 17.

{
  inputs = {
    nixpkgs = {
      url = "github:nixos/nixpkgs?ref=057f9aecfb71c4437d2b27d3323df7f93c010b7e";
    };
  };

  outputs = { self, nixpkgs }:
  let pkgs = import nixpkgs {
    system = "x86_64-linux";
    config.allowUnfree = true;
    overlays = [
      (self: super: {
        gradle = super.gradle.override {
          jdk = super.jdk21;
        };
      })
    ];
  };
  in {
    devShell.x86_64-linux = pkgs.mkShell {
      buildInputs = with pkgs; [
        graalvm-ce
        gradle
      ];
    };
  };
}

The docs don’t always take into account non-NixOS environments and often don’t cover flakes. Sadly, this is an issue that involves both – flakes on an Ubuntu 22.04 system. I have NixOS on my personal laptop but don’t have enough nixperience to proceed.

In general, I don’t know how to provide arguments for nixpkgs packages. It seems like it should be far simpler than the overlay approach, but I haven’t been able to find any alternatives online. I’ve tried using gradle.override ... or gradle.overrideAttrs ... in place of gradle in buildInputs, but that only leads to errors. Any help is much appreciated.

You were close, but there’s no need for an overlay :slightly_smiling_face:

{
  description = "A very basic flake";

  inputs.nixpkgs.url = "github:nixos/nixpkgs/nixos-23.11";

  outputs = { self, nixpkgs }: {

    devShells.x86_64-linux.default = let
        pkgs = import nixpkgs { system = "x86_64-linux"; };
        # choose our preferred jdk package
        jdk = pkgs.jdk21;
    in pkgs.mkShell {
        buildInputs = with pkgs; [
          jdk
          # customise the jdk which gradle uses by default
          (callPackage gradle-packages.gradle_8 {
            java = jdk;
          })
        ];
    };
  };
}
1 Like

Thank you, that works! Could you explain why I can’t replace gradle-packages.gradle_8 with gradle? (Attempting to do so results in error: getting status of '/nix/store/zgb6w1snrx9axyhc83kyy19c5vcq1dl2-gradle-8.4/default.nix': No such file or directory)