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 minced garlic and cook for 1 minute more. Remove from heat.
03 - In a small saucepan, combine heavy cream, whole milk, dried thyme, ground nutmeg, black pepper, and remaining 1 tbsp butter. Heat over medium-low heat until steaming, but do not boil. 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, overlapping slightly. Season lightly with salt.
05 - Spread half of the caramelized onions evenly over the potatoes. Sprinkle with half of the Gruyère cheese and half of the cheddar cheese.
06 - Repeat layering with remaining sweet potato slices, followed by remaining caramelized onions, and the rest of the Gruyère and cheddar cheeses.
07 - Pour the warm cream mixture evenly over the layered ingredients, ensuring it reaches all corners of the dish.
08 - Cover the baking dish tightly with aluminum foil. Bake for 40 minutes to allow potatoes to cook through.
09 - Remove the foil and continue baking uncovered for an additional 20 minutes, or until the top is golden brown and bubbly, and the potatoes are fork-tender.
10 - Let the gratin rest for 10 minutes before serving to allow the layers to set. Garnish with fresh chives if desired.