סימן יג סעיף ד
אָסוּר לֶאֱכֹל אוֹ לִשְׁתֹּת אוֹ לִישֹׁן בָּהֶם אֲפִלּוּ שְׁנַת עֲרַאי. וּלְצֹרֶךְ מִצְוָה כְּגוֹן בְּלֵיל יוֹם הַכִּפּוּרִים מֻתָּר לִישֹׁן, אַךְ יִתְרַחֵק מִן אֲרוֹן הַקֹּדֶשׁ. וְכֵן לֶאֱכֹל שָׁם לְצֹרֶךְ מִצְוָה, סְעוּדָה שֶׁאֵין בָּהּ שִׁכְרוּת וְלֹא קַלּוּת רֹאשׁ, מֻתָּר. וְכֵן אוֹתָם אֲנָשִׁים שֶׁלּוֹמְדִים שָׁם בִּקְבִיעוּת, מֻתָּרִים לֶאֱכֹל וְלִישֹׁן שָׁם אֲפִלּוּ שְׁנַת קֶבַע, שֶׁלֹּא יִתְבַּטְּלוּ מִלִּמּוּדָם (סִימָן קנ"א תרי"ט וביו"ד סִימָן רמ"ו).
It is forbidden to eat or drink or sleep in a holy place, even if it is only a short nap.3However in a Beis Midrash these things are permitted. However, for the sake of a mitzvah, for example, if you wish to spend Yom Kippur night [in the synagogue,] it is permissible to sleep there, but you should keep a distance from the Aron HaKodesh. Similarly, eating there for the sake of a mitzvah, provided there is no drunkenness or levity connected with it, is permitted. Likewise, those who study regularly in the synagogue are permitted to eat there and to sleep normally, in order not to lose time from their studies.
תנא דבי אליהו כל השונה הלכות בכל יום מובטח לו שהוא בן העולם הבא שנאמר הליכות עולם לו אל תקרי הליכות אלא הלכות (נדה ע"ג.)