Unverified Commit a7b3b258 authored by Wolfgang Walther's avatar Wolfgang Walther
Browse files

ci/treefmt: add markdown-code-runner

This was run as a test in `doc/tests/check-nix-code-blocks.nix` before,
but its DX can be improved: By including it in `treefmt` we get better
error reporting and auto-fixing, as well as running it on *all* markdown
files (including READMEs etc.) for free.
parent fed181fa
Loading
Loading
Loading
Loading
+17 −0
Original line number Diff line number Diff line
@@ -75,6 +75,23 @@ let
          includes = [ "*" ];
          priority = 1;
        };

        # TODO: Upstream this into treefmt-nix eventually:
        #   https://github.com/numtide/treefmt-nix/issues/387
        settings.formatter.markdown-code-runner = {
          command = pkgs.lib.getExe pkgs.markdown-code-runner;
          options =
            let
              config = pkgs.writers.writeTOML "markdown-code-runner-config" {
                presets.nixfmt = {
                  language = "nix";
                  command = [ (pkgs.lib.getExe pkgs.nixfmt) ];
                };
              };
            in
            [ "--config=${config}" ];
          includes = [ "*.md" ];
        };
      };
      fs = pkgs.lib.fileset;
      nixFilesSrc = fs.toSource {
+0 −1
Original line number Diff line number Diff line
@@ -137,7 +137,6 @@ stdenvNoCC.mkDerivation (

      tests = {
        manpage-urls = callPackage ../tests/manpage-urls.nix { };
        check-nix-code-blocks = callPackage ../tests/check-nix-code-blocks.nix { };
      };
    };
  }
+0 −32
Original line number Diff line number Diff line
{
  runCommand,
  markdown-code-runner,
  nixfmt-rfc-style,
}:

runCommand "manual_check-nix-code-blocks"
  {
    nativeBuildInputs = [
      markdown-code-runner
      nixfmt-rfc-style
    ];
  }
  ''
    set +e

    mdcr --check --config ${./mdcr-config.toml} ${./..}

    if [ $? -ne 0 ]; then
      cat <<EOF
    Error: `mdcr` command failed. Please make sure the Nix code snippets in Markdown files are correctly formatted.

    Run this command from the Nixpkgs repository root for automatic formatting:

        mdcr --log debug --config ${toString ./..}/tests/mdcr-config.toml ${toString ./..}

    EOF
      exit 1
    fi

    touch "$out"
  ''

doc/tests/mdcr-config.toml

deleted100644 → 0
+0 −3
Original line number Diff line number Diff line
[presets.nixfmt]
language = "nix"
command = ["nixfmt"]