c. 1500 BCE onward, in royal tombs
The twelve hours of the night
The sun does not set. It dies, travels through the underworld, unites with Osiris at midnight, and is reborn.
At dusk the sun enters the western horizon and boards the night barque. It travels through twelve hours, each a region with its own gates, guardians and inhabitants, and each requiring the correct names to pass. The dead are there, and the sun's arrival gives them an hour of light and air before it moves on and they return to darkness.
In the middle of the night the sun's ba and the body of Osiris unite. This is the pivot of the whole system: the sun is regenerated by joining, for an hour, with the god of the dead, and Osiris is animated by the sun's arrival. Neither could continue without the other.
In the seventh hour the serpent Apophis attacks, trying to stop the barque and swallow the water. He is bound, cut and speared by the gods aboard, Set prominent among them. He is never destroyed permanently. At dawn the sun emerges from the eastern horizon as Khepri, and the same thing has to happen again the following night.
Where it comes from
The Amduat
About this sourcec. 1500 BCE. A route map of the twelve hours, strictly royal for most of its history.
The Book of Gates and the Book of Caverns
New Kingdom. Alternative descriptions of the same journey, with different schemes and different inhabitants.
What the story was for
The most important idea in Egyptian religion, and the one that gets the least attention outside it. Order is not a state, it is a nightly achievement. The sun has to be helped through, the serpent has to be defeated again, and none of it is settled. That is why ritual mattered so much: the daily temple service was understood to be part of the work of keeping the world running, and this is also where Set spends most of Egyptian history, on the barque, killing Apophis, defending creation.
Other myths