Cosmology from a Lagrangian formulation for Rastall's theory