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, heat heavy cream, whole milk, dried thyme, ground nutmeg, black pepper, and remaining 1 tbsp butter over medium-low heat until steaming (do not boil). If using flour, whisk it in until smooth to slightly thicken the mixture.
04 - Layer 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 layering with remaining sweet potato slices, caramelized onions, and cheeses.
06 - Pour the warm cream mixture evenly over the layered ingredients, ensuring it penetrates throughout the dish.
07 - Cover the baking dish tightly with aluminum foil and bake for 40 minutes.
08 - Remove foil and continue baking uncovered for an additional 20 minutes, or until the top is golden brown and the 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.