We develop soft and stretchable fatigue-resistant hydrogel optical fibers that enable optogenetic modulation of peripheral nerves in naturally behaving animals during persistent locomotion. The formation of polymeric nanocrystalline domains within the hydrogels yields fibers with low optical losses of 1.07 dB cm−1, Young’s modulus of 1.6 MPa, stretchability of 200% and fatigue strength of 1.4 MPa against 30,000 stretch cycles. The hydrogel fibers permitted light delivery to the sciatic nerve, optogenetically activating hindlimb muscles in Thy1::ChR2 mice during 6-week voluntary wheel running assays while experiencing repeated deformation. The fibers additionally enabled optical inhibition of pain hypersensitivity in an inflammatory model in TRPV1::NpHR mice over an 8-week period. Our hydrogel fibers offer a motion-adaptable and robust solution to peripheral nerve optogenetics, facilitating the investigation of somatosensation.