Prepare a medium-hot charcoal fire or heat a gas grill for 20 minutes on high. In a bowl, mix the olive oil, basil, garlic, walnuts, and 1 tsp. salt. Brush the eggplant, tomato, and onion with more olive oil, season with salt and pepper, and put them on the grill. Grill the tomato, turning only once, until the skin is blistering and the flesh looks mushy and cooked, 15 to 25 minutes. Grill the onion until soft and blackened on both sides, about 15 minutes. Grill the eggplant until golden brown and tender, 10 to 25 minutes. Coarsely chop the eggplant, tomato, and onion; put them in a large bowl along with their juices.
Meanwhile, bring a large pot of well-salted water to a vigorous boil and add the spaghetti. Cook until al dente; drain well. Toss the pasta with the vegetables and the basil mixture. Sprinkle with pepper; toss again and serve immediately, with freshly grated Parmigiano, if you like.