Generating addresses the way Nominatim does

I am using a PostGIS database and I want to do reverse lookups for coordinates and get addresses in order (house, street… country in an ordered list). Nominatim already does so.

How do I generate the address for a given node/coordinates?

Also, I inferred that I must find “closed ways” the node/coordinate is a part of. How does Nominatim do that? And how do I do that myself?

I’ve never done it, but the wiki does contain installation instructions for Nominatim: