Praise be to Allah.
There is nothing wrong with making part of the building shops and so on, or with making all of it a mosque, according to what you think is best and what suits the needs of the worshippers.
If the space is large, and is more than people actually need, perhaps it is good to make part of it shops, the returns on which could be spent on the mosque and educational and da‘wah activities. If these shops are made into a waqf (endowment) for that purpose, that would be very good.
It says in al-Mawsoo‘ah al-Fiqhiyyah (12/295): The Shaafa‘is, Maalikis and Hanbalis regarded it as permissible to make the upper floor of a building into a mosque and not the lower floor, or vice versa, because both of them could be made into a waqf. So it is permissible to make one of them into a waqf and not the other. End quote.
It says in Fataawa al-Lajnah ad-Daa’imah (5/220 – vol. 2): There is nothing wrong with the mosque being underneath apartments, if the mosque and the apartments are built in this manner from the outset, or the decision was made later on to make a mosque underneath the apartments. But if the decision to build apartments over the mosque was made after the mosque was designated as such, that is not permissible, because the roof of the mosque and everything on top of it belongs to the mosque. End quote.
It is not permissible to buy and sell in the mosque, and any such transactions are not valid, because of the report narrated by Ahmad (6676), Abu Dawood (1097) and an-Nasaa’i (714) from ‘Amr ibn Shu‘ayb from his father from his grandfather who said: The Messenger of Allah (blessings and peace of Allah be upon him) forbade buying and selling in the mosque.
This hadith was classed as hasan by al-Albaani in Saheeh Abi Dawood, and by Shu‘ayb al-Arna’oot in Tahqeeq al-Musnad.
At-Tirmidhi (1321) narrated from Abu Hurayrah that the Messenger of Allah (blessings and peace of Allah be upon him) said: “If you see someone buying or selling in the mosque, then say: ‘May Allah not make your trade profitable!’ and if you see someone enquiring after lost property in the mosque, then say: ‘May Allah not return it to you!’”
This hadith was classed as saheeh by al-Albaani in Saheeh at-Tirmidhi.
Al-Bahooti (may Allah have mercy on him) said in Kashshaaf al-Qinaa‘ (2/366):
It is haraam to buy and sell, and rent or hire, in the mosque, because these are all types of transactions. That is haraam for one who is observing i‘tikaaf and for others. It appears that this is the case whether the amount sold is small or great, and whether there is a need for it or not, firstly because of the hadith of ‘Amr ibn Shu‘ayb, from his father, from his grandfather, who said: The Prophet (blessings and peace of Allah be upon him) forbade buying and selling, and reciting poetry, in the mosque. Narrated by Ahmad, Abu Dawood and an-Nasaa’i, and by at-Tirmidhi, who classed it as hasan.
‘Imraan al-Qaseer saw a man who was selling something in the mosque, and he said: Hey you! This is the marketplace of the hereafter; if you want to sell something, go out to the marketplace of this world.
If he buys and sells in the mosque, then [the transaction] is invalid. Ahmad said: Rather these are the houses of Allah, and nothing is to be bought or sold in them.
Abu Haneefah regarded it as permissible to sell, and that was also regarded as permissible but disliked (makrooh) by Maalik and ash-Shaafa‘i. End quote.
So it is essential to set up a boundary for the mosque, so that no one will buy and sell within that boundary; so that menstruating women and anyone who is in a state of janaabah may avoid it; so that no one will enter it without praying tahiyyat al-masjid (two rak‘ahs to “greet the mosque”); so that its sanctity and high status will be known and people will not mess about in it; and other rulings.
The boundary may be marked by a high or low wall, or by the carpets and the like that distinguish the mosque from the rest of the place that is not a mosque.
If buying and selling will take place close to the mosque, then building a complete wall is better, so as to prevent any disturbance to those who are praying and reading Qur’an.
And Allah knows best.