From a8bbb068a29827d3a6b3767bd309ed6b1a90bd0e Mon Sep 17 00:00:00 2001 From: Daniel Lundin Date: Mon, 21 Oct 2024 23:41:27 +0200 Subject: [PATCH] Add treefmt for code formatting --- flake.lock | 37 ++++++++++++++++++++++++++++++++++++- flake.nix | 30 +++++++++++++++++------------- justfile | 4 ++-- 3 files changed, 55 insertions(+), 16 deletions(-) diff --git a/flake.lock b/flake.lock index b1acd42..cb5c297 100644 --- a/flake.lock +++ b/flake.lock @@ -96,6 +96,22 @@ "type": "github" } }, + "nixpkgs_3": { + "locked": { + "lastModified": 1726871744, + "narHash": "sha256-V5LpfdHyQkUF7RfOaDPrZDP+oqz88lTJrMT1+stXNwo=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "a1d92660c6b3b7c26fb883500a80ea9d33321be2", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, "root": { "inputs": { "advisory-db": "advisory-db", @@ -103,7 +119,8 @@ "flake-utils": "flake-utils", "nix-filter": "nix-filter", "nixpkgs": "nixpkgs", - "rust-overlay": "rust-overlay" + "rust-overlay": "rust-overlay", + "treefmt-nix": "treefmt-nix" } }, "rust-overlay": { @@ -138,6 +155,24 @@ "repo": "default", "type": "github" } + }, + "treefmt-nix": { + "inputs": { + "nixpkgs": "nixpkgs_3" + }, + "locked": { + "lastModified": 1729242555, + "narHash": "sha256-6jWSWxv2crIXmYSEb3LEVsFkCkyVHNllk61X4uhqfCs=", + "owner": "numtide", + "repo": "treefmt-nix", + "rev": "d986489c1c757f6921a48c1439f19bfb9b8ecab5", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "treefmt-nix", + "type": "github" + } } }, "root": "root", diff --git a/flake.nix b/flake.nix index a9dd96c..9501f21 100644 --- a/flake.nix +++ b/flake.nix @@ -9,6 +9,7 @@ nix-filter.url = "github:numtide/nix-filter"; nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.05"; rust-overlay.url = "github:oxalica/rust-overlay"; + treefmt-nix.url = "github:numtide/treefmt-nix"; }; outputs = @@ -20,6 +21,7 @@ nix-filter, nixpkgs, rust-overlay, + treefmt-nix, }: flake-utils.lib.eachDefaultSystem ( system: @@ -54,23 +56,25 @@ }; commonArgs = { - inherit - src - stdenv - nativeBuildInputs - ; + inherit src stdenv nativeBuildInputs; strictDeps = true; LIBCLANG_PATH = "${pkgs.llvmPackages.libclang.lib}/lib"; }; cargoArtifacts = craneLib.buildDepsOnly commonArgs; - my-project = craneLib.buildPackage ( - commonArgs - // { - inherit cargoArtifacts; - } - ); + treefmtEval = treefmt-nix.lib.evalModule pkgs { + projectRootFile = "flake.nix"; + programs = { + nixfmt.enable = true; + nixfmt.package = pkgs.nixfmt-rfc-style; + shfmt.enable = true; + rustfmt.enable = true; + }; + settings.formatter.rustfmt.command = pkgs.lib.mkForce "${pkgs.rust-toolchain}/bin/rustfmt"; + }; + + my-project = craneLib.buildPackage (commonArgs // { inherit cargoArtifacts; }); in rec { # `nix build` @@ -100,8 +104,8 @@ ); }; - # Formatter for the Nix code - formatter = pkgs.nixpkgs-fmt; + # For `nix fmt` + formatter = treefmtEval.config.build.wrapper; # `nix develop` devShells.default = pkgs.mkShell { diff --git a/justfile b/justfile index cac65b8..a5926ea 100644 --- a/justfile +++ b/justfile @@ -10,11 +10,11 @@ check: # Lint all source code lint: - echo FIXME: Add linter(s) here + cargo clippy # Format all source code fmt: - echo FIXME: Add formatter here + nix fmt # Update all dependencies update: update-nix-pkgs update-rust-deps