Rust with flake.parts
Installing a Rust toolchain with Nix and Fenix is simple but there's a small hoop to jump through as soon as you move to the more modern approach provided by flake.parts.
Flake.parts advocates against excessive use of overlays, but provides an escape hatch when one needs to leverage the established ecosystem.
The trick is to override the pkgs
argument pass into perSystem
:
_module.args.pkgs = import inputs.nixpkgs {
inherit system;
overlays = [
inputs.fenix.overlays.default
];
};
A full-featured flake.nix
that installs the latest stable Rust toolchain, plus
rust-analyzer, might look something like this:
{
description = "Rust with flake.parts";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
devenv.url = "github:cachix/devenv";
fenix = {
url = "github:nix-community/fenix";
inputs.nixpkgs.follows = "nixpkgs";
};
};
nixConfig = {
extra-trusted-public-keys = "devenv.cachix.org-1:w1cLUi8dv3hnoSPGAuibQv+f9TZLr6cv/Hm9XgU50cw=";
extra-substituters = "https://devenv.cachix.org";
};
outputs = inputs @ {flake-parts, ...}:
flake-parts.lib.mkFlake {inherit inputs;} {
imports = [
inputs.devenv.flakeModule
];
systems = ["aarch64-darwin" "aarch64-linux" "i686-linux" "x86_64-darwin" "x86_64-linux"];
perSystem = {
config,
self',
inputs',
pkgs,
system,
...
}: {
_module.args.pkgs = import inputs.nixpkgs {
inherit system;
overlays = [
inputs.fenix.overlays.default
];
};
devenv.shells.default = {
name = "dev";
packages = with pkgs; [
(fenix.stable.withComponents [
"cargo"
"clippy"
"rust-src"
"rustc"
"rustfmt"
])
.rust-analyzer
];
};
};
};
}
We can, of course, install any of the supported toolchains provided by Fenix.