Preheat the oven to 375 degrees F.
Thinly slice the onions. Heat the 4 tablespoons of butter in a skillet over medium heat. Add in the onions with a big pinch of salt and pepper. Stir. Cook for 10 minutes, stirring occasionally, until the onions soften. Reduce the heat to medium-low, stir in the thyme and cook for another 30 to 40 minutes, stirring often, until the onions are golden and caramelly in color. If at any time the onions start to burn, turn down the heat or add a splash of water to the pan. If the onions are not cooking, you need to raise the temperature a bit.
Use a mandoline or sharp knife to thinly slice the potatoes - you want them to be very thin, uniform in size and ⅛ inch thick or thinner. If you are slicing them ahead of time or with a while to go before assembling, place them in a bowl of ice water so they don’t brown.
Make the sauce! Heat the other 4 tablespoons of butter in a saucepan until melted. Whisk in the flour to create a roux. Cook for 2 to 3 minutes, stirring, until the roux is golden in color and smells fragrant and nutty. Slowly stream in the milk while whisking the entire time, bringing the sauce together.
Continue to stir the sauce and whisk in a pinch of salt and pepper and the nutmeg. Reduce the heat to low. Slowly stir in 6 ounces (about half) of the gruyere until melted. Taste and season with more salt and pepper if needed.
Layer half of the potatoes in the baking dish. Cover with half of the cheese sauce, then half of the caramelized onions and half of the remaining grated gruyere. Layer the remaining potatoes on top. Top with the remaining cheese sauce, covering all the potatoes. Top with the remaining caramelized onions and the grated gruyere. Top with the bread crumbs.
Bake uncovered for 45 to 60 minutes, or until the potatoes are tender enough to pick up with a fork. If the top gets too brown towards the end, you can tent it with foil. Let cool slightly before serving.