Usually in the switch box or in the ceiling fitting, as these are typically the easiest places to access the wiring needed.
(There will likely be a fire safety issue if you try to dig out part of the stud and put it there. It’s an electrical device on a live circuit.)
With regard to the original question, the following thread should be of interest: