How to add packages to `nix-shell` via overlays

I am following the guide on https://github.com/NixOS/nixpkgs/blob/2987c1b26423c93c067a0d1152e674a0f5580109/doc/languages-frameworks/python.section.md#installing-environments-globally-on-the-system-installing-environments-globally-on-the-system

I got same config but with fewer packages to install

# ~/.config/nixpkgs/overlays/myEnv.nix
self: super: {
  myEnv = super.buildEnv {
    name = "myEnv";
    paths = [
      # A Python 3 interpreter with some packages
      (self.python3.withPackages (
        ps: with ps; [
          pytest
        ]
      ))
      self.mypy
    ];
  };
}

And here’s shell.nix

with import <nixpkgs> {
  overlays = [
    (import ~/.config/nixpkgs/overlays/myEnv.nix)
  ];
};

let
  pythonEnv = python3.withPackages (ps: [
    ps.boto3
    ps.pip
  ]);
in mkShell {
  packages = [
    pythonEnv
  ];
}

When I try to build env I get

$ nix-env -iA myEnv
error: attribute 'myEnv' in selection path 'myEnv' not found

Running nix-shell doesn’t import the modules or commands

$ nix-shell

[nix-shell:~/Documents]$ python -c 'import boto3'

[nix-shell:~/Documents]$ python -c 'import pytest'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'pytest'

[nix-shell:~/Documents]$ mypy
bash: mypy: command not found

You’re applying the overlay correctly, but:

This can never work, since myEnv and pythonEnv are two separate environments that cannot overlap. You only add myEnv to nixpkgs, and then don’t do anything with it, hence only the modules from pythonEnv are available.

The docs say as much:

One limitation of this is that you can only have 1 Python env installed globally, since they conflict on the python to load out of your PATH.

If you get a conflict or prefer to keep the setup clean, you can have nix-env atomically uninstall all other imperatively installed packages and replace your profile with just myEnv by using the --replace flag.

There may be a workaround if you dig into the guts of this, maybe you can merge two environments somehow, but it will be very hard to update the python3.withPackages contents.

I’m afraid I have no idea why nix-env can’t access your overlay, the documentation clearly says that should be possible, perhaps it’s a bug.

I was able to override a package version with

self: super:
let
  packageOverrides = py-self: py-super: {
    boto3 = py-super.boto3.overrideAttrs (oldAttrs: rec {
      pname = "boto3";
      version= "1.21.29";
      src = py-super.fetchPypi {
        pname = "boto3";
        version = "1.21.29";
        sha256 = "sha256-En699YyIJbU/Hv8RHgjEn//+sfbXpWZcmQfOgSj+FLE=";
      };
    });
  };
in
{
  python3 = super.python3.override {
    inherit packageOverrides;
  };
}

And got

$ nix-shell

[nix-shell:~/Documents]$ pip show boto3
Name: boto3
Version: 1.21.29
Summary: The AWS SDK for Python
Home-page: https://github.com/boto/boto3
Author: Amazon Web Services
Author-email: 
License: Apache License 2.0
Location: /nix/store/lpj5na29xy5qls87iin74i09gdy73ssq-python3-3.9.12-env/lib/python3.9/site-packages
Requires: botocore, jmespath, s3transfer
Required-by: 

But not sure how’d you add extra package.