01 - Preheat oven to 375°F. 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 with a pinch of salt. Cook stirring frequently until deeply golden and caramelized, about 20-25 minutes. Add minced garlic and cook 1 minute more. Remove from heat.
03 - Heat cream, milk, thyme, nutmeg, black pepper, and remaining 1 tbsp butter in a small saucepan over medium-low heat until steaming. Do not boil. If using flour, whisk in until smooth to slightly thicken.
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 with half of the Gruyère and cheddar cheese.
06 - Repeat layering with remaining sweet potatoes, caramelized onions, and cheeses.
07 - Pour the warm cream mixture evenly over all layers in the baking dish.
08 - Cover tightly with aluminum foil. Bake for 40 minutes.
09 - Remove foil and continue baking for 20 minutes or until top is golden brown and potatoes are fork-tender.
10 - Let gratin rest for 10 minutes before serving. Garnish with fresh chives if desired.