How to get a trace of how something is defined

When nix fails to build a derivation it prints out a trace of what lead to that failure and the final error. I’m wondering if there is a way to get a similar kind of trace for any attribute. For example, there is an attribute I’m using that is defined somewhere in the nixpkgs I imported and I want to look at how it was defined. Is there a way to get nix to tell me how and where that attribute is declared?

there is builtins.unsafeGetAttrPos, but the result are not always what you want.