We present one-dimensional models of the hot gas in dark matter haloes, which both predict the existence of cool cores and explain their structure. Our models are directly applicable to semi-analytic models of galaxy formation. We have previously argued that filaments of cold (∼104 K) gas condense out of the intracluster medium (ICM) in hydrostatic and thermal equilibrium when the ratio of the thermal instability time-scale to the free-fall time tTI/tff falls below 5–10. This criterion corresponds to an upper limit on the density of the ICM and motivates a model in which a density core forms wherever tTI/tff ≲ 10. Consistent with observations and numerical simulations, this model predicts larger and more tenuous cores for lower mass haloes – while the core density in a cluster may be as large as ∼0.1 cm−3, the core density in the Galactic halo should not exceed ∼10−4 cm−3. We can also explain the large densities in smaller mass haloes (galactic ‘coronae’) if we include the contribution of the central galaxy to the gravitational potential. Our models produce a favourable match to the observational X-ray luminosity–temperature (LX–TX) relation. For halo masses ≲1013 M⊙ the core size approaches the virial radius. Thus, most of the baryons in such haloes cannot be in the hot ICM, but either in the form of stars or in the form of hot gas beyond the virial radius. Because of the smaller mass in the ICM and much larger mass available for star formation, the majority of the baryons in low-mass haloes (≲1013 M⊙) can be expelled beyond the virial radius due to supernova feedback. This can account for the baryons ‘missing’ from low-mass haloes, such as the Galactic halo.