Both spelling are correct since they stand for at least two different parts of speech.
AdverbAs an adverb, нахуй should be spelledwritten as a solidone word like as any other adverb in Russian.
It may stand for ‘totally’, ‘entirely’can mean totally, entirely.
Как ты заебала, заткни на́хуй свое ебало! (song by ‘Evil Not Alone’the band Evil Not Alone)
Or along withas a variant of нахуя for ‘why’meaning why?, ‘what for’.what for?
На́хуй вы вообще приперлись? (trivial phrase)
Нахуя́ козе баян? (proverb)
A noun хуй withpreceded by the preposition на undoubtedly should invariably be spelledwritten separately regardless of what sense it is used in.
In the literal sense (хуй = penis):
Мой парень надел на хуй стаканчик и стал им болтать, чтобы получить звук трещетки... (random quote from google)
In the positive metaphorical sense:
И рыбку съесть, и на́ хуй сесть. (proverb)
In negative metaphorical sense:
Иди на́ хуй! (very common idiom)
Also it may be used as interjection:
Я попью а, нахуй, всё, пиздец, не могу больше. (‘Зеленый слоник’from the movie ‘Зеленый слоник’)
I tend to think it should be spelledwritten as one solid word, but I can’t give any rationale now.
As for the causes for the solid misspelling, I do notdon't think they are inhave anything to do with the stress atbeing on на. Stress on preposition isStressed prepositions are still quite usual in Russian: лечь на́ пол, зайти за́ угол, взять работу на́ дом. ItIt's most likely by analogy towith directional adverbs such as направо, налево, вперед, etc. There isThat would make a certain sense it in. However, на хуй in иди на хуй is considered as noun with preposition by the most of competent people.
There isThere's an empirical rule: if you still can separate two parts ofbreak a stable phrase up by addingsticking an adjective or somehow elsesome other form between its parts, then they arethey're two separate words. You definitely can describedefinitely modify хуй with an adjective:
Иди ты на большой и толстый хуй!
You can even omit it:
Иди ты на большой и толстый! (really in use)
Try to describe право or лево in направо / налево and you will feelyou'll see the difference.
All of the above is also applicable to numerous euphemisms of хуй: на́ фиг / на́фиг (нафига́), на́ хрен / на́хрен (нахрена́), на́ хер / на́хер (нахера́).