  The relic density of dark matter in the $\Lambda$CDM model restricts the
parameter space for a cosmological axion field, constraining the axion decay
constant, the initial amplitude of the axion field and the axion mass. It is
shown via lattice simulations how the relic density of axion-like particles
with masses close to the one of the QCD axion is affected by axion-gauge field
interactions and by initial axion inhomogeneities. For pre-inflationary axions,
once the Hubble parameter becomes smaller than the axion mass, the latter
starts to oscillate, and part of its energy density is spent producing gauge
fields via parametric resonance. If the gauge fields are dark photons and
Standard Model photons, the energy density of dark photons becomes higher than
the one of the axion, while the high conductivity of the primordial plasma
damps the oscillations of the photon field. Such a scenario allows for the
production of small-scale, primordial magnetic fields, and it is found that the
relic density of axions with a low decay constant are within the bounds set by
the $\Lambda$CDM model, while GUT-scale axions are far too abundant. It is also
shown that initial inhomogeneities of the axion field can change substantially
the gauge field production, boosting or suppressing (depending on the axion
parameters and couplings) the magnetogenesis mechanism with respect to an
homogeneous axion field. It is found that when the axion mass is far lighter
than the QCD axion model and the initial axion field is inhomogeneous, weak but
cosmologically relevant magnetic field seeds can be generated on scales of the
order of $0.1$ kpc.
