Biological cofactors include functionalized derivatives of cyclic tetrapyrrole structures that incorporate different metal ions. They build up structural partnerships with proteins, which play a crucial role in biochemical reactions. Porphyrin, chlorin, bacteriochlorin, and corrin are the basic structures of cofactors (heme, chlorophyll, bacteriochlorophyll, siroheme, F 430, and vitamin B12). Laboratory and theoretical work suggest that the molecular building blocks of proteins (α-amino acids) and nucleic acids (carbohydrates, purines, and pyrimidines) were generated under prebiotic conditions. On the other hand, experimental data on the prebiotic chemistry of cofactors are rare. We propose to search directly for the pathways of the formation of cofactors in the laboratory. Herein we report on the detection of N-heterocycles and amines in the room-temperature residue obtained after photo- and thermal processing of an interstellar ice analogue under high vacuum at 12 K. Among them, hexahydro-1,3,5-triazine and its derivatives, together with monopyrrolic molecules, are precursors of porphinoid cofactors. Hexahydropyrimidine was also detected. This is the first detection of these compounds in experiments simulating circumstellar/interstellar conditions. Except for 2-aminopyrrole and 2,4-diaminofuran, which were only found in 13C-labeled experiments, all the reported species were detected in both 12C- and 13C-labeled experiments, excluding contamination. The molecules reported here might be present in circumstellar/interstellar grains and cometary dust and could be detected by the Stardust and Rosetta missions.