The Roosevelt administration sued farmers who attempted to grow food in violation of a law passed by Congress to regulate interstate commerce, in this case to stabilize crop prices.
The Supreme Court ruled unanimously in Wickard v. Filburn that this was constitutional.
The interesting part is how they found "interstate commerce" in chicken feed which wasn't actually sold and didn't actually leave the state (didn't leave the farm even):
The Court decided that Filburn's wheat growing activities reduced the amount of wheat he would buy for chicken feed on the open market, and because wheat was traded nationally, Filburn's production of more wheat than he was allotted was affecting interstate commerce. Thus, Filburn's production could be regulated by the federal government.
By this reasoning, even free open-source software is interstate commerce, if it has a commercial competitor. Imagine a federal law restricting FOSS, as part of an economic stimulus for the software industry.
(edit: My FOSS analogy is actually too weak! You wouldn't need to distribute anything. It'd be enough to write your own code, for your personal use, if this prevents you from purchasing some commercial product).
It's a tricky discussion in this thread because we're mixing together the questions of "Is this allowed (constitutional)?" and "Is this a good idea?" I believe that in both the Filburn case and in your hypothetical FOSS example, that yes, Congress has the power to regulate them.
But that doesn't mean it's a good idea. At some point, the elected representatives in Congress have to make a law to regulate the interstate commerce. In Filburn's case, a majority of the democratically-elected officials in Congress decided that price stabilization of crops was important enough to restrict growing "private stashes" that weren't going to be sold. If supporters of Filburn didn't agree with that, then they would have to elect new representatives to do overturn the law. (Which by the way, will probably finally happen sometime in the next decade.) But there is zero support in Congress for restricting FOSS, even when it competes with commercial software. The benefits of FOSS to all Americans are so obvious that it won't happen.
This in turn led to the Raich case where the Supreme Court ruled the government could prohibit state-permitted personal private activity on the grounds that it affected interstate commerce by reducing demand for a federally illegal product.
The Supreme Court ruled unanimously in Wickard v. Filburn that this was constitutional.