01 - Preheat oven to 375°F. Generously 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 pinch of salt. Cook, stirring frequently, until deeply golden and caramelized, 20-25 minutes. Add minced garlic during final minute. Remove from heat.
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, avoiding boiling. If using flour, whisk until smooth and slightly thickened.
04 - Arrange half the sweet potato slices in overlapping rows in the prepared dish. Season lightly with salt.
05 - Spread half the caramelized onions over potatoes. Sprinkle with half the Gruyère and half the cheddar cheese.
06 - Layer remaining sweet potatoes, followed by remaining onions and cheeses.
07 - Pour warm cream mixture evenly over the layered ingredients, ensuring coverage across the dish.
08 - Cover tightly with aluminum foil. Bake for 40 minutes until potatoes begin to soften.
09 - Remove foil and continue baking 20 minutes until top is golden brown and potatoes are fork-tender.
10 - Let gratin rest 10 minutes before serving to allow layers to set. Garnish with fresh chives if desired.