FreeBSD considers Rust in the base system
The FreeBSD Project is, for the second time this year, engaging in a long-running discussion about the possibility of including Rust in its base system. The sequel to the first discussion included some work by Alan Somers to show what it might look like to use Rust code in the base tree. Support for Rust code does not appear much closer to being included in FreeBSD's base system, but the conversation has been enlightening.
Base versus ports
Unlike Linux, the FreeBSD operating system kernel
and user space are developed together as the base system, maintained in the FreeBSD source
tree (often referred to as "src"). This means, for the
purposes of discussing using Rust as a language for the FreeBSD kernel
or other programs/utilities in the base system, the Rust toolchain
would need to be present in base as well. Currently, the languages
supported for FreeBSD's base system are assembly, C,
FreeBSD also has a ports collection for third-party software that is not maintained as part of FreeBSD itself. This includes everything from the Apache HTTP Server to Xwayland. Rust is already present in the ports system, as are many applications written in the Rust language. A search on FreshPorts, which lists new packages in the ports collection, turns up more than 500 packages in the ports system that are written in Rust.
A dream of Rust
But Rust is not allowed in the base system. Somers noted
this fact with disappointment in a discussion about a
commit to fusefs tests in January. Enji Cooper asked
Somers why he had not used smart pointers in the change, to which
Somers said "it's because I'm not real great with
". He added that he had stopped trying to improve
his
Even when I wrote these tests in 2019, I strongly considered using Rust instead ofC++ . In the end, the only thing that forced me to useC++ is because I wanted them to live in the base system, rather than in ports.
Somers said that he dreamed about the day when Rust is allowed in
the base system, and mentioned several projects he would have done
differently if it were allowed. Warner Losh replied
that it would require some visible success stories for the FreeBSD
community to consider inclusion in base. Rust, he said, "has a lot
of logistical issues since it isn't quite supported in llvm out of the
box
". (By default, Rust uses
its own fork of
LLVM.) He suggested adding test cases in base that could be run by
installing Rust from ports prior to building "to show it's possible
and to raise awareness of rust's viability and to shake out the
inevitable growing pains that this will necessarily [cause]
".
Brooks Davis also suggested
bringing in Rust code that would use an external toolchain rather than
having the toolchain in base. "There
are a bunch of bootstrapping and reproducibility issues to consider,
but I think the fixes mostly lie outside the src tree.
"
The case for (and against) Rust
On January 20, Somers made his case to the freebsd-hackers mailing list on the costs and
benefits of including Rust code in FreeBSD's base system, which opened
the first lengthy discussion around Rust. He
summarized the cost as "it would double our build times
", but
the benefit would be that "some tools would become easier to write,
or even become possible
". Losh reiterated
his suggestion to start with adding better tests in Rust. That would
allow the project to get an external toolchain working to learn if
Rust "actually fits in and if we can keep up the
infrastructure
" or not.
Dimitry Andric wrote
that it might be possible to build Rust using FreeBSD's base version
of LLVM, but he said that the discussion was going in the wrong direction. The
upstream build systems for LLVM and Rust require too many
dependencies. Trying to support such tools "is getting more and
more complicated all the time
". He wanted to know why the project
was spending time trying to build toolchain components in the base
system at all. He argued, instead, that the focus should be on
removing toolchains from the base system. "Let new
toolchain components for base live in ports, please.
" In other
words, software written in Rust could live in base, but its toolchain
would stay in ports. That sentiment was shared by a number of other participants in the
discussion.
Alexander Leidinger asked
what kind of impact using Rust would have on backward
compatibility. Would it be possible to compile FreeBSD release x.0 in
two years on version x.2, for example, as it is with C/
So if we were to bring Rust code into the base system, we would probably want to settle on a single edition per stable branch. Then we would be able to compile it forever.
Some participants were not convinced that code written in Rust should be allowed in
base, even if its toolchain lived outside base. Cy Schubert complained
that the language was still evolving quickly. Poul-Henning Kamp said
that the pro-Rust argument simply boiled down to "'all the cool
kids do it'
". He argued that FreeBSD developers should "quietly
and gradually look more and more to
":
I also propose, that next time somebody advocates for importing some "all the cool kids are doing it language" or other, we refuse to even look at their proposal, until they have proven their skill in, and dedication to, the language, by faithfully reimplementing cvsup in it, and documented how and why it is a better language for that, than Modula-3 was.
Bertrand Petit agreed
with Kamp, and said that adding Rust to base should be avoided at all
costs. However, he suggested that if using Rust needs something in
base "such as kernel interfaces, linking facilities, etc.
" to
work properly in the ports system, it should be provided in base.
After the discussion had simmered a bit, Somers replied
with answers to some of the questions about
Rust in base. He said that the comparisons of Rust to Perl were
missing the mark. The crucial difference is that Rust is
suitable for systems programming, while the others were not. "Rust
isn't quite as low-level as C, but it's in about the same position as
" To Kamp's assertion that developers should just use In general, it feels like
"
Out of the blue
Ultimately, the discussion trailed off in early February without any concrete plan of adopting Rust. The thread was re-awakened by Shawn Webb on July 31. Webb replied to Somers' original email with the news that the Defense Advanced Research Projects Agency (DARPA) is investigating a program to automate rewriting C code to Rust, called Translating All C to Rust (TRACTOR).
Losh said
that he was still waiting for someone to take him up on the suggestion to
do build-system integration for Rust tests. "Since the Rust
advocates can't get even this basic step done for review, it's going
to be impossible to have Rust in the base.
" Webb replied
that he would be willing to find time in September to work on build
system integration, if Losh was willing to mentor him, which Losh
agreed to do.
Konstantin Belousov replied that it would be better to focus on what useful things could be implemented in Rust, rather than how to integrate code into the build. That caused Kamp to interject with a history lesson about the failures of importing Perl into FreeBSD's base system.
The choice to bring Perl into the base system, he said, was based
on arguments identical to those being made for Rust.
The project overlooked the fact that Perl was more than a
programming language, it was an ecosystem with a "rapidly exploding
number of Perl Modules
". The goals of rewriting things in Perl
went unrealized, he said, once developers realized that FreeBSD base
only offered Perl the language and not Perl the ecosystem:
Having Perl in the tree was a net loss, and a big loss, because it created a version gap between "real Perl" and "freebsd Perl", a gap which kept growing larger over time as [enthusiasm] for Perl in the tree evaporated.
Adding Rust to FreeBSD will be the /exact/ same thing!
That left two options, Kamp said. The first is a FreeBSD Rust only intended for
use in base without benefit of the Rust ecosystem, the second would be
to find a way to allow FreeBSD Rust to "play nice with both Rust in ports and the
Rust ecosystem outside ports
". He was pessimistic about the second
option being possible at all, and even if it was "it is almost guaranteed
to be a waste of our time and energy
" and would revert to the
first option in a few years.
A third option, he said, would be to work on a distribution of
FreeBSD based on packages instead of the base/ports system it has
now. That could allow FreeBSD to have the benefit of the Rust
ecosystem, or Python, or
A demo
On August 4, Somers posted
a link to
a repository forked from the FreeBSD src tree
with examples of new programs written from scratch, old programs
rewritten in Rust with new features, and libraries written in
Rust. Somers also noted several
features that his demo did not include, such as kernel modules
("those are too hard
"), integrating the Rust build system with
Make, or cdylib libraries (those are Rust libraries intended to be
linked into C/
Kamp replied
that Somers's demo was awesome, but asked if it was worth the effort
when compared to the idea of a package-based version of FreeBSD where Rust things
could be built without all the extra effort. That sparked a
back-and-forth about the difficulties of maintaining tests separately
from fast-moving features in the kernel. Ultimately, Kamp said that he
understood the problem: "I've been there myself with code I have
maintained for customers.
" The problem of maintaining Rust code
separately from kernel code only impacts a few dozen
developers. "Adding Rust to src would inconvenience /everybody/,
every time they do a 'make buildworld'.
" The solution, he said, is
not to add Rust to src, but to "get rid of the 'Src is the holy ivory tower,
everything else is barbarians' mentality
" that has caused FreeBSD
trouble over the years.
Into the black
Once again, the discussion trailed off without any firm resolution. No doubt the topic will come up again, perhaps later this year if Webb and Losh dig into Rust build-system integration. Rust may yet find its way into FreeBSD's base system, unless Kamp's vision of a package-based FreeBSD comes to pass and makes the distinction irrelevant.
