Both of these factors did play a role in bringing World War I about.
Nationalism helped to cause the war in at least two ways. First, it helped to bring about the alliances that made the war expand to such a great degree. For example, the Russians and the Serbs allied with one another largely on nationalistic grounds (they were both ethnically Slavic countries). The Germans and Austro-Hungarians also allied on nationalistic grounds as the rulers of Austria-Hungary were ethnically German. Second, nationalism led to the assassination that sparked the war. Slavs in the Austro-Hungarian Empire were unhappy about being ruled by Germans. To call attention to their unhappiness, they assassinated the Archduke Franz Ferdinand.
Imperialism helped to bring the war about because it caused rivalries between countries. Many countries wanted larger empires so as to enhance their national prestige in the world. This was part of the “place in the sun” that Germany wanted. They wanted an empire that would be in keeping with their vision of their importance in the world.
In these ways, both nationalism and imperialism (and not just one of them) helped bring WWI about.