01 - Preheat oven to 375°F. Grease a 9x13-inch baking dish with butter or cooking spray.
02 - Melt 2 tbsp butter in a large skillet over medium heat. Add sliced onions and a pinch of salt. Cook, stirring frequently, until deeply golden and caramelized, about 20-25 minutes. Add minced garlic during the last minute and cook until fragrant.
03 - Combine heavy cream, whole milk, thyme, nutmeg, black pepper, and remaining 1 tbsp butter in a small saucepan. Heat over medium-low until steaming but not boiling. If using flour, whisk in until smooth to create a slightly thickened base.
04 - Arrange half the sweet potato slices in the prepared baking dish. Season lightly with salt. Spread half the caramelized onion mixture over the potatoes. Sprinkle with half the Gruyère and half the cheddar cheese.
05 - Repeat layering with remaining sweet potatoes, caramelized onions, and cheeses.
06 - Pour the warm cream mixture evenly over the layered ingredients, ensuring it seeps through all layers.
07 - Cover the dish tightly with aluminum foil. Bake for 40 minutes to allow potatoes to cook through.
08 - Remove foil and continue baking uncovered for 20 minutes, or until the top is golden brown and bubbly and potatoes are fork-tender.
09 - Let the gratin rest for 10 minutes before serving to allow layers to set. Garnish with fresh chives if desired.