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 a pinch of salt. Cook, stirring frequently, until onions are deeply golden and caramelized, about 20-25 minutes. Add minced garlic during the final minute and remove from heat.
03 - Combine cream, milk, thyme, nutmeg, black pepper, and remaining 1 tbsp butter in a small saucepan over medium-low heat. Heat until steaming but not boiling. If using flour, whisk in until smooth and slightly thickened.
04 - Arrange half the sweet potato slices in the prepared dish, slightly overlapping. Season lightly with salt. Spread half the caramelized onions over potatoes, then 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 coverage across the entire dish.
07 - Cover baking dish tightly with aluminum foil. Bake for 40 minutes to allow potatoes to steam and soften.
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 - Allow gratin to rest for 10 minutes before serving to set. Garnish with chopped fresh chives if desired.