How to override jdk/jre version for scala

Hi,
I am new to nixpkgs trying to install scala. though it is installed successfully scala is still using “OpenJDK 64-Bit Server VM, Java 1.8.0_202”. I want it to use latest jdk/jre, either 11 or 13 with scala.
I tried searching for a solution but didnt get anything that would work.
my config is

{pkgs, ...}:
{
  allowUnfree = true;
  packageOverrides = pkgs: with pkgs; {
    sysPackages = pkgs.buildEnv {
      name = "syspackages";
      paths = [
scala
]; };};}

could someone please help fix this.

thanks for your help :slight_smile:

You can override the JRE with: (pkgs.scala.override { jre = pkgs.jdk11; })

The way to find out about this is by looking at the nixpkgs repository: https://github.com/NixOS/nixpkgs/blob/61335d51ace64fa75daa14c58768945af255d997/pkgs/top-level/all-packages.nix#L9208 shows you that ‘scala’ is defined in https://github.com/NixOS/nixpkgs/blob/61335d51ace64fa75daa14c58768945af255d997/pkgs/development/compilers/scala/2.13.nix, and the first line of that file shows you can override the jre dependency.

Of course I don’t know what you’re planning to use scala for, but usually I would recommend not using the ‘raw’ scala compiler directly, but using a build tool such as sbt. For sbt you can override the JDK to use in the same way (https://github.com/NixOS/nixpkgs/blob/61335d51ace64fa75daa14c58768945af255d997/pkgs/development/tools/build-managers/sbt/default.nix).

1 Like

thanks for the advice, but this didnt override the jdk version.

I’m using NixOS, and I installed scala by adding it to my /etc/nixos/configuration.nix:

...
environment.systemPackages = with pkgs; [
  ...
  scala
  ...
]

When I then start ‘scala’, I get:

$ scala
Welcome to Scala 2.13.2 (OpenJDK 64-Bit Server VM, Java 1.8.0_242).

… so it’s using Java 8.

Then when I edit /etc/nixos/configuration.nix:

environment.systemPackages = with pkgs; [
  ...
  (scala.override { jre = pkgs.jdk11; })
  ...
]

… and ran sudo nixos-rebuild switch, I get:

$ scala
Welcome to Scala 2.13.2 (OpenJDK 64-Bit Server VM, Java 11.0.6-internal).

… so it looks like this works for me.

Alternatively, I can also use packageOverrides:

nixpkgs.config.packageOverrides = pkgs: rec {
    scala = pkgs.scala.override {
      jre = pkgs.jdk11;
    };
  };

environment.systemPackages = with pkgs; [
  ...
  scala
  ...
]

When I nixos-rebuild switch to that setup, it also appears to work:

$ scala
Welcome to Scala 2.13.2 (OpenJDK 64-Bit Server VM, Java 11.0.6-internal).

Can you spot the difference with what you’re doing?

1 Like

thank you very much for the detailed explanation. I learned a lot from it.

BTW it worked for me now.

1 Like