How to override a nested derivation

I am attempting to converted the changes found in this PR into an overlay so I don’t need to keep updating my own fork of nixpkgs. The trouble is, I need to override the attributes of a derivation inside another derivation.

I’ve attempted to just override the inner derivation, hoping it would be that easy, but as expected that did not work. I’ve also attempted to override the “sublime_text” attribute of the outer derivation with another override, thought that surprisingly also did not work. What am I missing here?

My latest attempt is below:

final: prev:
assert builtins.hasAttr "sublime4" prev;
{
  final.sublime4 = prev.sublime4.overrideAttrs(prevAttrs:
  let
    primaryBinary = "sublime_text";
  in
  assert builtins.hasAttr primaryBinary prevAttrs;
  {
    ${primaryBinary} = prevAttrs.${primaryBinary}.overrideAttrs (
      let
        pkexecPath = "/run/wrappers/bin/pkexec";
        redirects = [ "/usr/bin/pkexec=${pkexecPath}" ];
      in {
        postFixup = ''
          wrapProgram $out/${primaryBinary} \
            --set LD_PRELOAD "${final.libredirect}/lib/libredirect.so" \
            --set NIX_REDIRECTS ${builtins.concatStringsSep ":" redirects} \
            --set LOCALE_ARCHIVE "${final.glibcLocales.out}/lib/locale/locale-archive" \
            "''${gappsWrapperArgs[@]}"
        '';
      }
    );
  });
}

Should be sublime4 here not final.sublime4. Though there may be other issues that come up.

Besides, since this is a leaf package, you likely don’t even need an overlay, and could override in situ.

Should be sublime4 here not final.sublime4.

This was the issue. It’s always the simple things, isn’t it?

Besides, since this is a leaf package, you likely don’t even need an overlay, and could override in situ.

Oh, I didn’t realize that was possible. Are overlays really only needed for things that might be depended on?

Overlays are for overriding all uses of an attribute of a nixpkgs instance. If your config only uses that attribute in one place (let’s say, environment.systemPackages), then an overlay is a bit overkill. It’s mostly for dependencies that need to be overriden for feature-related or security reasons.

edit: and for context, (pkgs.foo.override { ... }) (or overrideAttrs) is a package expression, so if you wish, you can drop that in directly where you’d normally use a package.