Issues with overlay to include newer package

I am running NixOS 20.03 and would like to install Gerbil Scheme. Currently, only 0.15.1 is available, but 0.16 is available in unstable. I honestly considering just jumping to unstable, but I thought this would be a good time to stretch my nix muscles a little, so I am trying to write an overlay.

Here’s my overlay.nix, the vars are basically verbatim from what’s in the unstable nixpkgs:

self: super:

{
  gerbil = super.gerbil.overrideAttrs ( oldAttrs: rec {
    version = "0.16";
    git-version = version;
    src = self.fetchFromGitHub {
      owner = "vyzo";
      repo = "gerbil";
      rev = "v${version}";
      sha256 = "0vng0kxpnwsg8jbjdpyn4sdww36jz7zfpfbzayg9sdpz6bjxjy0f";
    };
  });
}

and my shell.nix:

{ nixpkgs ? import <nixpkgs> { overlays = [(import ./overlay.nix)]; } }:
nixpkgs.mkShell {
  buildInputs = [nixpkgs.gerbil];
}

When I run this, it appears to run correctly, it mentioned unpacking source archive /nix/store/3bhsqprh02qr5aana5bdz20ysb53hdbz-source, and when I checked that directory it does appear to have the correct version source: (define (gerbil-version-string) "v0.16"). However once it finishes building, and the shell launches, it loads gerbil 0.15.1!

[nix-shell:~/work/gerbil]$ gxi
Gerbil v0.15.1 on Gambit v4.9.3
[nix-shell:~/work/gerbil]$ which gxi
/nix/store/n0fwpf0r83xdy48wx7d1sxmf4kdv6p8z-gerbil-0.16/bin/gxi

That nix store link appears to be correct based on the build output from when I ran nix-shell so it appears to have build 0.15.1 or 0.15.1 has otherwise leaked into the build. Does anyone have any idea for how I may proceed?

The builder for gerbil seems to patch in the correct version, which is likely why it reports being version 0.16. However I don’t know why the correct version is not being used

I have managed to get this to work, thanks in part to your insightful comment alexarice, and thanks in part to balsoft in the Nix room on Matrix.org

postPatch was indeed my problem! I believe I was actually building and running Gerbil 0.16 the entire time, and this postPatch call was what was overwriting the version number that I could see in the repl. When I check for a function introduced in 0.16, it does also appear in my “0.15.1 repl.”

When I passed in that same postPatch via overrideAttrs everything worked as expected. I believe this could be “fixed” by adjusting postPatch to use "v$git-version" to do that variable substitution at package-build-time rather than nix-compile-time, although I’m not sure if this will be accepted.

The final overlay.nix for those following along at home:

self: super:

{
  gerbil = super.gerbil.overrideAttrs ( oldAttrs: rec {
    version = "0.16";
    git-version = version;
    postPatch = ''
      echo '(define (gerbil-version-string) "v${git-version}")' > src/gerbil/runtime/gx-version.scm ;
      patchShebangs . ;
      grep -Fl '#!/usr/bin/env' `find . -type f -executable` | while read f ; do
        substituteInPlace "$f" --replace '#!/usr/bin/env' '#!${self.coreutils}/bin/env' ;
      done ;
    '';
    src = self.fetchFromGitHub {
      owner = "vyzo";
      repo = "gerbil";
      rev = "v${version}";
      sha256 = "0vng0kxpnwsg8jbjdpyn4sdww36jz7zfpfbzayg9sdpz6bjxjy0f";
    };
  });
}