‘I was not a cool kid,” admits Kumail Nanjiani. “There was no reason for me to be marginalised. I was just a nerd. I was kind of weird and I found solace in video games.” In a pop culture universe dominated by grown men who never quite outgrew their adolescent preoccupations, Nanjiani’s origin story is hardly unusual. The Pakistan-born comic, actor and writer spent his formative years huddled in his bedroom playing Golden Axe on his Sega Genesis, and becoming immersed to the point of obsession with The X-Files.

Since embarking on a career in comedy – he relocated to the US with his family when he was 18 – he has largely catered to audiences that share his particular enthusiasms. He has hosted a podcast about teenage video gaming (The Indoor Kids) and another recapping each season of The X-Files (The X-Files Files) that led to him being cast as an animal control officer on the revived series’ best-received episode, Mulder and Scully Meet the Were-Monster. He plays Dungeons & Dragons on Community creator Dan Harmon’s Harmontown podcast and had recurring roles on nerd-friendly TV shows such as Adventure Time and Portlandia. Since 2014, he has also been a series regular on Mike Judge’s Silicon Valley, now beginning its fourth season on Sky Atlantic, where he plays Dinesh, the deadpan computer programmer who is forever denied even the slightest sliver of happiness or success.

“I think he puts himself in these situations where he can’t succeed,” says Nanjiani. “If he was to get success, he would have to realise some things about himself. He’s like [Seinfeld character] George Costanza, and if George Costanza understood why he failed all the time he wouldn’t be that funny.”

X factor ... Nanjiani in Mulder & Scully Meet the Were-Monster. Photograph: Ed Araquel/Fox

These are unimpeachable nerd credentials and it would be understandable if Nanjiani were content to coast on them. But he isn’t. Instead, he has made the leap to leading man in a romantic comedy-drama that sold for $12m at this year’s Sundance festival. Variety hailed The Big Sick, the film he co-wrote and Judd Apatow produced, as “a perfectly charming film that morphs into something completely riveting”. At the recent CinemaCon event where studios preview their upcoming releases for American multiplex owners, Nanjiani described it as “the best movie by a Pakistani comedian and a girl in a coma coming out this year.”

The Big Sick is the best movie by a Pakistani comedian and a girl in a coma coming out this year Kumail Nanjiani

Love at first sight was not a factor in the real-life relationship between Nanjiani and his then-girlfriend Emily V Gordon. He was a struggling standup and she was a heckler. She was an open book and he was an insular guy. He hid the fact that he was dating a divorced, tatted, indie rock-loving white woman from his strict Muslim parents, who expected him to have an arranged marriage. She regarded his keeping her a secret as a massive red flag. After eight months of not-quite committing to each other, Gordon got a cold she couldn’t shake. It turned out to be adult-onset Still’s, a disease that resulted in her being placed in a medically induced coma for 12 days. Huddled around Gordon’s bedside, it was the first time the couple’s families, his from Karachi, hers from North Carolina, became aware of each other’s existences. “I was like, holy shit, I didn’t know this had happened,” says Michael Showalter, director of the movie Nanjiani and Gordon wrote about the experience (starring Nanjiani as himself and Zoe Kazan as Gordon). “I was just riveted by the story.”

After a month in hospital and successful lung surgery, the couple finally committed fully to each other, marrying in 2007 with the blessings of both sets of parents. At the risk of sounding indelicate, I ask Nanjiani how long after Gordon’s recovery did they start considering their joint trauma as material. “Oh my God, much later,” he exclaims. “When you’re going through it, it’s such a rough experience that it’s all-encompassing, it becomes your world. I’d go to the grocery store to get something to eat and I’d be like, ‘How is everybody in this store not concerned with Emily?’ It almost felt vulgar that they get to live their lives and go to work when this crazy huge thing was happening. I remember playing this Mario game on [the Nintendo] DS with Emily before she got sick and then I couldn’t play it after that. All the sounds reminded me of that time. I even got pissed at Mario.”

Casting couch ... Zoe Kazan and Nanjiani in The Big Sick. Photograph: Lionsgate

There’s a sizeable gulf between the nerd-friendly cameos and supporting roles that make up the majority of Nanjiani’s previous filmography and the demands of an emotionally complex leading role. Michael Showalter harboured no doubts he was up to the task. “I know Kumail’s a good actor, but I also knew he was really serious about finding it in himself to give the performance in this movie. I could see early on how hard he was working and how serious he was about it.” He was serious enough to write and perform an unsparing portrait of his younger self as a callow, self-centred nerd completely unprepared to deal with a life or death situation. “I don’t play myself in a way that’s self-aggrandising or pure in any way. In the movie, I make a lot of mistakes and shrug off a lot off responsibility and fuck up a lot of things, just like I did when I was going through that stuff.”

Nanjiani and Gordon spent two years relentlessly rewriting their script to Apatow’s satisfaction. “The process is: you write something, you send it to Judd, Judd tears it to pieces, you write it again. It’s a trying process.” I ask for an example of Apatow’s input. “He really pushed the emotional stuff and said you have to go to the places that are awkward and scary and the jokes will come from that. There was a long time when we weren’t talking about any religious stuff. My parents are from a different culture, they’re very religious Muslim people, and Judd said, ‘You have to engage with that aspect of the story.’ I said, ‘I don’t have anything to say about religion,’ and he said, ‘Just say that.’ You don’t have to have the answers, you just have to address the questions.”

The last significant romantic comedy to feature a loved one in a coma as its subject matter was 1995’s Sandra Bullock vehicle, While You Were Sleeping.

A vocal mob of movie industry insiders have been making the claim that the romantic comedy genre itself has been in a coma for the best part of a decade. But maybe it isn’t. Maybe it now rests in the hands of people about whom Nancy Meyers, director of What Women Want and the genre’s reigning monarch, didn’t know existed. Look at The Mindy Project, Master of None and The Big Sick. That’s a giant leap from Sandra Bullock. That’s a sign that, even in an industry as risk-averse as Hollywood, something is changing. “I think the romantic comedy as being the domain of the bourgeois, white good-looking person doesn’t feel right any more,” says Showalter.

‘The romantic comedy as the domain of the bourgeois, white good-looking person doesn’t feel right any more’ ... Kumail Nanjiani. Photograph: Matt Winkelmeyer/Getty Images for SXSW

For Nanjiani, the change goes beyond romantic comedy. “It was so exciting to me when I saw Rogue One and I saw Riz [Ahmed] and the good guys were sort of dressed like Middle-Eastern people and it meant so much to me, emotionally. It was unpredictable the way I reacted. It’s something I never thought would happen. For a long time, there was one famous American brown actor. That was Kal Penn [Harold & Kumar]. When I first started doing standup, when people wanted to be racist to me, they would call me him, and now there’s Aziz [Ansari] and Riz and Dev Patel – there’s like a bunch of famous brown peers.”

Bringing up Kal Penn reminds me of the actor’s recent tweet about the roles for which he used to have to audition; roles that ran the gamut from offensive racial stereotype to really offensive racial stereotype. Nanjiani groans with familiarity. “I started auditioning for roles in 2009 and I would see stuff like that all the time. It was shocking to me. Some of these things were years ago and they were really, really awful. There would always be a script about an Indian man and a white woman and they never worked. They were so stereotypical.”

Despite Hollywood’s attempts to become a more inclusive employer, depictions of mixed-race couples in movies are still relatively rare occurrences. “The world is getting smaller,” says Nanjiani. “And people are bumping up against people

from different parts of the world with very different points of view. The challenge of our time is going to be: how do you allow other points of view to exist within what you traditionally see as your world?” The reaction to that challenge right now is, he says, “‘Let’s take America back to the way it was.’ But that’s a coded way of saying, ‘Let’s go back to when we didn’t have to worry about all the different points of view.’ And I think interracial relationships is a very interesting way to show different cultures learning to adapt to each other. I hope that you would see more of it because it really is such relevant thing to right now.”

Silicon Valley starts on 24 April on Sky Atlantic; The Big Sick is in cinemas from 7 July