How to use overlays to override version in a `shell.nix`? (e.g., anki)

I found a couple threads (this, this, this, and this) in this topic, but none of the advice in them seemed applicable. (Then again, I could be wrong.)

I did an attempt to try out overlays after reading the nixos.wiki on Overlays and taking this Anki example, but no joy, and I have inconsistent results after trying things out (probably because of my ignorance).

Here is anki with a simple nix-shell -p:

$ nix-shell -p anki --run "anki --help"
...
Anki 2.1.15
...

My initial shell.nix:

let
  ankiOverlay =
    final: prev:
    {
      anki = prev.anki.overrideAttrs (
            oldAttrs:
            rec {
              version =
                "2.1.0";
                # "2.1.15";
              pname = oldAttrs.pname;
              src = prev.fetchurl {
                urls = [ "https://github.com/ankitects/anki/archive/${version}.tar.gz" ];
                # /*2.1.15*/ sha256 = "1yc6rhrm383k6maq0da0hw779i00as6972jai0958m6gmj32kz0n";
                /*2.1.0*/ sha256 = "1c7gwlq1lx6zij88nq45f216pd6xfpiw01svskmzivza75s7adzb";
              };
            }
      );
    };
in
  {
    pkgs ? import <nixpkgs> {
      config = {};
      overlays =
        [
          ankiOverlay
        ];
    }
  }:

  pkgs.mkShell
    { buildInputs = [ pkgs.anki ]; }
$ nix-shell shell.nix

these derivations will be built:
  /nix/store/87v5d4qd2135113d9v3r53l52xirsd7y-anki-2.1.15.drv
building '/nix/store/87v5d4qd2135113d9v3r53l52xirsd7y-anki-2.1.15.drv'...
bash: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8)
Sourcing python-recompile-bytecode-hook.sh
Sourcing python-remove-tests-dir-hook
Sourcing python-catch-conflicts-hook.sh
Sourcing python-remove-bin-bytecode-hook.sh
Sourcing setuptools-build-hook
Using setuptoolsBuildPhase
Using setuptoolsShellHook
Sourcing pip-install-hook
Sourcing python-imports-check-hook.sh
Using pythonImportsCheckPhase
Sourcing python-namespaces-hook
Sourcing setuptools-check-hook
unpacking sources
unpacking source archive /nix/store/z659056c70vcpk80q6hypfkwbjl1vmkk-2.1.0.tar.gz
source root is anki-2.1.0
setting SOURCE_DATE_EPOCH to timestamp 1533529033 of file anki-2.1.0/web/webview.js
patching sources
applying patch /nix/store/2cjiw9b8yay9gyrrg9l7fqj8zymp8gid-no-version-check.patch
patching file aqt/main.py
Hunk #1 succeeded at 906 with fuzz 1 (offset 86 lines).
rm: missing operand
Try 'rm --help' for more information.
builder for '/nix/store/87v5d4qd2135113d9v3r53l52xirsd7y-anki-2.1.15.drv' failed with exit code 1
error: build of '/nix/store/87v5d4qd2135113d9v3r53l52xirsd7y-anki-2.1.15.drv' failed

Then, after commenting out the lines with 2.1.0, and using the 2.1.15 ones, I got the same (only some numbers were different). My next thought was to pin Nixpkgs to the same revision where my channel points to, and my expectation was that the output will be the same as for nix-shell -p anki; apart from copying everything to the Nix store again, and re-compiling Anki from scratch, this was the case.

$ nix-channel --list
nixos https://nixos.org/channels/nixos-20.09
 
$ ls -l $(readlink -f ~/.nix-defexpr/channels)
...
nixos -> /nix/store/k737c631q19n54fhjmnf68frg5dar14w-nixos-20.09.3009.8e78c2cfbae/nixos
...

and this seemed to have worked:
The 2.1.15 lines are active in the shell.nix at the top at this point

edit-1: Of course this worked because I effectively overriden the pkgs argument with an external one, completely sidestepping ankiOverlay… (This is confirmed by the second invoication below when getting the same result for anki --help, and nix-shell doesn’t even recompile anything…).

$ nix-shell \
  --arg pkgs 'import (fetchTarball "https://github.com/NixOS/nixpkgs/archive
/8e78c2cfbae.tar.gz") {}' \
  shell.nix \
--run "anki --help"
...
# long output of copying to the Nix store
# and subsequent compilation of Anki
# (see link after this code block)
...
Anki 2.1.15

(command output)

After commenting out the 2.1.15 lines, and enabling the 2.1.0 ones, the output of the same command above was the same, without the long output so no copying and no compilation took place.

What am I doing wrong?

Based on another post, my basic shell.nix seems to be okay; taking the posts dhall example, and simply embedding it in the shell.nix works:

let
  ankiOverlay =
    final: prev: { 
      anki = builtins.fetchTarball 
        "https://github.com/ankitects/anki/releases/download/2.1.44/anki-2.1.44-linux.tar.bz2";
    };
  
  dhallOverlay =
    self: super: {
        dhall = builtins.fetchTarball
            "https://github.com/dhall-lang/dhall-haskell/releases/download/1.30.0/dhall-1.30.0-x86_64-linux.tar.bz2";
          };
in
  { pkgs ? import <nixpkgs> {
      config = {};
      overlays = [ ankiOverlay dhallOverlay ];
    }
  }:

  pkgs.mkShell
    { buildInputs = [ pkgs.anki pkgs.dhall ]; }

dhall works, but not Anki:

[nix-shell:~/clones]$ dhall --version
1.30.0

[nix-shell:~/clones]$ anki --help
The program ‘anki’ is currently not installed. You can install it by typing:
  nix-env -iA nixos.anki

The dhall release tarball only contains the /bin directory, but Anki’s release is organized a different way.


I guess the bottom line is that overlays are unique to each application, one only has to spend some time to find the right recipe.