Add an overlay in shell.nix


I have a problem adding an overlay in a mkShell derivation when other overlays are defined globally: the global defined ones are removed.

For example, I have configured the mozilla overlay to access the latest version of the rust compiler, adding it to ~/.config/nixpkgs/overlays/ . The overlay is working correctly, if added to any shell.nix or equivalent and used via nix-shell, lorry/direnv etc.

Now I would like to configure something else, and I tried to add another overlay in one shell.nix, but I noticed that if I have a file like this (there is an the empty overlay configured in overlays just to reproduce the problem) all my previous overlays (for example the mozilla ones) are no longer available

{ pkgs ? import <nixpkgs> {
  overlays =  [
    (self: super: {}) ];
with pkgs;
mkShell {
  buildInputs = [

In this case latest.rustChannels.stable.rust is not defined anymore.

It seems that the overlays attribute is not merged with the previous values, so if I have already some overlay defined, the overlays properties is overwritten and the previous overlays are lost.
I tried to ++ overlays using rec but then I have an infinite recursion; I was not able to define a second pkgs extending the first one adding just an overlay too.

Do you have any hint how to solve this problem?

btw: then I would like to use one of global overlays inside my custom overlay in shell.nix, but I think it should not be a big problem using self if the new overlay is added correctly to the overlay chain

Personally I consider usage of import <nixpkgs> {} itself a smell.

It makes it hard to reproduce the results.

You really should use niv to pin nixpkgs as well as your overlay to get a reproducible result.

hi @NobbZ,
it really depends on the use case, in this situation I’m not interested on reproducible builds: on the contrary I do not want to build using an obsolete version of nixpkgs or rust.
So I would like to apply on the fly both the overlay that is proving rust (and it is defined in ~/.config/nixpkgs/overlays) and any other overlay that I’m defining in my current derivation.

But if you transfer such a shell.nix to someone else’s PC who doesn’t use the same overlay as you, it will break.

You really should add it to your shell explicitly.

With flakes inheriting overlays from the environment will be ultimately banned anyway.

As a workaround you could probably overlays = [ your overlay ] ++ (import /home/user/.config/nixpkgs/overlays);, maybe adjusted to your actual overlay home.