Can I create a shader that uses the distance to the first opaque object behind it?
you can get a "haze" effect without material shader nodes. Under composite, you can set a Z pass (or distance to camera pass), and you can then blur it, where the intensity of the blur is controlled by the "distance".
You can consider Z pass as a black to white raster/mask, where black conceals and white reveals, and also where black to white values represent 2.5D information on Z towards the camera. With that in mind, you can manipulate this "mask", or even create it (manually) in third party software (gimp, photoshop, etc.), then import the raster layer, and use this layer as the Z pass.

About haze. Naturally haze is a "blur effect" caused by different compositions and densities in the atmosphere. You can achieve a more natural haze effect with: you set a "domain" for your atmosphere (a big cube or sphere will be enough), then you set the material of this atmosphere object to volumetric. Using the volumetric shader nodes, you will be able to have more control on the "atmosphere".