Revealing the connectivity of functionally identified individual neurons is necessary to understand how activity patterns emerge and support behaviour. Yet the brain-wide presynaptic wiring rules that lay the foundation for the functional selectivity of individual neurons remain largely unexplored. Cortical neurons, even in primary sensory cortex, are heterogeneous in their selectivity, not only to sensory stimuli but also to multiple aspects of behaviour. Here, to investigate presynaptic connectivity rules underlying the selectivity of pyramidal neurons to behavioural state1, 2, 3, 4, 5, 6, 7, 8, 9-10 in primary somatosensory cortex (S1), we used two-photon calcium imaging, neuropharmacology, single-cell-based monosynaptic input tracing and optogenetics. We show that behavioural state-dependent activity patterns are stable over time. These are minimally affected by direct neuromodulatory inputs and are driven primarily by glutamatergic inputs. Analysis of brain-wide presynaptic networks of individual neurons with distinct behavioural state-dependent activity profiles revealed that although behavioural state-related and behavioural state-unrelated neurons shared a similar pattern of local inputs within S1, their long-range glutamatergic inputs differed. Individual cortical neurons, irrespective of their functional properties, received converging inputs from the main S1-projecting areas. Yet neurons that tracked behavioural state received a smaller proportion of motor cortical inputs and a larger proportion of thalamic inputs. Optogenetic suppression of thalamic inputs reduced behavioural state-dependent activity in S1, but this activity was not externally driven. Our results reveal distinct long-range glutamatergic inputs as a substrate for preconfigured network dynamics associated with behavioural state.