The recent global COVID-19 pandemic has highlighted treatments for coronavirus infection as an unmet medical need. The main protease (Mpro) has been an important target for the development of SARS-CoV-2 direct-acting antivirals. Nirmatrelvir as a covalent Mpro inhibitor was the first such approved therapy. Although Mpro inhibitors of various chemical classes have been reported, they are generally less active against nirmatrelvir-resistant variants and have limited pan-coronavirus potential, presenting a significant human health risk upon future outbreaks. We here present a novel approach and utilized DNA-encoded chemical library screening to identify the noncovalent Mpro inhibitor 5, which demonstrated a distinct binding mode to nirmatrelvir. A macrocyclization strategy designed to lock the active conformation resulted in lactone 12 with significantly improved antiviral activity. Further optimization led to the potent lactam 26, which demonstrated exceptional potency against nirmatrelvir-resistant variants as well as against a panel of viral main proteases from other coronaviruses.