Quick Start¶
This document describes how to get started with kernel development in Rust.
There are a few ways to install a Rust toolchain needed for kernel development.A simple way is to use the packages from your Linux distribution if they aresuitable -- the first section below explains this approach. An advantage of thisapproach is that, typically, the distribution will match the LLVM used by Rustand Clang.
Another way is using the prebuilt stable versions of LLVM+Rust provided onkernel.org. These are the same slimand fast LLVM toolchains fromGetting LLVM with versionsof Rust added to them that Rust for Linux supports. Two sets are provided: the“latest LLVM” and “matching LLVM” (please see the link for more information).
Alternatively, the next two “Requirements” sections explain each component andhow to install them throughrustup, the standalone installers from Rustand/or building them.
The rest of the document explains other aspects on how to get started.
Distributions¶
Arch Linux¶
Arch Linux provides recent Rust releases and thus it should generally work outof the box, e.g.:
pacman -S rust rust-src rust-bindgen
Debian¶
Debian 13 (Trixie), as well as Testing and Debian Unstable (Sid) provide recentRust releases and thus they should generally work out of the box, e.g.:
apt install rustc rust-src bindgen rustfmt rust-clippy
Fedora Linux¶
Fedora Linux provides recent Rust releases and thus it should generally work outof the box, e.g.:
dnf install rust rust-src bindgen-cli rustfmt clippy
Gentoo Linux¶
Gentoo Linux (and especially the testing branch) provides recent Rust releasesand thus it should generally work out of the box, e.g.:
USE='rust-src rustfmt clippy' emerge dev-lang/rust dev-util/bindgen
LIBCLANG_PATH may need to be set.
Nix¶
Nix (unstable channel) provides recent Rust releases and thus it shouldgenerally work out of the box, e.g.:
{ pkgs ? import <nixpkgs> {} }:pkgs.mkShell { nativeBuildInputs = with pkgs; [ rustc rust-bindgen rustfmt clippy ]; RUST_LIB_SRC = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}";}openSUSE¶
openSUSE Slowroll and openSUSE Tumbleweed provide recent Rust releases and thusthey should generally work out of the box, e.g.:
zypper install rust rust1.79-src rust-bindgen clang
Ubuntu¶
25.04¶
The latest Ubuntu releases provide recent Rust releases and thus they shouldgenerally work out of the box, e.g.:
apt install rustc rust-src bindgen rustfmt rust-clippy
In addition,RUST_LIB_SRC needs to be set, e.g.:
RUST_LIB_SRC=/usr/src/rustc-$(rustc --version | cut -d' ' -f2)/library
For convenience,RUST_LIB_SRC can be exported to the global environment.
24.04 LTS and older¶
Though Ubuntu 24.04 LTS and older versions still provide recent Rustreleases, they require some additional configuration to be set, usingthe versioned packages, e.g.:
apt install rustc-1.80 rust-1.80-src bindgen-0.65 rustfmt-1.80 \ rust-1.80-clippyln -s /usr/lib/rust-1.80/bin/rustfmt /usr/bin/rustfmt-1.80ln -s /usr/lib/rust-1.80/bin/clippy-driver /usr/bin/clippy-driver-1.80
None of these packages set their tools as defaults; therefore they should bespecified explicitly, e.g.:
make LLVM=1 RUSTC=rustc-1.80 RUSTDOC=rustdoc-1.80 RUSTFMT=rustfmt-1.80 \ CLIPPY_DRIVER=clippy-driver-1.80 BINDGEN=bindgen-0.65
Alternatively, modify thePATH variable to place the Rust 1.80 binariesfirst and setbindgen as the default, e.g.:
PATH=/usr/lib/rust-1.80/bin:$PATHupdate-alternatives --install /usr/bin/bindgen bindgen \ /usr/bin/bindgen-0.65 100update-alternatives --set bindgen /usr/bin/bindgen-0.65
RUST_LIB_SRC needs to be set when using the versioned packages, e.g.:
RUST_LIB_SRC=/usr/src/rustc-$(rustc-1.80 --version | cut -d' ' -f2)/library
For convenience,RUST_LIB_SRC can be exported to the global environment.
In addition,bindgen-0.65 is available in newer releases (24.04 LTS and24.10), but it may not be available in older ones (20.04 LTS and 22.04 LTS),thusbindgen may need to be built manually (please see below).
Requirements: Building¶
This section explains how to fetch the tools needed for building.
To easily check whether the requirements are met, the following targetcan be used:
make LLVM=1 rustavailable
This triggers the same logic used by Kconfig to determine whetherRUST_IS_AVAILABLE should be enabled; but it also explains why notif that is the case.
rustc¶
A recent version of the Rust compiler is required.
Ifrustup is being used, enter the kernel build directory (or use--path=<build-dir> argument to theset sub-command) and run,for instance:
rustup override set stable
This will configure your working directory to use the given version ofrustc without affecting your default toolchain.
Note that the override applies to the current working directory (and itssub-directories).
If you are not usingrustup, fetch a standalone installer from:
Rust standard library source¶
The Rust standard library source is required because the build system willcross-compilecore.
Ifrustup is being used, run:
rustup component add rust-src
The components are installed per toolchain, thus upgrading the Rust compilerversion later on requires re-adding the component.
Otherwise, if a standalone installer is used, the Rust source tree may bedownloaded into the toolchain’s installation folder:
curl -L "https://static.rust-lang.org/dist/rust-src-$(rustc --version | cut -d' ' -f2).tar.gz" | tar -xzf - -C "$(rustc --print sysroot)/lib" \ "rust-src-$(rustc --version | cut -d' ' -f2)/rust-src/lib/" \ --strip-components=3
In this case, upgrading the Rust compiler version later on requires manuallyupdating the source tree (this can be done by removing$(rustc--printsysroot)/lib/rustlib/src/rust then rerunning the above command).
libclang¶
libclang (part of LLVM) is used bybindgen to understand the C codein the kernel, which means LLVM needs to be installed; like when the kernelis compiled withLLVM=1.
Linux distributions are likely to have a suitable one available, so it isbest to check that first.
There are also some binaries for several systems and architectures uploaded at:
Otherwise, building LLVM takes quite a while, but it is not a complex process:
Please seeBuilding Linux with Clang/LLVM for more information and further waysto fetch pre-built releases and distribution packages.
bindgen¶
The bindings to the C side of the kernel are generated at build time usingthebindgen tool.
Install it, for instance, via (note that this will download and build the toolfrom source):
cargo install --locked bindgen-cli
bindgen uses theclang-sys crate to find a suitablelibclang (whichmay be linked statically, dynamically or loaded at runtime). By default, thecargo command above will produce abindgen binary that will loadlibclang at runtime. If it is not found (or a differentlibclang thanthe one found should be used), the process can be tweaked, e.g. by using theLIBCLANG_PATH environment variable. For details, please seeclang-sys’sdocumentation at:
Requirements: Developing¶
This section explains how to fetch the tools needed for developing. That is,they are not needed when just building the kernel.
rustfmt¶
Therustfmt tool is used to automatically format all the Rust kernel code,including the generated C bindings (for details, please seeCoding Guidelines).
Ifrustup is being used, itsdefault profile already installs the tool,thus nothing needs to be done. If another profile is being used, the componentcan be installed manually:
rustup component add rustfmt
The standalone installers also come withrustfmt.
clippy¶
clippy is a Rust linter. Running it provides extra warnings for Rust code.It can be run by passingCLIPPY=1 tomake (for details, please seeGeneral Information).
Ifrustup is being used, itsdefault profile already installs the tool,thus nothing needs to be done. If another profile is being used, the componentcan be installed manually:
rustup component add clippy
The standalone installers also come withclippy.
rustdoc¶
rustdoc is the documentation tool for Rust. It generates pretty HTMLdocumentation for Rust code (for details, please seeGeneral Information).
rustdoc is also used to test the examples provided in documented Rust code(called doctests or documentation tests). Therusttest Make target usesthis feature.
Ifrustup is being used, all the profiles already install the tool,thus nothing needs to be done.
The standalone installers also come withrustdoc.
rust-analyzer¶
Therust-analyzer language server canbe used with many editors to enable syntax highlighting, completion, go todefinition, and other features.
rust-analyzer needs a configuration file,rust-project.json, whichcan be generated by therust-analyzer Make target:
make LLVM=1 rust-analyzer
Configuration¶
Rustsupport (CONFIG_RUST) needs to be enabled in theGeneralsetupmenu. The option is only shown if a suitable Rust toolchain is found (seeabove), as long as the other requirements are met. In turn, this will makevisible the rest of options that depend on Rust.
Afterwards, go to:
Kernel hacking -> Sample kernel code -> Rust samples
And enable some sample modules either as built-in or as loadable.
Building¶
Building a kernel with a complete LLVM toolchain is the best supported setupat the moment. That is:
make LLVM=1
Using GCC also works for some configurations, but it is very experimental atthe moment.
Hacking¶
To dive deeper, take a look at the source code of the samplesatsamples/rust/, the Rust support code underrust/ andtheRusthacking menu underKernelhacking.
If GDB/Binutils is used and Rust symbols are not getting demangled, the reasonis the toolchain does not support Rust’s new v0 mangling scheme yet.There are a few ways out:
Install a newer release (GDB >= 10.2, Binutils >= 2.36).
Some versions of GDB (e.g. vanilla GDB 10.1) are able to usethe pre-demangled names embedded in the debug info (
CONFIG_DEBUG_INFO).