Preheat oven to 350F
Toast hazelnuts or walnuts on a rimmed baking sheet, tossing halfway through, until golden brown, about 6 - 8 minutes. Let cool, then finely chop and transfer to a medium bowl; stir in 2 tbsp olive oil and set aside
Heat remaining 2 tbsp oil in a medium pot over medium heat. Cook onion, stirring occasionally, until golden brown and softened, 8 - 10 minutes
Add garlic and red pepper flakes and cook, stirring, until fragrant and garlic is softened, about 1 minute
Add squash and toss to combine. Add stock and bring to a boil. Reduce heat to medium-low and gently simmer until squash is tender, 12 - 15 minutes. Let cool slightly
Meanwhile, cook pasta in a large pot of salted boiling water until very al dente, about 3 minutes less than package directions. Drain and rinse with cold water
Transfer squash mixture to a blender. Add 1 cup Parmesan and puree until smooth. Season with salt
Pour 1/2 cup squash puree into a 13 x 9 inch baking dish and spread to edges. Scatter pasta over the sauce, then pour remaining sauce over. Top with 1/2 cup Parmesan. Cover with foil and bake 15 minutes. Uncover and increase oven temperature to 425F. Continue to bake until top is darkened in spots and sauce is bubbling, 10 - 15 minutes
Add basil to bowl with reserved hazelnuts, season with salt, and toss to combine. Spoon hazelnut mixture over pasta and top with more Parmesan and black pepper