Preheat and prep: Heat your oven to 375°F (190°C).
Grease a 9x13-inch baking dish with butter or olive oil. Set aside.
Slice the potatoes: Using a mandoline or a sharp knife, slice potatoes into 1/8-inch rounds. Keep slices similar in thickness so they cook evenly.
Sauté the aromatics: In a medium saucepan, melt 4 tablespoons butter over medium heat.
Add sliced onion and a pinch of salt. Cook 5–7 minutes until soft and translucent. Stir in the garlic and cook 30 seconds.
Make the roux: Sprinkle flour over the onions and stir for 1 minute to cook off the raw taste.
The mixture will look pasty—this is good.
Add milk and cream: Slowly whisk in the milk and cream. Keep whisking as it thickens to a smooth, velvety sauce, 3–5 minutes. Season with 1 teaspoon salt, 1/2 teaspoon black pepper, and a pinch of nutmeg if using.
Stir in cheese: Remove from heat and add 1 1/2 cups cheddar and 1/2 cup Gruyère.
Stir until melted and silky. Taste and adjust seasoning.
Layer the potatoes: Arrange a third of the potato slices in the baking dish, slightly overlapping. Sprinkle lightly with salt, pepper, and thyme if using.
Spoon over a third of the cheese sauce. Repeat two more times, finishing with sauce.
Top with remaining cheese: Sprinkle the remaining cheddar and Gruyère over the surface for a browned, bubbly crust.
Bake covered, then uncovered: Cover the dish tightly with foil and bake 35 minutes. Remove foil and bake another 20–30 minutes, until the top is golden and a knife slips through the layers easily.
Rest before serving: Let the dish sit for 10–15 minutes.
This helps the sauce set so slices hold together nicely.