Melt the butter in a large non-stick frying pan, add the onions and cook for 20 mins, stirring now and then, until they are soft and golden brown. Season, stir in the thyme leaves, then transfer to a plate to cool.
Heat oven to 200C/180C fan/gas 6 with a large baking sheet inside. Roll out the pastry on a lightly floured surface until large enough to line a 25cm loose-bottomed tart tin, with a little overhang of pastry all the way round the top. This will stop the pastry shrinking in the oven later. If there is more overhang than you need, trim off the excess with scissors. Gently press the pastry into the sides of the tin and prick the base with a fork. Chill for 15 mins.
Line the pastry case with a sheet of baking parchment and fill with ceramic baking beans. Bake for 15 mins on the hot baking sheet (this helps to prevent a soggy bottom). Carefully remove the parchment and beans, then return to the oven for 10 mins more until the pastry looks cooked like shortbread, but is not too brown.
Meanwhile, put the bacon in the pan you cooked the onions in (there’s no need to wash it first or add any oil) and fry for 10 mins until golden. Lift from the pan onto kitchen paper to remove any excess fat. Beat the cream, milk and eggs with seasoning and nutmeg, then stir in the bacon and half the cheese.
Remove the pastry case from the oven and reduce heat to 190C/170C fan/gas 5. Spoon the caramelised onions evenly over the base of the pastry case. Pour in the bacon mixture, then scatter with the remaining cheese, the thyme sprigs and a little nutmeg. Bake for 25-30 mins until golden and the filling is just set with a slight wobble in the centre.
Trim the excess pastry and leave to settle for 10 mins, then remove from the tin and slice.