Judd Apatow heard about Nanjiani’s life and said, “That should be a movie.” Photograph by Nathanael Turner for The New Yorker

In 2009, on the “Late Show with David Letterman,” the comedian Kumail Nanjiani walked onstage, wearing a boxy black suit and a cordless mike, to do a standup set. The band played a few bars of “Born in the U.S.A.,” an allusion, presumably, to the fact that he wasn’t. The first anecdote of Nanjiani’s set fell flat. He stood stiffly, swallowing hard, his hands clasped tightly in front of his chest. Then he told a joke about theme-park attractions with excessively convoluted backstories. “It’s like a story line to a porn movie,” he said. “I really don’t care what all your professions are. I’m just here for the ride.” It wasn’t the cleverest punch line in Nanjiani’s act, but it received a big laugh and a ten-second applause break. He exhaled audibly, relaxing his hands. His next bit was about the Cyclone, the rickety roller coaster on Coney Island. “The Cyclone was made in the year 1927! Let that sink in. They should change the name of that ride to 1927, ’cause that fact is way scarier than any cyclone,” he said. “And the whole thing is made of wood . . . you know, that indestructible substance that NASA uses for its space shuttles.” The bit could have been delivered in the nineteen-sixties, by Woody Allen or Mort Sahl, with one exception: Nanjiani said the ride was “the scariest experience of my life—and I grew up in Pakistan.”

Nanjiani spent his childhood in Karachi, Pakistan’s biggest city. In 1997, when he was nineteen, he left to attend Grinnell College, a small liberal-arts school in the middle of Iowa. “I thought, from watching TV and stuff, that America was one place,” he told me. “They only show you L.A. and New York. They don’t warn you about Iowa.” When he got to college, he says, “I was super shy, but I learned that my friends thought I was funny.” His senior year, there was an open mike on campus, and his friends urged him to try standup. He performed for thirty-five minutes. “I don’t think I’ve ever done better than that crowd, reaction-wise,” he said. “Of course, it was full of people who knew me. But it gave me an irrational amount of confidence.” After school, he moved to Chicago and started performing. Michael Showalter, a comedian and director who has admired Nanjiani from the beginning, told me, “Anyone who saw him saw how smart and fresh his voice was. The question wasn’t whether he’d be successful, only which direction he’d choose to go in.”

The year of the Letterman set, Nanjiani landed a recurring role on “The Colbert Report,” as a Guantánamo detainee who lives under Stephen Colbert’s desk. Many of Nanjiani’s earliest film and TV credits were, he says, “more or less what you’d expect”: “Delivery Guy,” “Cable Guy,” “Pakistani Chef.” But he quickly started getting more substantial roles, and in the past few years he has appeared on almost every show beloved by comedy snobs, including “Portlandia,” “Broad City,” “Community,” “Key & Peele,” and “Inside Amy Schumer.” He now has a lead part on “Silicon Valley,” an ensemble comedy on HBO, playing a coder who, despite his good looks, remains hopelessly unlucky with women. “It’s a version of me in high school, when I was at my least confident,” he said.

As a child, Nanjiani spoke Urdu at home; he learned English at school, and picked up colloquialisms from TV. “I grew up watching ‘Ghostbusters’ and ‘Knight Rider’ and Hot Wheels commercials,” he said. “When I got to college, having never set foot in America, I knew more American pop-culture references than my friends did.” As a standup, he said, “I was so eager to avoid being known as an immigrant comedian, or as a Muslim comedian, that I would just come out wearing a T-shirt and start talking about video games. I wasn’t judgmental about other comedians using their backgrounds to their advantage—joining the Spicy Masala Comedy Tour, or whatever—but I could never bring myself to do it, even though I could have used the work.”

Then came 9/11. “Suddenly, Islam was the elephant in the room,” he continued. “I just thought, O.K., I’m brown, I speak with an accent—I have to at least bring it up.” He began opening his sets by saying, “Don’t worry, I’m one of the good ones,” which put some audiences at ease. Other times, he was interrupted by someone shouting “Go home!” or “Go back to the Taliban!” Recalling one heckler, at a club in Milwaukee, Nanjiani said, “The room got so quiet and awkward. I fumbled around with words and tried to ignore it. It made the audience pity me, which is not a good look for comedy. After that, I came up with something to say—I realized it doesn’t have to be a perfect line, just something to show the audience that you’re still in control.” The next time he was heckled, he responded, “That guy’s right. I am a terrorist. I just do standup comedy on the side, to keep a low profile.”

A similar exchange, with “Taliban” updated to “ISIS,” appears in Nanjiani’s movie “The Big Sick.” It premièred earlier this year, at the Sundance Film Festival, where it was a favorite among both audiences and critics. The movie was directed by Showalter, whose film career has included slapstick cult classics (“Wet Hot American Summer”) as well as offbeat romantic comedies (“Hello, My Name Is Doris”), and produced by Judd Apatow, who has specialized, recently, in helping almost famous comedians adapt their formative experiences into memoiristic meta-comedies. Apatow’s producing partner, Barry Mendel, described “The Big Sick” to me as “part comedy about comedy, part drama about families, part medical mystery, and also, incidentally, a Muslim American rom-com.”

Nanjiani co-wrote the screenplay with his wife, Emily V. Gordon, and he plays its protagonist, a standup comic named Kumail. It’s the first feature either of them has written, and it’s Nanjiani’s first starring role. The fictional Kumail works as an Uber driver, a day job that didn’t exist when the real Kumail still had day jobs. Aside from that, and a few other departures to help a joke land or a plotline cohere, the movie doesn’t stray too far from a dramatically rich series of events that befell Gordon and Nanjiani a decade ago, shortly before they turned thirty.

Nanjiani didn’t conceive of the film as at all political. “It was just supposed to be a heartwarming little movie that, if we did it right, would be funny and maybe a bit poignant,” he said. But it was filmed last summer, when much of the conversation between takes was, inevitably, about the Presidential campaign; the Sundance première was on January 20th, the day Donald Trump was sworn in. “That coincidence is so weird and terrible that I don’t even know what to make of it,” Nanjiani told me. (On Twitter, where he has more than a million followers, he makes no secret of his political opinions: “I’m thankful our new President-elect is anti-Muslim so now my parents & I agree on politics”; “Silver lining: one day the ocean will take us.”)