Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a GitHub Action #76

Open
grahamc opened this issue Jan 20, 2021 · 9 comments
Open

Provide a GitHub Action #76

grahamc opened this issue Jan 20, 2021 · 9 comments
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@grahamc
Copy link
Member

grahamc commented Jan 20, 2021

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
Copy link
Contributor

Lucus16 commented Jan 21, 2021 via email

@yorickvP yorickvP added enhancement New feature or request good first issue Good for newcomers labels Feb 25, 2022
@infinisil infinisil moved this to Todo in Nix formatting May 28, 2024
@omkumar312
Copy link

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
Copy link

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
Copy link
Member

dasJ commented Sep 18, 2024

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
Copy link
Member

Briefly discussed in the meeting today, agreed with @dasJ

@infinisil infinisil closed this as not planned Won't fix, can't repro, duplicate, stale Oct 1, 2024
@github-project-automation github-project-automation bot moved this from Todo to Done in Nix formatting Oct 1, 2024
@nixos-discourse
Copy link

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

@cdepillabout
Copy link
Member

While I can certainly understand if the nixfmt considers this out of scope, I disagree with @dasJ:

I don't think this is implementable anymore with the removal of recursive mode.

It is certainly still possible to implement a GitHub Action that finds all the .nix files in a repo, runs nixfmt on them, and fails if any of them are not formatted.

I'm currently in the process of switching my company over to using nixfmt-rfc-style, and not having an out-of-the-box, easy-to-use GitHub Action makes it just slightly harder to switch over.

@infinisil
Copy link
Member

infinisil commented Feb 21, 2025

I'd like to reconsider this. Being able to have a simple nixfmt check with GHA would be nice. Some brainstorming on that:

Could have an interface like this:

# Put this in your repos .github/workflows/nixfmt.yml
name: "Check Nix formatting"
on:
  pull_request:
  push:
jobs:
  check:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    - uses: cachix/install-nix-action@v30
    - uses: NixOS/treefmt-nix-action@v1
      with:
        nixpkgs: https://github.com/NixOS/nixpkgs/archive/f1c5d6d23fd57011b78cb2424f5c73e68c9e8879.tar.gz
        treefmt-nix: https://github.com/numtide/treefmt-nix/3d0579f5cc93436052d94b73925b48973a104204.tar.gz
        module: |
          {
            # See https://github.com/numtide/treefmt-nix/tree/main/programs for more options 
            programs.nixfmt-rfc-style.enable = true;
          }
        # As an alternative to the above:
        # file: treefmt.nix
        # flake: .#treefmt

In the back this would:

  • Create a treefmt.nix file like this:
    {
      # Alternatively, get pinned versions from npins, flakes, niv, gridlock or so
      nixpkgs ? fetchTarball {
        url = "https://github.com/NixOS/nixpkgs/archive/f1c5d6d23fd57011b78cb2424f5c73e68c9e8879.tar.gz";
        # sha256 = "";
      },
      treefmt-nix ? fetchTarball {
        url = "https://github.com/numtide/treefmt-nix/3d0579f5cc93436052d94b73925b48973a104204.tar.gz";
        # sha256 = "";
      },
      system ? builtins.currentSystem,
      pkgs ? import nixpkgs {
        config = { };
        overlays = [ ];
        inherit system;
      };
    }
    let
      treefmtEval = (import treefmt-nix).evalModule pkgs
        {
          # See https://github.com/numtide/treefmt-nix/tree/main/programs for more options 
          programs.nixfmt-rfc-style.enable = true;
        }
        ;
    };
    in
    {
      # Use this with `mkShell { inputsFrom = [ treefmt.shell ]; packages = [ ... ]; }
      # shell = treefmtEval.config.build.devShell;
      pkg = treefmtEval.config.build.wrapper;
    };
  • Run nix-build treefmt.nix -A pkg
  • Clone the users repo
  • Run result/bin/treefmt . in the repo

The action should also tell the user that they can copy that generated file and put it in treefmt.nix to allow more reuse with other Nix code. Or alternatively use flake.nix.

This could be implemented using a composite GitHub Action.

@infinisil infinisil reopened this Feb 21, 2025
@MattSturgeon
Copy link

MattSturgeon commented Feb 24, 2025

If this is added, it'd be great for it to have PR annotation message support, as-per #76 (comment). Ideally it'd also have line-wise annotations, not just per-file. See workflow commands docs.

Somewhat related treefmt issues:

EDIT: I was convinced by discussion on numtide/treefmt#526 that inline PR annotations aren't suitable when using a treewide auto-formatter. They could still make sense if files are being formatted on a per-file basis, as is currently the case in nixpkgs (although that's soon to change: NixOS/nixpkgs#380990).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
Status: Done
Development

No branches or pull requests

9 participants