How to prefetch all build dependencies from binary cache, then build offline?

Let’s say I have a derivation in flake.nix, with only nixpkgs as input.

I want to download/prefetch every build dependencies of that derivation from cache.nixos.org to local /nix/store, so that when I run

nix buid --offline github:myname/myrepo#mypackage

it will not build anything from nixpkgs.

Is there any method/command to achieve this?

EDIT: Assume there is no IFD

For NixOS there’s the system.includeBuildDependencies option. I’ve never looked into the implementation, but I bet you could try to reuse the code or at least the same approach for a general derivation.

EDIT: Indeed, it’s based on pkgs.closureInfo, which can be used on any derivation.

Do you want deep dependencies too or just an offline build? I would just run it with -j0 for the latter; for the former you need to instantiate the derivation to build, ask for its closure, then realise each derivation in the closure with -j0