If you are growing organically, fungus gnats lay eggs in the soil, so you can sprinkle diatomaceous earth onto the soil surface. It feels like a white powder, but is made of microscopic shells that cut the larva to pieces when they hatch and try to move.

The silica content is a nutrient to plants too.

You can purchase it at some nurseries (call around, most don't carry it) or in pool supply stores.