Abstract Legumes play critical roles in agroecosystems by modulating nitrogen-fixing microorganisms to enhance soil fertility and promote crop productivity. Current research on the effects of legumes predominantly focuses on surface soil, lacking a comprehensive analysis of their overall impact across multiple soil layers and an in-depth understanding of associated microbial mechanisms. Here, the community structure of soil nitrogen-fixing microorganisms in three soil layers (0-20, 20-50, and 50-100 cm) under legume and non-legume cultivation was investigated through metagenomic sequencing. We found that only in topsoil (0-20 cm) legume treatment exhibited a significantly higher relative abundance of nitrogen-fixing genes than non-legume treatment. Under legume cultivation, the relative abundance of nitrogen-fixing genes was significantly higher in the topsoil layer than in deeper layers, whereas non-legume treatment displayed an inverse depth-dependent pattern. Combining soil physicochemical properties, the relative abundance of nitrogen-fixing genes correlated significantly with soil moisture, total carbon (TC), and dissolved organic carbon (DOC) content. Both TC and DOC were identified as key drivers of these genes. Subsequently, a similar depth-dependent pattern within the relative abundance of soil carbon degradation genes was found in response to the cultivation of both crops. The relative abundances of soil carbon degradation genes were negatively correlated with nitrogen-fixing genes under legume treatment individually, distinct from non-legume treatment. Our findings highlight the depth-dependent impact of legumes on nitrogen fixation and the critical interaction between soil carbon degradation and nitrogen fixation, providing insights into carbon management in legume cultivation practices to enhance nitrogen fixation in future agriculture.