The Cardenden author, 59, used the library regularly as a student and in his early years as an author, and wrote his PhD there, on fellow Edinburgh writer Muriel Spark.
The final scene of his first Rebus novel, Noughts and Crosses, was set in the tunnels beneath the library.
And the original typed manuscript for the 1989 thriller, along with train tickets, bank statements, and stories written in his schooldays, will now be stored in the archive.
It was gifted to the nation after being cleared out of his garage as the family moved to a smaller flat in the capital, where most of Mr Rankin’s books are set.
Despite having several Rebus novels published in the early 90s, Mr Rankin said he found fame late in his career.
He told how he was disheartened to browse Edinburgh bookshops and see none of his books for sale, until his breakthrough novel, Black and Blue.
Included in the 23 boxes donated, were letters from an American publisher urging for murder scenes to be moved forward, to page one.
Mr Rankin said: “The main emotion was looking at a life from the beginning, when I was totally unsure and I didn’t know what I was doing - a lot of trial and error.
“Looking back at young Ian and I wish I knew what I know now, that it’s going to be okay.”
Among the archive were unpublished short stories, scripts for TV shows which were never made, and letters exchanged with other writers, many of whom have now died.
Describing himself as a ‘natural hoarder’, Mr Rankin said: “Correspondence is going to be lost to the next generation of writers, not text and email but actual physical letters.”
He recalled listening for the sound of the postman’s motorbike arriving at the house in France where he lived with his wife for six years, hoping it would deliver a letter.
He exchanged letters with fellow Scots thriller writer, the late Iain Banks, before they met and became friends – and said he was astonished Banks had never read Trainspotting at the height of its notoriety.
But as well as crime writers such as PD James, Ruth Rendell and Val McDermid, who he also corresponded with, Mr Rankin revealed his own catholic taste in page-turners.
He added: “People sometimes find it odd that I’m a fan of Jilly Cooper, but I’m a fan of hers too.
“She would send me presents – a bar of posh soap, and a bottle of champagne, and tiny wee handwriting that went all the way around the card, and the margins of the card.”
As well as the manuscripts and letters, computer disks from an Amstrad were donated.
Letters from Mr Rankin’s early years discussed fears he would be dropped by his publisher, and that he was not selling enough books.
But teenage diaries and poems will go with him to his grave, Mr Rankin joked.
He said: “The first three novels were pretty much planned and written in this building.
“The closing scene of the first Rebus book was set in this building.
“Underground Edinburgh was what I was trying to write about from the get-go.
“It still feels like a Jekyll and Hyde city, that is cultured and civilised but has a darker underbelly.”
Mr Rankin is considered one of Scotland’s greatest living writers, and has written 30 books, most recently In a House of Lies.
But he said he considered his first novels to be ‘an apprenticeship’.
He said: “Rebus has been a way of me trying to make sense of life and mortality.
“There is a sense of using the character to try and deal with changes in Edinburgh, society changing, political life.
“He is looking around him, bewildered that things are changing so rapidly.”
After penning his first Rebus novels in Edinburgh, Mr Rankin moved to France – but found writing to be a form of ‘therapy’ when his youngest son was diagnosed with special needs.
The worried parents had to travel 50km to a hospital to speak to specialists they struggled to understand.
Mr Rankin recalled: “It was just rage – and I put all of that into Rebus. They were talking technical language in French, and we would drive back 50km.
“I would go into the attic, and suddenly I was God, and had complete control of the world.
“That was me just channelling all of that into the books. Writing can be a form of therapy.”