Overlays: respect upstream's nixpkgs flake.lock

Hello,

I’m trying to adapt a flake that exports a package that I’m installing directly in a flake to additionally export the package + an overlay of that package, so I can use that overlay in my configuration and install it that way. I know the final outcome is the same, I’m learning nix.

I already modified the input flake (a neovim override) to export the overlay correctly but I’m having trouble with the nixpkgs revision used when I import that overlay in my configuration.

Now I have something like this:

{
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixos-22.11";
    neovim-flake.url = "github:aorith/neovim-flake";
  };
  outputs = inputs: {
      nixosConfigurations.trantor = inputs.nixpkgs.lib.nixosSystem {
          ...
          modules = [
            ({pkgs, ...}: {nixpkgs.overlays = [overlay-unstable];})
            ({inputs, ...}: {environment.systemPackages = [inputs.neovim-flake.packages.${system}.default];})
            ./machines/trantor
          ];
      };
  };
}

I want to use the overlay instead:

{
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixos-22.11";
    neovim-flake.url = "github:aorith/neovim-flake";
  };
  outputs = inputs: {
      nixosConfigurations.trantor = inputs.nixpkgs.lib.nixosSystem {
          ...
          modules = [
            ({pkgs, ...}: {nixpkgs.overlays = [overlay-unstable inputs.neovim-flake.overlays.default];})
            ./machines/trantor  # adds 'pkgs.aorith.neovim' in environment.systemPackages
          ];
      };
  };
}

This “works” but the neovim overlay then uses the nixpkgs version from this flake instead of its own.

This causes the neovim flake to build with a nixpkgs which is pinned to some revision from 22.11 instead of the revision from its repository that comes from unstable (also, the build fails because I use a package that only exists in unstable).

I guess that this is expected and makes external overlays behave correctly, but, can I instead use the ‘flake.lock’ from the other repository somehow? I don’t known how to override the ‘pkgs’ argument in a module.

That would allow to have a consistent package between different systems, if it’s not possible then I’ll go back to install the package directly.

References:

Try this:

{
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixos-22.11";
    neovim-flake.url = "github:aorith/neovim-flake";
    neovim-flake.inputs.nixpkgs.follows = "nixpkgs";
  };

Thanks! But that’s actually the opposite of what I want!

I think I found the solution, I needed to fix this in the neovim-flake flake.

Before:

      overlays.default = final: prev: let
        nvimCfg = final.callPackage ./config {pkgs = final.extend inputs.self.overlays.vimPlugins;};
      in {
        # finally use callPackage to create the neovim derivation
        aorith.neovim = final.callPackage ./packages/neovim {inherit inputs nvimCfg;};
      };

And now:

      overlays.default = final: prev: let
        pkgs = import inputs.nixpkgs {
          system = prev.system;
          overlays = [inputs.self.overlays.vimPlugins];
        };
        nvimCfg = final.callPackage ./config {inherit pkgs;};
      in {
        aorith.neovim = final.callPackage ./packages/neovim {inherit pkgs inputs nvimCfg;};
      };

So basically overriding pkgs by importing it (and by the way not using .extend which seems to be discouraged).

Now the package can be used in an overlay respecting the nixpkgs version that is bundled in neovim-flake (and actually now I can add inputs.nixpkgs.follows = "nixpkgs"; if I would like to override the neovim-flake nixpkgs input, which I don’t want to currently).

I’ve learned a thing or two about overlays today :slight_smile:

Why do you want to use its overlay? If it’s just to get the package in pkgs rather than having to refer to inputs.neovim-flake.packages, then you don’t want to use the flake’s overlay at all. You can just do something like this:

nixpkgs.overlays = [(final: prev: {
  aorith.neovim = inputs.neovim-flake.packages.${system}.default;
})];

i.e. Overlays don’t work the way you want by design, but you can still take a package that has nothing to do with the nixpkgs being given to the overlay and stuff it in like this.

Understood! Many thanks for the help.
It was just a way to learn since that’s my first flake that isn’t used to configure NixOS.