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