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

feat: add support for formatter #131

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

elliott-farrall
Copy link

Since the nix flake outputs schema allows us to specify a formatter, I figured it makes sense to support formatter in snowfall-lib (without having to use outputs-builder).

The schema for formatter is similar to checks, devShells, etc but currently only allows for a single option per-system. With this in mind, I think a single derivation (dependent on system) specified at formatter/default.nix would fit well with the current snowfall-lib structure.

Since there is only one formatter, alias is not applicable here. In theory overrides could be implemented but would require setting priority between full-flake-options and user-outputs so I have skipped this for now.

The implementation of create-formatter is based on create-checks and create-shells so has some redundant steps but provides a basic proof of concept.

@jakehamilton
Copy link
Member

Hmm, I am not totally sure if the additional abstraction is really beneficial here. The standalone formatter file would probably just be:

{ nixfmt }: nixfmt

In which case, I don't know if that is worth separating just to avoid a few lines:

outputs-builder = channels: {
  formatter = channels.nixpkgs.nxifmt;
};

@elliott-farrall
Copy link
Author

I think you're right in that, for most people, the reduction in boilerplate is pretty minimal. For me personally, I mainly use treefmt-nix and so I might have a formatter file like:

{ pkgs
, inputs
, ...
}:

inputs.treefmt-nix.lib.mkWrapper pkgs {
  projectRootFile = ".git/config";

  settings.global.excludes = [
    "LICENSE.md"
  ];

  programs = {
    prettier.enable = true;
    taplo.enable = true;
    yamlfmt.enable = true;
  };

  settings.formatter = {
    yamlfmt.options = [ "-formatter" "retain_line_breaks_single=true" ];
  };
}

which is a bit more substantial. I do also have some check files that are one-liners, essentially pulling a check from an input and getting it into the right form. I prefer this over using outputs-builder as I feel it keeps things more organised.

I understand that adding support for formatter is probably overkill. I just thought I'd submit a PR to see if anyone else thought it was a good idea.

@TakodaS
Copy link

TakodaS commented Nov 8, 2024

For posterity, I use the following configuration in snowfall-lib to set up treefmt:

outputs-builder = channels: let
    treefmtConfig = {pkgs, ...}: { 
    projectRootFile = "flake.nix";  
    programs.alejandra.enable = true; 
    # rest of config
}; 
    treefmtEval = inputs.treefmt-nix.lib.evalModule (channels.nixpkgs) treefmtConfig;
in {
    formatter = treefmtEval.config.build.wrapper;
}

@TakodaS TakodaS mentioned this pull request Nov 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants