The detection of nucleobases in carbonaceous chondrites such as Murchison supports the scenario in which extraterrestrial organic molecules could have contributed to the origin of life on Earth. However, such large molecules have not been observed to date in astrophysical environments, in particular, comets and the interstellar medium (ISM). The physico-chemical conditions under which nucleobases and, more generally, N-heterocycles were formed are unknown, as are their mechanisms of formation. In this work, H2O:pyrimidine ice mixtures were irradiated with UV photons under interstellar/cometary- relevant conditions to study the formation of pyrimidine derivatives, including the nucleobase uracil. Liquid and gas chromatography analyses of the samples produced in our experiments revealed the presence of numerous photoproducts among which 4(3H)-pyrimidone and uracil could be conclusively identified. The photostability of pyrimidine against UV photons was also studied, and we showed that it would survive from the ISM to the solar nebula if formed and preserved in ice mantles on the surface of cold grains. We propose pathways for the formation of 4(3H)-pyrimidone and uracil under astrophysically relevant conditions and discuss the possibility for such molecules to survive from the ISM to their delivery to Earth and other Solar System bodies.