In a large, heavy-bottomed pot or Dutch oven, heat the olive oil and 2 tablespoons of butter over medium heat. Add the sliced onions and cook, stirring occasionally, for 10 minutes until they have softened.
Stir in the salt. Reduce the heat to low and continue to cook the onions for 40-50 minutes, stirring every 5-10 minutes to prevent sticking. The onions should be a deep golden brown and very soft. If they start to dry out, add a tablespoon of water.
When the onions are nearly done, bring a large pot of salted water to a boil. Cook the pasta according to package directions until al dente. Before draining, reserve about 2 cups of the pasta water.
While the pasta cooks, return the onion pan to medium heat (or use a separate large skillet). Melt the remaining 2 tablespoons of butter. Add the minced garlic and red pepper flakes, if using. Cook for 1 minute until fragrant, but do not let the garlic brown.
Pour in the heavy cream and bring to a gentle simmer. Reduce the heat to low. Add the Parmesan cheese in small batches, stirring until each addition is melted and the sauce is smooth. Stir in the fresh thyme.
Drain the pasta and add it directly to the skillet with the cream sauce. Add the caramelized onions, lemon juice, and 1/2 cup of the reserved pasta water.
Toss everything together over low heat for 1-2 minutes, allowing the sauce to coat the pasta and thicken slightly. Add more pasta water, a tablespoon at a time, to reach your desired sauce consistency. Season with black pepper.
Serve immediately, garnished with extra Parmesan cheese and lemon zest if you like.