Preheat the oven to 190C/170C fan/Gas 5. Grease a medium ovenproof dish with a little butter.
Melt the butter in a saucepan over medium heat. Add the leeks and onion, cook gently for 8 minutes until soft and sweet. Remove from the pan and set aside.
In the same pan, add the flour and stir for 1 minute. Gradually whisk in the milk, stirring until you have a smooth sauce. Cook for 3-4 minutes until thickened. Stir in half the cheese, the thyme and some salt, pepper and nutmeg if using.
Layer half the sliced potatoes in the dish. Spread the leek and onion mixture over the top. Layer the remaining potatoes on top.
Pour the cheese sauce evenly over the potatoes. Scatter the remaining cheese on top.
Bake for 40-45 minutes until the top is golden and bubbling and the potatoes are tender when pierced with a knife. Let it rest for 5 minutes before serving.