NixOS / nixfmt

The official (but not yet stable) formatter for Nix code
https://nixfmt.serokell.io
Mozilla Public License 2.0
936 stars 41 forks source link

Provide a GitHub Action #76

Closed grahamc closed 2 months ago

grahamc commented 3 years ago

It'd be really great if I could hook up a GitHub Action with nixfmt so it provided annotations to the code about reformatting that needs doing.

Lucus16 commented 3 years ago

I think that would be cool too, but I don't really know anything about Github actions. There is already the -c flag to check if files are formatted, for use in CI. If you just want a list of files that need formatting, it should be pretty trivial and I'd appreciate a PR. If github supports annotations per line, I think that would be easiest to achieve with a separate diff tool since nixfmt does not remember source locations after parsing.

omkumar312 commented 3 months ago

I suggest adding a GitHub Action to check Nix formatting using nixfmt and annotate issues in pull requests.

Create a workflow: <-add a file named->.github/workflows/nixfmt.yml

`name: Check Nix Formatting

on: [pull_request, push]

jobs: nixfmt: runs-on: ubuntu-latest

steps:
  - uses: actions/checkout@v3
  - run: nix-env -iA nixpkgs.nixfmt
  - run: nixfmt --check .
  - run: nixfmt --diff . | sed 's/^/::error file=/' || true`

This workflow installs nixfmt, checks formatting, and uses GitHub's problem matcher to annotate any reformatting needed directly in the pull request.

omkumar312 commented 3 months ago

steps workflow

  1. Checkout Code: Pulls the latest code from the repository.
  2. Install nixfmt: Installs the nixfmt tool.
  3. Check Formatting: Verifies if the Nix files are correctly formatted.
  4. Annotate Issues: Adds inline annotations to the pull request for any formatting issues.

The action ensures code the highlights formatting issues directly in pull requests.

dasJ commented 2 months ago

I don't think this is implementable anymore with the removal of recursive mode. A GH action in treefmt would probably be more appropriate.

infinisil commented 2 months ago

Briefly discussed in the meeting today, agreed with @dasJ

nixos-discourse commented 2 months ago

This issue has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/formatting-team-meeting-2024-10-01/53763/1