There is no rule about the required exact distance from the line of scrimmage. It can be taken from the line itself or behind. They kick from 7 yards behind the line primarily because they're used to it.
The only rule covering this is Rule 11.4.1 Successful Field Goal (link):
a) The kick must be a placekick or dropkick made by the offense from on
or behind the line of scrimmage or from the spot of a fair catch (fair
catch kick). If a fair catch is made or awarded outside the inbound
line, the spot of the kick is the nearest inbound line.
(...)
However, because Field Goal trys have little to no margin for errors the players will do anything they can to succeed. This includes a fair distance from the LoS and always the same distance whenever possible...
- A shorter distance may get the ball blocked while a longer distance is harder to snap and an unnecessary extra distance for the kick.
- The snap needs to be very precise in order to give the holder the chance to place the ball correctly before the kicker arrives. That would be a difficult task if the distance is varying.
TL;DR:
It's allowed to use other distances, but it would be an unnecessary risk (and a lot of extra training work). That's why they don't do that.