Preheat oven to 375°F (190°C) and grease a 9x13-inch baking dish.
In a saucepan, melt butter over medium heat. Add chopped onion and sauté until translucent.
Whisk in flour and cook for 1 minute. Gradually whisk in milk until smooth.
Season with salt, pepper, and nutmeg. Stir until sauce thickens.
Layer half of the sliced potatoes in the baking dish. Pour half of the sauce over the potatoes and sprinkle with half the cheese.
Repeat with remaining potatoes, sauce, and cheese.
Cover with foil and bake for 45 minutes. Remove foil and bake for an additional 20–25 minutes until golden and bubbly.
Garnish with fresh thyme if desired. Let rest for 10 minutes before serving.