  In this paper we show that the on-shell Lagrangian of a perfect fluid depends
on microscopic properties of the fluid, giving specific examples of perfect
fluids with different on-shell Lagrangians but with the same energy-momentum
tensor. We demonstrate that if the fluid is constituted by localized
concentrations of energy with fixed rest mass and structure (solitons) then the
average on-shell Lagrangian of a perfect fluid is given by ${\mathcal L}_m=T$,
where $T$ is the trace of the energy-momentum tensor. We show that our results
have profound implications for theories of gravity where the matter Lagrangian
appears explicitly in the equations of motion of the gravitational and matter
fields, potentially leading to observable deviations from a nearly perfect
cosmic microwave background black body spectrum: $n$-type spectral distortions,
affecting the normalization of the spectral energy density. Finally, we put
stringent constraints on $f(R,{\mathcal L}_m)$ theories of gravity using the
COBE-FIRAS measurement of the spectral radiance of the cosmic microwave
background.
