One may only establish eruvei techumin for the purpose of a mitzvah. There is one who wishes to say that this applies to an eruv with bread, but with one's feet—one may establish an eruv even for a discretionary matter, (Beit Yosef and the Ri, beginning of chapter 8) and in a time of pressing need one may rely on this. (Eliyah Rabbah) And post facto, if one established an eruv for a discretionary matter, according to the opinion of the Bahag—it is not an eruv, and the Rambam in chapter 6 ruled—that it is an eruv, and the Rashba wrote to distinguish between one who establishes an eruv with bread and one who establishes an eruv with his feet, for with bread—his eruv is not an eruv, and with his feet—it is an eruv, (Beit Yosef) and our teacher, the Beit Yosef, ruled like the Rambam. And it is certain that when one established an eruv for the purpose of a mitzvah—he may go even for a discretionary matter.
The reason that one may only establish an eruv for the purpose of a mitzvah is simple, for it is well understood that eruvei chatzerot and shitufei mevo'ot—this itself is a mitzvah, so that we do not stumble into the prohibition of carrying. But regarding techumin, it is not relevant to say it is to avoid coming to a prohibition, and it is only due to necessity. Therefore, for the need of a mitzvah they permitted it, and not for a discretionary matter. And in truth, the intention is not for an absolute mitzvah, but even when there is in this the delight of Shabbat and Yom Tov—it is also called a mitzvah, as will be explained, for in this the sages also saw fit to permit, because it is a mitzvah, "and you shall call the Shabbat a delight," and on Yom Tov we were commanded in joy.
אין מערבין עירובי תחומין אלא לדבר מצוה. ויש מי שרוצה לומר דזהו בעירוב בפת, אבל ברגל - מערבין אפילו לדבר הרשות, (ב"י והר"י רפ"ח) ובשעת הדחק יש לסמוך על זה. (א"ר)ובדיעבד אם עירב לדבר הרשות, לדעת בה"ג - אינו עירוב, והרמב"ם בפרק ו' פסק - דהוי עירוב, והרשב"א כתב לחלק בין מערב בפת למערב ברגליו, דבפת - אין עירובו עירוב, וברגליו - הוי עירוב, (ב"י) ורבינו הב"י פסק כהרמב"ם. וזהו וודאי דכשעירב לדבר מצוה - יכול לילך אף לדבר הרשות. והטעם שאין מערבין אלא לדבר מצוה הוא פשוט, דבשלמא עירובי חצרות ושיתופי מבואות - זהו עצמו מצוה, כדי שלא נכשל באיסור טלטול. אבל בתחומין אין שייך שלא לבא לידי איסור, ואין זה אלא מפני הצורך. ולכן בצורך מצוה התירו, ולא בדבר של רשות. ובאמת אין הכוונה למצוה גמורה, אלא אפילו כשיש בזה עונג שבת ויום טוב - מקרי גם כן מצוה, כמו שיתבאר, דבזה ראו חכמים גם כן להתיר, מפני שזהו מצוה 'וקראת לשבת עונג', וביום טוב נצטוינו בשמחה.