remember before the gvt pumped money into his companies they were on the brink of bankruptcy. once they got the nasa contract spacex immediately got profitable. which allowed musk to use spacex money to loan to tesla. n also use the govt loan to tesla to make the save the company.
gist of the story - the money is in gvt. same applies to thiel.
But you're right that government contracts are the way you truly get rich. A lot of people don't realize California's biggest export is airplanes and airplane parts (a business run on gov't contracts) not Silicon Valley. All of Musk's ventures (Tesla, SpaceX, Boring, solar, etc) rely on heavy subsidization/contracts with gov'ts. All of his ventures are some of the most heavily subsidized companies in the US[0] and he's been sued in several countries over subsidy fraud
[0] https://subsidytracker.goodjobsfirst.org/parent/tesla-inc