The chemical reactions and pathways involving a nucleobase, a nitrogenous base that is a constituent of a nucleic acid, e.g. the purines: adenine, guanine, hypoxanthine, xanthine and the pyrimidines: cytosine, uracil, thymine.