Why does magnesium react with oxygen more than nitrogen?

Nitrogen has greater electron affinity than oxygen, so it grabs magnesium's electrons more readily than oxygen. As a result, you get white magnesium oxide rather than green magnesium nitride.