Adding the JDK to vscode

I’d like to use vscode on NixOS with the ‘metals’ extension, which requires a JDK.

I was already successful in loading the Metals extension with:

environment.systemPackages = with pkgs; [
    ...
    (vscode-with-extensions.override {
      vscodeExtensions = with vscode-extensions; vscode-utils.extensionsFromVscodeMarketplace [
        {
          name = "metals";
          publisher = "scalameta";
          version = "1.5.7";
          sha256 = "0n99gw1imnm2hbizxrwnbrk8sxz5ysbzll4hn3jqy1xv3ky1pkix";
        }
        {
          name = "scala";
          publisher = "scala-lang";
          version = "0.3.8";
          sha256 = "17dl10m3ayf57sqgil4mr9fjdm7i8gb5clrs227b768pp2d39ll9";
        }
      ];
    })
    ...
];

However, I’m running into trouble adding the JDK. I guess I want to do 2 things: register the JDK as a dependency, and also setting the JAVA_HOME environment variable so that the metals extension can actually find it.

So far I tried:

environment.systemPackages = with pkgs; [
    ...
    (vscode-with-extensions.override {
      vscode = vscode.overrideAttrs (oldAttrs: rec {
        buildInputs = oldAttrs.buildInputs ++ [ jdk ];
        JAVA_HOME = "${jdk}";
      });
      vscodeExtensions = with vscode-extensions; vscode-utils.extensionsFromVscodeMarketplace [
        {
          name = "metals";
          publisher = "scalameta";
          version = "1.5.7";
          sha256 = "0n99gw1imnm2hbizxrwnbrk8sxz5ysbzll4hn3jqy1xv3ky1pkix";
        }
        {
          name = "scala";
          publisher = "scala-lang";
          version = "0.3.8";
          sha256 = "17dl10m3ayf57sqgil4mr9fjdm7i8gb5clrs227b768pp2d39ll9";
        }
      ];
    })
    ...
];

In /nix/store/3ci6gvxflqh36d00kpv84rvhahsg1q5y-vscode-1.38.1.drv I do see

("JAVA_HOME","/nix/store/hna1lkls18hhr9hg2nrc0sgdjmxbv9cs-openjdk-8u222-ga")

but when I actually run code I don’t see it in the environment.

What could I be missing?

Try setting metals.javaHome to /run/current-system/sw/lib/openjdk if you have the jdk in environment.systemPackages.

I wasn’t aware of that, thanks

Wait, how/where do I set that? I’m rather new to NixOS I’m afraid.

metals.javaHome is a setting for the extension in vscode.

Shouldn’t be be setting it to pkgs.jdk? run/current-system/sw/lib/openjdk seems non functional

Then you’ll have to update it everytime it’s garbage collected. I generate settings.json with builtins.toJson through home-manager to circumvent that [1].

[1] https://git.sr.ht/~eadwu/nixos-configuration/tree/master/modules/home-manager/code.nix