Context. It is believed that the majority of stars form in clusters. Therefore it is likely that the gas physical conditions that prevail in forming clusters largely determine the properties of stars that form, in particular, the initial mass function (IMF). Aims. We develop an analytical model to account for the formation of low-mass clusters and the formation of stars within clusters. Methods. The formation of clusters is determined by an accretion rate, the virial equilibrium, as well as energy and thermal balance. For this, both molecular and dust cooling are considered using published rates. The star distribution is computed within the cluster using the physical conditions inferred from this model and the Hennebelle & Chabrier theory. Results. Our model reproduces well the mass-size relation of low-mass clusters (up to a few 10 3 M of stars corresponding to about five times more gas) and an IMF that is i) very close to the Chabrier IMF, ii) weakly dependent on the mass of the clusters, iii) relatively robust to (i.e. not too steeply dependent on) variations in physical quantities such as accretion rate, radiation, and cosmic ray abundances. Conclusions. The weak dependence of the mass distribution of stars on the cluster mass results from the compensation between varying clusters densities, velocity dispersions, and temperatures that are all inferred from first physical principles. This constitutes a possible explanation for the apparent universality of the IMF within the Galaxy, although variations with the local conditions may certainly be observed.