From f9a8373893ceb85e0f5187e8bb2e26adc5aa94b6 Mon Sep 17 00:00:00 2001 From: David Arnold Date: Sat, 29 Jan 2022 20:52:57 -0500 Subject: [PATCH] chore: add a more ergonomic (and cleaner) devshell - project a pre-commit hook for contributors - add an editorconfig --- .editorconfig | 29 +++++++++++++++++++++++++++++ README.md | 15 +++++++++++++++ flake.lock | 16 ++++++++++++++++ flake.nix | 27 ++++++++++++++++++++++++--- pre-commit.sh | 25 +++++++++++++++++++++++++ 5 files changed, 109 insertions(+), 3 deletions(-) create mode 100644 .editorconfig create mode 100644 pre-commit.sh diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..54b2e85 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,29 @@ +# Editor configuration, see http://editorconfig.org +root = true + +[*] +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true +charset = utf-8 +indent_style = space +indent_size = 2 + +# Ignore diffs/patches +[*.{diff,patch}] +end_of_line = unset +insert_final_newline = unset +trim_trailing_whitespace = unset +indent_size = unset + +[*.md] +max_line_length = off +trim_trailing_whitespace = false + +[tests/cases/**] +end_of_line = unset +insert_final_newline = unset +trim_trailing_whitespace = unset +charset = unset +indent_style = unset +indent_size = unset diff --git a/README.md b/README.md index 8f8c3a2..ae3287a 100644 --- a/README.md +++ b/README.md @@ -116,6 +116,21 @@ Let's get Alejandra on our systems: - [nixpkgs-fmt](https://github.com/nix-community/nixpkgs-fmt) - [nixfmt](https://github.com/serokell/nixfmt) +## Contributing + +#### With `direnv` + +```console +$ direnv allow +``` + +#### Without `direnv` + +```console +$ nix develop -c $SHELL +$ menu +``` + ## Footnotes [^benchmark-specs]: diff --git a/flake.lock b/flake.lock index fa6dc6e..037076a 100644 --- a/flake.lock +++ b/flake.lock @@ -38,6 +38,21 @@ "type": "github" } }, + "devshell": { + "locked": { + "lastModified": 1643393796, + "narHash": "sha256-+wb2OFWJjXUZw3HQezACc9Lj/5uuhNpUtrjDiIYw8H4=", + "owner": "numtide", + "repo": "devshell", + "rev": "fff3dc6e4538f6df85ee3027f13cc7730b23f61d", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "devshell", + "type": "github" + } + }, "fenix": { "inputs": { "nixpkgs": [ @@ -111,6 +126,7 @@ "root": { "inputs": { "alejandra": "alejandra", + "devshell": "devshell", "fenix": "fenix", "flakeCompat": "flakeCompat", "flakeUtils": "flakeUtils", diff --git a/flake.nix b/flake.nix index 5769256..ebdb894 100644 --- a/flake.nix +++ b/flake.nix @@ -20,6 +20,7 @@ treefmt.url = "github:numtide/treefmt"; treefmt.inputs.flake-utils.follows = "flakeUtils"; treefmt.inputs.nixpkgs.follows = "nixpkgs"; + devshell.url = "github:numtide/devshell"; }; outputs = inputs: @@ -31,6 +32,7 @@ nixpkgs = import inputs.nixpkgs { inherit system; }; cargoToml = builtins.fromTOML ( builtins.readFile ./Cargo.toml ); treefmt = inputs.treefmt.defaultPackage.${ system }; + devshell = inputs.devshell.legacyPackages.${ system }; fenix = inputs.fenix.packages.${ system }; fenixPlatform = nixpkgs.makeRustPlatform { inherit ( fenix.latest ) cargo rustc; }; in @@ -60,11 +62,16 @@ }; }; devShell = - nixpkgs.mkShell + devshell.mkShell { + imports = [ "${ devshell.extraModulesDir }/git/hooks.nix" ]; + git.hooks = { + enable = true; + pre-commit.text = builtins.readFile ./pre-commit.sh; + }; + name = "alejandra"; packages = [ fenix.rust-analyzer - fenix.latest.cargo fenix.latest.clippy fenix.latest.rust-src fenix.latest.rustc @@ -75,7 +82,21 @@ nixpkgs.nodePackages.prettier nixpkgs.nodePackages.prettier-plugin-toml nixpkgs.shfmt - treefmt + ]; + commands = [ + { + package = fenix.latest.cargo; + name = "cargo"; + help = "the cargo tool"; + } + { + package = treefmt; + category = "formatters"; + } + { + package = nixpkgs.editorconfig-checker; + category = "formatters"; + } ]; }; } diff --git a/pre-commit.sh b/pre-commit.sh new file mode 100644 index 0000000..5e460f1 --- /dev/null +++ b/pre-commit.sh @@ -0,0 +1,25 @@ +#!/usr/bin/env bash + +if git rev-parse --verify HEAD >/dev/null 2>&1; then + against=HEAD +else + # Initial commit: diff against an empty tree object + against=$(${git}/bin/git hash-object -t tree /dev/null) +fi + +diff="git diff-index --name-only --cached $against --diff-filter d" +all_files=($($diff)) + +# Format the entire tree. +treefmt + +# check editorconfig +editorconfig-checker -- "${all_files[@]}" +if [[ $? != '0' ]]; then + printf "%b\n" \ + "\nCode is not aligned with .editorconfig" \ + "Review the output and commit your fixes" >&2 + exit 1 +fi + +