build(nix): enable rustfmt check+formatter

with this, the gh actions ci will fail if submitted code does not adhere
to rustfmt rules.

additionally, running `nix fmt` will also format rust code.

because we want rules only supported by nightly rustfmt, and nixpkgs
doesn't have a nightly toolchain, we need to pull in another flake
input: fenix.
This commit is contained in:
Thomas Mühlbacher 2024-05-27 17:04:24 +02:00 committed by Kent Overstreet
parent 0e414b462f
commit ce01c61ba5
2 changed files with 52 additions and 0 deletions

View File

@ -1,5 +1,26 @@
{ {
"nodes": { "nodes": {
"fenix": {
"inputs": {
"nixpkgs": [
"nixpkgs"
],
"rust-analyzer-src": "rust-analyzer-src"
},
"locked": {
"lastModified": 1716791293,
"narHash": "sha256-zxFADJsXhODHH9rrF7gXjN+BYVkMoNY2NpLyjGQDOJA=",
"owner": "nix-community",
"repo": "fenix",
"rev": "9af557bccdfa8fb6a425661c33dbae46afef0afa",
"type": "github"
},
"original": {
"owner": "nix-community",
"repo": "fenix",
"type": "github"
}
},
"flake-compat": { "flake-compat": {
"flake": false, "flake": false,
"locked": { "locked": {
@ -64,12 +85,30 @@
}, },
"root": { "root": {
"inputs": { "inputs": {
"fenix": "fenix",
"flake-compat": "flake-compat", "flake-compat": "flake-compat",
"flake-parts": "flake-parts", "flake-parts": "flake-parts",
"nixpkgs": "nixpkgs", "nixpkgs": "nixpkgs",
"treefmt-nix": "treefmt-nix" "treefmt-nix": "treefmt-nix"
} }
}, },
"rust-analyzer-src": {
"flake": false,
"locked": {
"lastModified": 1716714715,
"narHash": "sha256-xJ5vzVr5Et6wxHRQLOYicsa0gN2bwm4mj6AFHc8ugJc=",
"owner": "rust-lang",
"repo": "rust-analyzer",
"rev": "71a816a90facb6546a0a06010da17598e11812f7",
"type": "github"
},
"original": {
"owner": "rust-lang",
"ref": "nightly",
"repo": "rust-analyzer",
"type": "github"
}
},
"treefmt-nix": { "treefmt-nix": {
"inputs": { "inputs": {
"nixpkgs": [ "nixpkgs": [

View File

@ -11,6 +11,11 @@
inputs.nixpkgs.follows = "nixpkgs"; inputs.nixpkgs.follows = "nixpkgs";
}; };
fenix = {
url = "github:nix-community/fenix";
inputs.nixpkgs.follows = "nixpkgs";
};
flake-compat = { flake-compat = {
url = "github:edolstra/flake-compat"; url = "github:edolstra/flake-compat";
flake = false; flake = false;
@ -23,6 +28,7 @@
nixpkgs, nixpkgs,
flake-parts, flake-parts,
treefmt-nix, treefmt-nix,
fenix,
flake-compat, flake-compat,
... ...
}: }:
@ -41,8 +47,12 @@
self', self',
config, config,
pkgs, pkgs,
system,
... ...
}: }:
let
rustfmtToml = builtins.fromTOML (builtins.readFile ./rustfmt.toml);
in
{ {
packages.default = config.packages.bcachefs-tools; packages.default = config.packages.bcachefs-tools;
packages.bcachefs-tools = pkgs.callPackage ./build.nix { }; packages.bcachefs-tools = pkgs.callPackage ./build.nix { };
@ -74,6 +84,9 @@
programs = { programs = {
nixfmt-rfc-style.enable = true; nixfmt-rfc-style.enable = true;
rustfmt.edition = rustfmtToml.edition;
rustfmt.enable = true;
rustfmt.package = fenix.packages.${system}.default.rustfmt;
}; };
}; };
}; };