Alexei Navalny knew he would likely die in prison.

In messages to his supporters posted on social media, Navalny, the Russian opposition leader, often struck a hopeful note about the future of his country, or a comic one, joking about the absurdities and indignities of prison life.

But in the journal entries he managed to write and smuggle out of prison, he was more introspective, and blunt: “I knew from the outset that I would be imprisoned for life — either the rest of my life or until the end of the life of this regime,” Navalny wrote in his diary in March 2022. “I will spend the rest of my life in prison and die here.”

He reflected on what that would mean: missing birthdays, anniversaries, his children’s graduations. Never meeting his grandchildren. The thought made him want to scream and smash things, he wrote. But then he thought of other Russian dissidents who had suffered similar fates. “I resigned myself and accept it,” he wrote.

Those passages appear near the end of “Patriot,” a posthumous memoir by Navalny that published Tuesday, eight months after his death at age 47 in an Arctic penal colony.

“Patriot” is by turns funny, fiery, reflective and tragic, laced with Navalny’s trademark wry humor and idealism. Even from his prison cell, he takes obvious delight in attacking Russian President Vladimir Putin. It’s also a gutting personal account from a husband and father facing the reality that he will never be with his family again, that Putin might succeed in silencing him and that the sacrifices he made to oppose authoritarianism and corruption will have grave consequences for the people he loves most.

The memoir, being released in the United States by Knopf, was pieced together after his death with the help of Yulia Navalnaya, Navalny’s widow. It will be published in 22 languages, including Russian. For Navalnaya, releasing the memoir is a way to instill hope in the struggling Russian opposition movement, and to keep her husband present in the world.

“It’s very important for me to keep his legacy alive, to bring his voice to as many people as possible,” Navalnaya said in a video interview last Wednesday, from a location in Europe that she couldn’t disclose because of security concerns. “When you lose somebody who’s very close to you, you want everyone to remember him.”When Navalny flew back to Moscow from Berlin in January 2021, he was arrested at the airport, and later faced a series of trials on charges of extremism and other offenses that his supporters say were manufactured by the state. He wrote the rest of the memoir in prison.There are moments in the memoir when Navalny, who publicly projected fearlessness, confesses to feeling frightened, if not for himself then for his family: “The thought fills me with dread that Novichok could be put on my door handle and that my son or my daughter could touch the door handle,” he wrote.At times, Navalny despaired at the turn his story had taken. “I’m lamenting that my book, originally an autobiography with an intriguing thriller about uncovering an assassination attempt using chemical weapons, has turned into a prison diary,” he wrote. “It’s a genre so saturated with clichés that it’s impossible not to write them.”

He griped about how hard it was to write — “You are kept constantly busy. There is no time to read, let alone write” — and explained why he felt he needed to record his memories and experience.

“If they do finally whack me, the book will be my memorial,” he wrote Oct. 21, 2021.

At one point, he joked that his death could boost book sales: “The book’s author has been murdered by a villainous president; what more could the marketing department ask for?”

Navalnaya said it was devastating at times to read her husband’s descriptions of life in prison. He was kept in isolation cells, tormented with sleep deprivation and denied medical care when he had crippling back pain and lost sensation in his legs — treatment that he protested by going on a hunger strike.

In a diary entry dated May 2, 2021, Navalny describes how, after he had ended his 24-day hunger strike and was trying to recover his strength, his cellmate offered him an apple. But the guards wouldn’t let him have it.

“It’s very difficult to read these things,” Navalnaya said. “I don’t know how to express the feeling — it’s like your heart is squeezing.”

But Navalnaya said she was also often heartened by his dispatches, because they captured the man she loved — ironic, jovial, defiant.

“It’s difficult, but at the same time, it’s inspiring, because he’s very honest in sharing these thoughts,” she said. “He’s writing about these awful things, but on the other hand there’s this sense of humor.”

Even though Navalny faced restrictive conditions in prison, he found ways to communicate with the outside world. He was able to send hundreds of handwritten letters to friends and family, and to get dispatches to members of his team, who then posted them on social media.

In a diary entry from the fall of 2021, Navalny wrote about how it was becoming more difficult to get his messages and memoir pages out. “Everything that I write and keep, or take along when I meet my lawyer,” he wrote, “is both attentively read and photographed by my keepers.”

He then described a plan to pass on notebooks during court proceedings. “I had to devise a whole clandestine operation to bamboozle the guards, involving the substitution of identical notebooks bought specifically for the purpose.”

Sometimes, six months would pass before Navalny was able to get his journals out, Navalnaya said.

Some material was confiscated and never returned. And it’s possible there was more writing that was never recovered after his death — none of his personal belongings were given to his family, Navalnaya said.

There’s also a gap in the memoir that shows how eventually, it became even harder for Navalny to write. As the prison conditions grew harsher, he was only allowed a pen and paper for half an hour each day. The final diary entry in the book is from Sept. 2, 2022.

Navalny’s experience over the remaining months is compiled from his social media dispatches, which he continued to write until shortly before his death in February of this year.

Navalnaya said she sometimes wondered how their lives would have turned out if they had not gone back to Russia. “I would be lying if I answered that I never thought about it,” she said. His death has been devastating for her and their children, Daria and Zakhar. “We were a very full, happy family and it just ended one day,” she said.

Still, she doesn’t regret his decision. “I had no doubt he was doing the right thing,” she said.