Pre-commit Hooks with Pixi#
Overview#
Pre-commit hooks are scripts that run automatically before each commit to identify and fix common issues early in the development process. This project uses pre-commit to manage these hooks, ensuring code quality and consistency.
When integrated with Pixi, pre-commit hooks can be easily managed within your project's development environment.
Benefits of Pre-commit Hooks#
- Catch issues early: Identify problems before they're committed
- Consistent code style: Automatically format code to project standards
- Prevent security issues: Detect secrets, vulnerabilities, and other security concerns
- Save time: Avoid failed CI/CD pipelines by catching issues locally
- Improve code quality: Run tests and linters automatically
Setting Up Pre-commit with Pixi#
1. Install Pixi#
If you haven't installed Pixi yet, follow the instructions on the Pixi installation page:
For Windows:
For Linux/macOS:
2. Clone the Repository and Install Dependencies#
3. Install Pre-commit in the Pixi Environment#
The project already has pre-commit as a development dependency in the pyproject.toml
file. When you run pixi install
, pre-commit will be installed in the development environment.
To verify that pre-commit is installed:
4. Install the Pre-commit Hooks#
To install the pre-commit hooks into your local git repository:
This will set up the git hooks script and also install the hook environments.
To install the commit message hook:
Using Pre-commit Hooks#
Running Pre-commit Hooks Manually#
To run all pre-commit hooks manually on all files:
To run a specific hook:
For example, to run just the black formatter:
Automatic Execution#
Once installed, pre-commit hooks will run automatically when you attempt to commit changes. If any hook fails, the commit will be aborted, allowing you to fix the issues before trying again.
Skipping Hooks#
In some cases, you might need to skip pre-commit hooks:
However, this should be used sparingly, as the hooks are designed to maintain code quality.
Configured Hooks#
This project has the following pre-commit hooks configured:
Code Formatting#
- black: Python code formatter
- isort: Sorts Python imports
- beautysh: Shell script formatter
Code Quality#
- flake8: Python linter
- bandit: Security linter for Python code
File Formatting#
- trailing-whitespace: Removes trailing whitespace
- end-of-file-fixer: Ensures files end with a newline
- mixed-line-ending: Normalizes line endings
- pretty-format-json: Formats JSON files
Security Checks#
- gitleaks: Scans for secrets and credentials
- detect-secrets: Detects secrets in code
- checkov: Scans for security issues
- truffleHog: Finds credentials and secrets
Testing#
- pytest-check: Runs pytest with coverage
- notebook-check: Validates Jupyter notebooks
- doctest: Runs doctests in Python modules
Customizing Pre-commit Configuration#
The pre-commit configuration is stored in the .pre-commit-config.yaml
file at the root of the repository. You can modify this file to add, remove, or configure hooks.
After modifying the configuration, update the installed hooks:
Troubleshooting#
Hook Installation Issues#
If you encounter issues installing hooks:
Slow Hooks#
Some hooks, particularly those that run tests, can be slow. You can skip these during development:
Updating Hooks#
To update all hooks to their latest versions:
Best Practices#
- Run hooks before pushing: Ensure all hooks pass before pushing to remote
- Keep hooks updated: Regularly run
pre-commit autoupdate
- Add custom hooks: Create local hooks for project-specific checks
- Document hook behavior: Ensure team members understand what each hook does
- Use with CI/CD: Run the same checks in your CI/CD pipeline
Note
This documentation was generated on 2025-07-24.