teslamate-org / teslamate

A self-hosted data logger for your Tesla 🚘
https://docs.teslamate.org
MIT License
6.05k stars 755 forks source link

should enable treefmt flakeCheck #4294

Open brianmay opened 1 month ago

brianmay commented 1 month ago

Is there an existing issue for this?

What happened?

This was disabled because we couldn't get it to work.

Expected Behavior

No response

Steps To Reproduce

No response

Relevant log output

N/A

Screenshots

No response

Additional data

I think we need to fix with a wrapper script, something like:

{inputs, ...}: {
  imports = [
    inputs.treefmt-nix.flakeModule
  ];
  perSystem = {
    config,
    pkgs,
    ...
  }: let
    script = pkgs.writeScriptBin "treefmt" ''
      #! ${pkgs.bash}/bin/bash
      set -x
      set -e
      export MIX_REBAR3="${pkgs.rebar3}/bin/rebar3"
      exec ${pkgs.treefmt}/bin/treefmt "$@"
    '';

    wrapper = pkgs.symlinkJoin {
      name = "treefmt-wrapper";
      version = pkgs.treefmt.version;
      paths = [script pkgs.treefmt];
    };
  in {
    # Auto formatters. This also adds a flake check to ensure that the
    # source tree was auto formatted.

    treefmt = {
      flakeFormatter = true; # Enables treefmt the default formatter used by the nix fmt command
      flakeCheck = true; # Add a flake check to run treefmt, disabled, as mix format does need the dependencies fetched beforehand
      package = wrapper;
      projectRootFile = "VERSION"; # File used to identity repo root

      # we really need to mirror the treefmt.toml as we can't use it directly
      settings.global.excludes = [
        "*.gitignore"
        "*.dockerignore"
        ".envrc"
        "*.node-version"
        "Dockerfile"
        "grafana/Dockerfile"
        "Makefile"
        "VERSION"
        "LICENSE"
        "*.metadata"
        "*.manifest"
        "*.webmanifest"
        "*.dat"
        "*.lock"
        "*.txt"
        "*.csv"
        "*.ico"
        "*.png"
        "*.svg"
        "*.properties"
        "*.xml"
        "*.po"
        "*.pot"
        "*.json.example"
        "*.typos.toml"
        "treefmt.toml"
        "grafana/dashboards/*.json" # we use the grafana export style
      ];
      programs.mix-format.enable = true;
      settings.formatter.mix-format.includes = [
        "*.ex"
        "*.exs"
        "*.{heex,eex}"
      ];
      # run shellcheck first
      programs.shellcheck.enable = true;
      settings.formatter.shellcheck.priority = 0; # default is 0, but we set it here for clarity

      # shfmt second
      programs.shfmt.enable = true;
      programs.shfmt.indent_size = 0; # 0 means tabs
      settings.formatter.shfmt.priority = 1;

      programs.prettier.enable = true;

      programs.nixpkgs-fmt.enable = true;
    };
  };
}

Unfortunately this currently fails, it is looking for local.hex. Which hasn't been packaged in nix. I think local.hex just allows downloading other things, probably not what we want in nix.

We cannot download stuff in the wrapper script, being part of a nix build it doesn't have Internet access.

At this stage out of time to try to comprehend this, but thought I should document my findings.

nix flake check --override-input devenv-root "file+file://"<(printf %s "$PWD") .
warning: Git tree '/home/brian/tree/3rdparty/teslamate' is dirty
warning: not writing modified lock file of flake 'git+file:///home/brian/tree/3rdparty/teslamate':
• Updated input 'devenv-root':
    'file:///dev/null?narHash=sha256-d6xi4mKdjkX2JFicDIv5niSzpyI0m/Hnm8GGAIU04kY%3D'
  → 'file:///proc/self/fd/11?narHash=sha256-MR36Ywqs/zTxXcyqXW6MESFZ3X7OT91FF2wTpsZPoEg%3D'
error: builder for '/nix/store/mqcdlp6v87bdbvqja2r0jx3ix367wvpk-treefmt-check.drv' failed with exit code 1;
       last 15 log lines:
       > Initialized empty Git repository in /build/project/.git/
       > + set -e
       > + export MIX_REBAR3=/nix/store/d9r8459r3vr2c9g712hs1rbqmsvwwbl3-rebar3-3.24.0/bin/rebar3
       > + MIX_REBAR3=/nix/store/d9r8459r3vr2c9g712hs1rbqmsvwwbl3-rebar3-3.24.0/bin/rebar3
       > + exec /nix/store/frmyslc7ayg3afpcsnd5whn0zrhsrs6k-treefmt-2.0.5/bin/treefmt --config-file=/nix/store/6g4inxx2hj41cjgk81lazjp52sk4wvak-treefmt.toml --tree-root-file=VERSION --version
       > treefmt v2.0.5
       > + set -e
       > + export MIX_REBAR3=/nix/store/d9r8459r3vr2c9g712hs1rbqmsvwwbl3-rebar3-3.24.0/bin/rebar3
       > + MIX_REBAR3=/nix/store/d9r8459r3vr2c9g712hs1rbqmsvwwbl3-rebar3-3.24.0/bin/rebar3
       > + exec /nix/store/frmyslc7ayg3afpcsnd5whn0zrhsrs6k-treefmt-2.0.5/bin/treefmt --config-file=/nix/store/6g4inxx2hj41cjgk81lazjp52sk4wvak-treefmt.toml --tree-root-file=VERSION --no-cache
       > mix-format error:
       > Mix requires the Hex package manager to fetch dependencies
       > Shall I install Hex? (if running non-interactively, use "mix local.hex --force") [Yn] ** (Mix) Could not find an SCM for dependency :castore from TeslaMate.MixProject
       >
       > treefmt: error: formatting failure: formatter '/nix/store/r2bjg78m7b7s75lwcgn69svk010j3sqb-elixir-1.17.3/bin/mix' with options '[format]' failed to apply: exit status 1
       For full logs, run 'nix log /nix/store/mqcdlp6v87bdbvqja2r0jx3ix367wvpk-treefmt-check.drv'.
error: build of '/nix/store/iqfayabdc3qppi8xhdwy9g20nw6am7zs-vm-test-run-teslamate.drv', '/nix/store/mqcdlp6v87bdbvqja2r0jx3ix367wvpk-treefmt-check.drv' failed

Note that treefmt gets called multiple times also, the wrapper script needs to be able to cope.

Type of installation

Manual

Version

master

JakobLichterfeld commented 1 month ago

At this stage out of time to try to comprehend this, but thought I should document my findings.

Thanks for document your progress and findings.

I couldn't find anyone else using formatter.mix-format in a real project yet