In the model organism Drosophila melanogaster, the PIWI-interacing RNA pathway contributes in silencing transposable elements (TEs) through smallRNAs (piRNAs), which arise from genomic loci (piRNA clusters) that contain sequences of previously-acquired TEs. As such, they are a functionally-immune archive of previous TE invasions that is passed to the offspring. In the arboviral vector Aedes aegypti, piRNA clusters contain TEs and endogenous viral elements from nonretroviral RNA viruses (nrEVEs) which produce piRNAs, supporting the hypothesis that nrEVEs are heritable immunity effectors. However, direct evidence that nrEVEs mediate adaptive immunity is lacking. Here, by using an analytic approach intersecting population genomics with molecular biology we demonstrate that the composition of piRNA clusters is modular through acquisition and absence of nrEVEs. We show that the genomes of wild-caught mosquitoes have a different set of nrEVEs than those annotated in the reference genome, including population-specific integrations. nrEVEs are not distributed in mosquito genomes only by genetic drift, but some show signs of positive selection. Moreover, by comparing natural mosquito populations expressing or lacking two newly characterised nrEVEs with high sequence complementarity to cell fusing agent virus, we show that nrEVEs confer antiviral immunity in ovaries against the cognate virus. Our results confirm that some nrEVEs have been co-opted for adaptive immunity to viral infections. form an "archive" in the genome (Marraffini and Sontheimer, 2010; Koonin and Makarova 2017; Kofler 2019). CRISPR RNAs (Clustered Regularly Interspaced Palindromic Repeats, crRNAs) are produced from short fragments of foreign nucleic acids previously integrated into the repetitive CRISPR locus of the host genome. crRNAs assemble with specific CRISPR-associated proteins (Cas) proteins to form complexes that bind and cleave any phage or plasmid bearing sequence complementarity to the crRNA (Amitai and Sorek et al., 2016).piRNAs are a class of small RNAs of around 25-30 nt that guide PIWI proteins onto complementary target RNAs, resulting in gene silencing at the post-transcriptional or transcriptional level (Ozata et al., 2019). In D. melanogaster, piRNA precursors are generated from genomic loci, called piRNA clusters (Czech and Hannon, 2016). These regions are enriched for (remnants of) TE sequences and, as a consequence, cluster-derived piRNAs show sequence complementarity to TEs. It is thought that piRNA clusters act as traps for new TE invasions by horizontal transfer (Brennecke et al., 2007; Khurana et al., 2011;Parhad and Theurkauf, 2019). Thus, akin to CRISPR loci in prokaryotes for