Microbial life on Earth commonly occurs in diverse and complex communities where species interact, and their genomic repertoires evolve over time. Our understanding of species interaction and evolution has increased during last decades, but most studies of evolutionary dynamics are based on single species in isolation or experimental systems composed of few interacting species. Here, we use the microbial ecosystem found in groundwater-fed sand filters as a model to avoid this limitation. In these systems, diverse microbial communities experience relatively stable conditions, and the coupling between chemical and biological processes is generally well defined. Metagenomic analysis of 12 sand filters revealed systematic co-occurrence of at least five comammox Nitrospira species, favoured by low ammonium concentrations. Nitrospira species showed intra-population sequence diversity, although possible clonal expansion was detected in few abundant local comammox populations. Nitrospira populations were separated by gene flow boundaries, suggesting natural and cohesive populations. They showed low homologous recombination and strong purifying selection, the latest process being especially strong in genes essential in energy metabolism. Positive selection was detected on genes related to resistance to foreign DNA and phages. Additionally, we analysed evolutionary processes in populations from different habitats. Interestingly, our results suggest that in comammox Nitrospira these processes are not an intrinsic feature but greatly vary depending on the habitat they inhabit. Compared to other habitats, groundwater fed sand filters impose strong purifying selection and low recombination. Together, this study improves understanding of interactions and evolution of species in the wild, and sheds light on the environmental dependency of evolutionary processes.