How to search nixpkgs by e.g. `buildInputs`?

I explored writing something in nix for this purpose. Unfortunately evaluation failures make it kind of clumsy to write and ultimately I was unable to catch specific errors making it fail when I try to run across all of nixpkgs.

# filterByInput.nix
{
  depName,
  attr ? "propagatedBuildInputs",
}: let
  pkgs = import <nixpkgs> {};

  hasName = pkg: (builtins.tryEval pkg).success && (pkg ? pname || pkg ? name);
  hasDep = pkg: pkg ? "${attr}" && builtins.any (dep: hasName dep && (builtins.match (depName + ".*") (dep.pname or dep.name)) != null) pkg.${attr};

  pkgList = pkgs.lib.take 20 (builtins.attrValues pkgs);
  # pkgList = builtins.attrValues pkgs;
in {
  triedOn = builtins.map (pkg: (builtins.tryEval (pkg.pname or pkg.name or "<unnamed>")).value) pkgList;
  result = builtins.map (pkg: pkg.pname) ((builtins.filter (pkg: hasName pkg && hasDep pkg)) pkgList);
}

Obviously could use some refactoring and simplification, but it seems to work:

$ nix eval --json -f filterByInput.nix --argstr depName python --argstr attr propagatedBuildInputs result
["fabric"]
$ nix eval --json -f filterByInput.nix --argstr depName cmake --argstr attr nativeBuildInputs result
["AusweisApp2","CHOWTapeModel","ChowCentaur","ChowKick","ChowPhaser","EBTKS","empty-epsilon","LAStools","LASzip","LASzip"]

I’m not sure how to get rid of the clumsy “result” requirement for the nix eval call ($ nix eval ... result) since the -f makes it expect an attrset (and an attr to get from that attrset).

Unfortunately expanding pkgList to all of nixpkgs results in an error, and I can’t seem to tryEval my way around it:

$ nix eval --json -f filterByInput.nix --argstr depName cmake --argstr attr nativeBuildInputs result
error: anonymous function at /nix/store/hqyzlrq2kpp6w9cgvaybgx8swmc3p6za-source/pkgs/development/compilers/openjdk/darwin/19.nix:1:1 called with unexpected argument 'enableJavaFX'

       at /nix/store/hqyzlrq2kpp6w9cgvaybgx8swmc3p6za-source/lib/customisation.nix:80:16:

           79|     let
           80|       result = f origArgs;
             |                ^
           81|

I’m sure the easy way would be to just write something in bash that deals with the exceptions.
filterAttrs would probably also be an improvement.