It doesn’t have to be import from derivation. If the lockfile is in json format, nix can import it as-is without an derivation that results in nix code.
This way it is possible to read the json file, convert the structure to one that fits a mkYarnPackage-like call. It probably maps urls and their hashes to fetchurl calls.
The same can be done with other text-based files (like Gemfile.lock) by reading the file as a string and using the Nix built-in functions to for instance split the string into lines and parse lines that way. The built-in functions are quite limited so I presume this isn’t the right way to go.
It would probably require more compute time to convert such files compared to a pregenerated nix file, but it is very different from an IFD. In addition, I thought Nix 3 would be doing more evaluation caching, so such computations will become faster in the future.