Using an overlay in home-manager

Hello, I am trying to use an overlay in my home-manager setup. This is what I have so far:

home-manager.users.john = { config, pkgs, ... }:
  let
    unstable = import <nixos-unstable> { config = config.nixpkgs.config; };
    nur = import <nur> { inherit pkgs; };
  in {
    nixpkgs.overlays = [
      nur.repos.moaxcp.overlays.use-adoptopenjdk11
    ];
    nixpkgs.config.allowUnfree = true;
    home.packages = with pkgs; [
        ...
        nur.repos.moaxcp.micronaut-1_3_3
    ];

The overlay should upgrade the jdk used in micronaut to adoptopenjdk from my nur repo but when I check the install it is still on the normal jdk.

$ mn --version
| Micronaut Version: 1.3.3
| JVM Version: 1.8.0_222

This is the code for the overlay: https://github.com/moaxcp/nur/blob/8f6e568905592690162cc589beec9262d816f4be/overlays/use-adoptopenjdk11/default.nix

And my configuration is here: https://github.com/moaxcp/etc-nixos/blob/008b0f981058d29eddf1ed6f98f7ed97d603791b/user.nix

The overlay overrides jdk

self: super: {
    micronaut-1_3_3 = super.micronaut-1_3_3.override {
        jdk = self.adoptopenjdk-hotspot-bin-11;
    };
}

Am I doing something wrong?

1 Like

You are not installing the package from the overlay, but the regular one from the NUR, where jdk is not replaced.

Just pkgs.micronaut-1_3_3 should do.

That makes sense and I think I’m getting closer. When changed home.packages to use micronaut-1_3_3 I am now getting an error from the overlay.

# nixos-rebuild switch
building Nix...
building the system configuration...
error: attribute 'micronaut-1_3_3' missing, at /nix/store/n4cy1al11ncn3hn4fp2maw5zhldsvvvi-source/overlays/use-adoptopenjdk11/default.nix:2:23
(use '--show-trace' to show detailed location information)

How do I get the overlay to recognize the package?

usually i just pass the overlay when importing the package set:

  let
    unstable = import <nixos-unstable> { 
      config = config.nixpkgs.config; };
      overlays = [
         nur.repos.moaxcp.overlays.use-adoptopenjdk11
      ];
    };
    nur = import <nur> { inherit pkgs; };
  in {
  ...

The overlay is supposed to work on a package in the nur repository. So I tried to put the overlays in the import for it.
This is what I tried:

nur = import <nur> {                                                        
  inherit pkgs;                                                             
  overlays = [                                                              
    nur.repos.moaxcp.overlays.use-adoptopenjdk11                            
  ];                                                                        
};

I am getting a new error now.

# nixos-rebuild switch
building Nix...
building the system configuration...
error: anonymous function at /nix/store/g85jk50f1qwk97rs6pwdf3k65ks3ridj-nur/nur/default.nix:1:1 called with unexpected argument 'overlays', at /etc/nixos/user.nix:15:11
(use '--show-trace' to show detailed location information)

What I was originally trying to do is use the nixpkgs.overlays option for home manager with the overlay.

I’ve posted on Example how to use newer version of a package configured by home-manager · Issue #1107 · nix-community/home-manager · GitHub for a simpler example of using overlays.

I just wanted to follow up that I found the solution.

My problem was that I thought overlays could be applied to the nur repo. This is definitely not the case. Instead, the packages need to be added to nixpkgs first. The nur namespace cannot be used. This can be done using the overlay provided by the nur template.

I added this overlay to the nur overlays with an import so they are in a consistent location.

Now both overlays can be applied to update packages to use adoptopenjdk11.

  home-manager.users.john = { config, pkgs, ... }:
  let
    unstable = import <nixos-unstable> { config = config.nixpkgs.config; };
    nur = import <nur> {
      inherit pkgs;
    };
  in {
    nixpkgs.overlays = [
      nur.repos.moaxcp.overlays.use-moaxcp-nur-packages
      nur.repos.moaxcp.overlays.use-adoptopenjdk11 
    ];
    home.packages = with pkgs; [
      ...
      micronaut-1_3_4
      ...
    ];

Ideas for improvements

I think it would be nice to add overlays to the nur repo. Then overlays could be applied and the nur namespace can be used. The overlays can be isolated to just that repo. The idea of use-adoptopenjdk11 is to only change the jdk for the packages in the nur repo.

If use-adoptopenjdk11 can be written to detect all packages with jdk and override it automatically that would be much nicer.

1 Like

you can add a global nixpkgs overlay to replace the default jdk
anyway, there is a common way that replacing env JAVA _HOME and make effect on java base command line tools,