1. Prepare the potatoes
Peel, wash, and slice the potatoes. Cook them in a large pot of boiling salted water for 10 to 12 minutes, or until tender. Carefully drain the water.
2. Prepare the filling: If desired, sauté the onion and garlic in a little butter or oil until softened. Add thyme, if desired.
3. Prepare the sauce: Melt the butter, stir in the flour, and cook until a roux forms. Gradually whisk in the milk. Let the sauce thicken, then stir in the crème fraîche and season with salt, pepper, and nutmeg.
4. Assemble the gratin: Preheat the oven to 180°C (350°F). Grease a gratin dish. Layer the potatoes, ham, onion/garlic (if using), and sauce. Sprinkle with grated cheese.
5. Bake: Bake for 30 to 40 minutes until the surface is golden brown. Let stand for 5 minutes before serving.