We study the existence and regularity of optimal
domains for functionals depending on the spectrum of the Dirichlet Laplacian or of more general Schrödinger operators. The domains are subject to perimeter and volume constraints; we also take into account the possible presence of geometric obstacles. We investigate the properties of the optimal sets and of the optimal state functions. In particular, we prove that the eigenfunctions are Lipschitz continuous up to the boundary and that the optimal sets subject to the perimeter constraint have regular free boundary. We also consider spectral optimization problems in non-Euclidean settings and optimization problems for potentials and measures, as well as multiphase and optimal partition problems.