01 - Preheat oven to 375°F and generously grease a 9x13-inch baking dish with butter or cooking spray.
02 - Melt 2 tablespoons 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 last minute and remove from heat.
03 - In a small saucepan, combine heavy cream, whole milk, dried thyme, nutmeg, black pepper, and remaining 1 tablespoon butter. Heat over medium-low until steaming but not boiling. If using flour for thickening, whisk it in until smooth and slightly thickened. Remove from heat.
04 - Arrange half of the sweet potato slices in an even layer at the bottom of the prepared baking dish. Season lightly with salt.
05 - Spread half of the caramelized onions over the potatoes. Sprinkle with half of the Gruyère and half of the cheddar cheese.
06 - Add remaining sweet potato slices, followed by remaining caramelized onions, and finish with remaining cheeses.
07 - Evenly pour the warm cream mixture over the layered ingredients, ensuring coverage across the entire dish.
08 - Cover the baking dish tightly with aluminum foil and bake for 40 minutes.
09 - Remove foil and continue baking uncovered for 20 minutes, or until the top is golden brown and bubbly, and potatoes are fork-tender.
10 - Let the gratin rest for 10 minutes before serving to allow layers to set. Garnish with fresh chives if desired.