The government (aka public funding) funds all sorts of technology for national security and public health that could not be profitable for various reasons. Profit is NOT always necessary for a technology to benefit mankind and create good will.
NASA is merely an agency that hires various contractors at the lowest bid including SpaceX. Musk and SpaceX just have better marketing so they attracted more investors partially thanks to the internet (which was also created by the government for national security long before profitability). Most of SpaceX profit was from NASA contracts until the success of Starlink.
SpaceX utilized public domain and expired patented technology that was was funded by NASA. It cost many billions of taxpayer dollars to perform the destructive testing to obtain the data since no computers could model it. Further SpaceX engine developments since then are trade secrets to protect their profits so they are restricting future development by other entities even though they greatly benefited from public money. Musk did very similar with government electric car subsidies.
NASA I agree has been a laughing stock for decades due to ridiculous policies. Russian rocket engines were the most efficient in the world until 2019 release of the Raptor engine.
I cannot imagine, after COVID, what interest anyone would believe that government has in benefitting mankind or creating good will.
Musk has a vision that NASA does not. Clearly the profit-motive is better than anything the government can scare up.
I am comforted by the fact that, when Percival Lowell was documenting the Canals of Mars, he was wasting his own money.
I am not a Luddite. Today’s world is so small that one cannot wait for a WWII need for technological advancement. It will be too late. Having said that, there are severe limitations to government, I believe. For mankind to depend on it for benefit is a mistake.