01 - Preheat 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 with a pinch of salt. Cook, stirring frequently, until deeply golden and caramelized, about 20-25 minutes. Add minced garlic during the final minute and remove from heat.
03 - In a small saucepan, combine heavy cream, whole milk, dried thyme, nutmeg, black pepper, and remaining 1 tbsp butter. Heat over medium-low heat until steaming but not boiling. If using flour for thickening, whisk in until smooth and slightly thickened.
04 - Arrange half of the sweet potato slices in the prepared baking dish. Season lightly with salt.
05 - Spread half of the caramelized onions over the potatoes. Sprinkle evenly with half of the Gruyère and half of the cheddar cheese.
06 - Repeat layers with remaining sweet potato slices, caramelized onions, and cheeses.
07 - Pour the warm cream mixture evenly over the layered ingredients, ensuring it reaches all corners of the dish.
08 - Cover the dish tightly with aluminum foil. Bake for 40 minutes until potatoes begin to soften.
09 - Remove foil and continue baking for 20 minutes until the top is golden brown and bubbly, and potatoes are fork-tender when pierced.
10 - Let the gratin rest for 10 minutes before serving to allow layers to set. Garnish with chopped fresh chives if desired.