### Install Cachix and Use Binary Cache Source: https://github.com/cachix/git-hooks.nix/blob/master/README.md Installs the Cachix tool and configures it to use the pre-commit-hooks binary cache to avoid compilation. ```sh nix-env -iA cachix -f https://cachix.org/api/v1/install cachix use pre-commit-hooks ``` -------------------------------- ### Check Nix Build Size Source: https://github.com/cachix/git-hooks.nix/blob/master/README.md This example demonstrates how to check the size of a Nix build, which is a guideline for contributing new hooks. Ensure the closure size is small, ideally under 50MB. ```sh $ du -sh $(nix-build -A go) 463M /nix/store/v4ys4lrjngf62lvvrdbs7r9kbxh9nqaa-go-1.18.6 ``` -------------------------------- ### Flake Configuration for Git Hooks Source: https://github.com/cachix/git-hooks.nix/blob/master/README.md Set up git hooks within a Nix flake. This example demonstrates how to define a formatter, checks, and development shells that automatically include the hooks. ```nix { description = "An example project"; inputs = { systems.url = "github:nix-systems/default"; git-hooks.url = "github:cachix/git-hooks.nix"; }; outputs = { self, systems, nixpkgs, ... }@inputs: let forEachSystem = nixpkgs.lib.genAttrs (import systems); in { # Run the hooks with `nix fmt`. formatter = forEachSystem ( system: let pkgs = nixpkgs.legacyPackages.${system}; config = self.checks.${system}.pre-commit-check.config; inherit (config) package configFile; script = '' ${pkgs.lib.getExe package} run --all-files --config ${configFile} ''; in pkgs.writeShellScriptBin "pre-commit-run" script ); # Run the hooks in a sandbox with `nix flake check`. # Read-only filesystem and no internet access. checks = forEachSystem (system: { pre-commit-check = inputs.git-hooks.lib.${system}.run { src = ./.; hooks = { nixfmt.enable = true; }; }; }); # Enter a development shell with `nix develop`. # The hooks will be installed automatically. # Or run pre-commit manually with `nix develop -c pre-commit run --all-files` devShells = forEachSystem (system: { default = let pkgs = nixpkgs.legacyPackages.${system}; inherit (self.checks.${system}.pre-commit-check) shellHook enabledPackages; in pkgs.mkShell { inherit shellHook; buildInputs = enabledPackages; }; }); }; } ``` -------------------------------- ### Enter Development Shell with Git Hooks Source: https://github.com/cachix/git-hooks.nix/blob/master/README.md Command to enter a development shell where pre-commit hooks are automatically installed and ready to be used. ```shell nix develop ``` -------------------------------- ### Define Custom Pre-commit Hook Source: https://github.com/cachix/git-hooks.nix/blob/master/README.md Example of defining a custom pre-commit hook for a C project using Make. This demonstrates how to specify the entry command, file patterns, types, exclusions, language, filename passing, and stages. ```nix let nix-pre-commit-hooks = import (builtins.fetchTarball "https://github.com/cachix/git-hooks.nix/tarball/master"); in { pre-commit-check = nix-pre-commit-hooks.run { hooks = { # ... # Example custom hook for a C project using Make: unit-tests = { enable = true; # The name of the hook (appears on the report table): name = "Unit tests"; # The command to execute (mandatory): entry = "make check"; # The pattern of files to run on (default: "" (all)) # see also https://pre-commit.com/#hooks-files files = "\\.(c|h)$"; # List of file types to run on (default: [ "file" ] (all files)) # see also https://pre-commit.com/#filtering-files-with-types # You probably only need to specify one of `files` or `types`: types = [ "text" "c" ]; # Exclude files that were matched by these patterns (default: [ ] (none)): excludes = [ "irrelevant\\.c" ]; # The language of the hook - tells pre-commit # how to install the hook # (default: "system", or "unsupported" for pre-commit >= 4.4.0) # see also https://pre-commit.com/#supported-languages language = "unsupported"; # Set this to false to not pass the changed files # to the command (default: true): pass_filenames = false; # Which git hooks the command should run for (default: [ "pre-commit" ]): stages = ["pre-push"]; }; }; }; } ``` -------------------------------- ### Configure clang-format for C/C++ Source: https://github.com/cachix/git-hooks.nix/blob/master/README.md Use this configuration to enable clang-format and restrict its formatting to C and C++ files. Ensure clang-format is installed and enabled. ```nix clang-format = { enable = true; types_or = lib.mkForce [ "c" "c++" ]; }; ``` -------------------------------- ### Direnv Configuration for Nix Source: https://github.com/cachix/git-hooks.nix/blob/master/README.md Configures direnv to use Nix for managing the project's environment, ensuring consistency and proper toolchain setup. ```conf use nix ``` -------------------------------- ### Integrate Hooks in shell.nix Source: https://github.com/cachix/git-hooks.nix/blob/master/README.md Sets up a Nix shell environment that includes pre-commit hooks, making the `pre-commit` executable available for use during development. This ensures tools are built and configurations are symlinked. ```nix let pre-commit = import ./default.nix; in (import {}).mkShell { shellHook = '' ${pre-commit.pre-commit-check.shellHook} ''; buildInputs = pre-commit.pre-commit-check.enabledPackages; } ``` -------------------------------- ### Run All Hooks Sandboxed Source: https://github.com/cachix/git-hooks.nix/blob/master/README.md Execute all configured pre-commit hooks in a sandboxed environment using `nix flake check`. Note that this environment has limitations like no internet access and read-only filesystem. ```shell nix flake check ``` -------------------------------- ### Format Code with Nix Fmt Source: https://github.com/cachix/git-hooks.nix/blob/master/README.md Use the `nix fmt` command to format your Nix code, leveraging the formatter configured in your flake. This is an alternative to running pre-commit directly for formatting tasks. ```shell nix fmt ``` -------------------------------- ### Configure Git Hooks in Nix Source: https://github.com/cachix/git-hooks.nix/blob/master/README.md Enable and configure various pre-commit hooks like nixfmt, black, shellcheck, and mdsh. Custom hooks can also be defined. Supports overriding packages for hooks like clippy. ```nix { inputs, ... }: { git-hooks.hooks = { # Format Nix code nixfmt.enable = true; # Format Python code black.enable = true; # Lint shell scripts shellcheck.enable = true; # Execute shell examples in Markdown files mdsh.enable = true; # Override a package with a different version ormolu.enable = true; ormolu.package = pkgs.haskellPackages.ormolu; # Some hooks have more than one package, like clippy: clippy.enable = true; clippy.packageOverrides.cargo = pkgs.cargo; clippy.packageOverrides.clippy = pkgs.clippy; # Some hooks provide settings clippy.settings.allFeatures = true; # Define your own custom hooks # See all options: https://github.com/cachix/git-hooks.nix#custom-hooks my-custom-hook = { enable = true; entry = "./on-pre-commit.sh"; }; }; # Use alternative pre-commit implementations git-hooks.package = pkgs.prek; } ``` -------------------------------- ### Integrate Hooks in default.nix Source: https://github.com/cachix/git-hooks.nix/blob/master/README.md Configures pre-commit hooks within a Nix expression for integration into your project's build process. Supports enabling specific hooks and overriding packages or settings. ```nix let nix-pre-commit-hooks = import (builtins.fetchTarball "https://github.com/cachix/git-hooks.nix/tarball/master"); in { # Configured with the module options defined in `modules/pre-commit.nix`: pre-commit-check = nix-pre-commit-hooks.run { src = ./.; # If your hooks are intrusive, avoid running on each commit with a default_states like this: # default_stages = ["manual" "pre-push"]; hooks = { elm-format.enable = true; # override a package with a different version ormolu.enable = true; ormolu.package = pkgs.haskellPackages.ormolu; ormolu.settings.defaultExtensions = [ "lhs" "hs" ]; # some hooks have more than one package, like clippy: clippy.enable = true; clippy.packageOverrides.cargo = pkgs.cargo; clippy.packageOverrides.clippy = tools.clippy; # some hooks provide settings clippy.settings.allFeatures = true; }; }; } ``` -------------------------------- ### Run Pre-commit Manually in Development Shell Source: https://github.com/cachix/git-hooks.nix/blob/master/README.md Execute pre-commit hooks manually within a development shell. This is a recommended alternative for hooks that require internet access or file modification capabilities. ```shell nix develop -c pre-commit run -a ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.