Bring a large pot of water to a boil and salt it generously. Cook the pasta 1 minute past al dente, since pasta firms up as it cools.
Before draining, scoop out 1/2 cup of the cooking water. Drain the pasta and rinse briefly with cool water just to stop the cooking, then drain well.
In a large bowl mix the pesto, lemon juice, olive oil and 2 tablespoons of the reserved pasta water. Add the pasta and toss to coat.
Let it cool for 10 minutes, then fold in the tomatoes, mozzarella, cucumber and onion.
Season with salt and pepper. Scatter the toasted nuts and torn basil over the top just before serving.