From 383224651fd267c151eb1d35756e86c41ea0766f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dawid=20Ci=C4=99=C5=BCarkiewicz?= Date: Thu, 19 Nov 2026 18:17:00 -0800 Subject: [PATCH] feat(lint): add opt-in YAML and GitHub Actions checks Fedimint added YAML and workflow checks in fedimint/fedimint#9290. Expose them as independent Flakebox options so projects can enable either without writing a custom hook or adding both linters to their dev shell. Use the pre-commit hook's tracked text inventory to respect exclusions and pass filenames without shell splitting. Yamllint checks syntax and duplicate keys without enforcing style. Actionlint checks only direct workflow files and uses ShellCheck independently of the standalone hook. Leave Docker Compose and Tokio lint rules to individual projects. ### Review Code review caught a nonportable ShellCheck store path in the generated hook. Resolve ShellCheck at runtime and report a missing binary explicitly. Editorial review clarified how to opt in and run the checks. --- checks/git-hooks.nix | 106 +++++++++++++++++++++++++++++++++++++ docs/best-practices.md | 23 ++++++++ lib/modules/actionlint.nix | 40 ++++++++++++++ lib/modules/yamllint.nix | 23 ++++++++ 4 files changed, 192 insertions(+) create mode 100644 lib/modules/actionlint.nix create mode 100644 lib/modules/yamllint.nix diff --git a/checks/git-hooks.nix b/checks/git-hooks.nix index dc85b9c..270623f 100644 --- a/checks/git-hooks.nix +++ b/checks/git-hooks.nix @@ -69,6 +69,23 @@ let inherit (flakeboxLib.config.git.pre-commit.hooks) shellcheck; }; + yamlLintConfig = + (mkLib pkgs { + config = { + shellcheck.enable = false; + yamllint.enable = true; + }; + }).config; + actionLintConfig = + (mkLib pkgs { + config = { + shellcheck.enable = false; + actionlint.enable = true; + }; + }).config; + yamlLintHooks = mkHookFixture { inherit (yamlLintConfig.git.pre-commit.hooks) yamllint; }; + actionLintHooks = mkHookFixture { inherit (actionLintConfig.git.pre-commit.hooks) actionlint; }; + newlineLib = mkLib pkgs { config.git.pre-commit = { trailing_newline = true; @@ -184,6 +201,15 @@ let emptyHooks = mkHookFixture { }; in assert pkgs.lib.elem timer flakeboxLib.config.env.shellPackages; +assert !(flakeboxLib.config.git.pre-commit.hooks ? yamllint); +assert !(flakeboxLib.config.git.pre-commit.hooks ? actionlint); +assert !(pkgs.lib.elem pkgs.yamllint flakeboxLib.config.env.shellPackages); +assert !(pkgs.lib.elem pkgs.actionlint flakeboxLib.config.env.shellPackages); +assert !(yamlLintConfig.git.pre-commit.hooks ? actionlint); +assert !(actionLintConfig.git.pre-commit.hooks ? yamllint); +assert pkgs.lib.elem pkgs.yamllint yamlLintConfig.env.shellPackages; +assert pkgs.lib.elem pkgs.actionlint actionLintConfig.env.shellPackages; +assert pkgs.lib.elem pkgs.shellcheck actionLintConfig.env.shellPackages; assert flakeboxLib.config.git.enable; assert gitDisabledLib.config.git.pre-commit.enable; assert gitDisabledLib.config.git.commit-msg.enable; @@ -1209,6 +1235,86 @@ pkgs.runCommand "git-hooks-tests" [ "$(grep -c '^invocation$' "$FLAKEBOX_SHELLCHECK_LOG")" -eq 2 ] cd .. + # Exercise the real tools: malformed tracked input fails, but formatting, + # ignored paths, and empty selections do not. + ( + export PATH="${ + pkgs.lib.makeBinPath [ + pkgs.yamllint + pkgs.actionlint + pkgs.shellcheck + ] + }:$PATH" + mkdir yaml-repo + cd yaml-repo + git init -q + mkdir -p .github/workflows/nested + run_yamllint() { + NO_STASH=1 bash ${yamlLintHooks}/misc/git-hooks/pre-commit + } + run_actionlint() { + NO_STASH=1 bash ${actionLintHooks}/misc/git-hooks/pre-commit + } + + printf 'broken: [\n' > .github/workflows/untracked.yml + ln -s untracked.yml .github/workflows/symlink.yml + git add .github/workflows/symlink.yml + run_yamllint + run_actionlint + + yaml_path=$'-odd file[0]\n.yaml' + printf 'key: value \n' > "$yaml_path" + git add -- "$yaml_path" + run_yamllint + + printf 'key: value\nkey: duplicate\n' > "$yaml_path" + if run_yamllint > duplicate.log 2>&1; then + echo 'yamllint accepted a duplicate key' >&2 + exit 1 + fi + grep -F 'key-duplicates' duplicate.log + FLAKEBOX_GIT_LS_IGNORE='^-odd' run_yamllint + FLAKEBOX_GIT_LS_TEXT_IGNORE='^-odd' run_yamllint + + printf 'broken: [\n' > "$yaml_path" + if run_yamllint > syntax.log 2>&1; then + echo 'yamllint accepted invalid YAML' >&2 + exit 1 + fi + grep -F 'syntax' syntax.log + + workflow_path=$'.github/workflows/odd file[0]\n.yml' + printf 'not a workflow\n' > .github/workflows/nested/not-a-workflow.yaml + cat > "$workflow_path" <<'EOF' + on: push + jobs: + check: + runs-on: ubuntu-latest + steps: + - run: echo ok + EOF + git add -- "$workflow_path" .github/workflows/nested/not-a-workflow.yaml + run_actionlint + + sed -i 's/ubuntu-latest/unknown-runner/' "$workflow_path" + if run_actionlint > workflow.log 2>&1; then + echo 'actionlint accepted an unknown runner label' >&2 + exit 1 + fi + grep -F 'label "unknown-runner" is unknown' workflow.log + FLAKEBOX_GIT_LS_IGNORE='^\.github/workflows/odd' run_actionlint + FLAKEBOX_GIT_LS_TEXT_IGNORE='^\.github/workflows/odd' run_actionlint + + printf 'self-hosted-runner: {labels: [unknown-runner]}\n' > .github/actionlint.yaml + run_actionlint + sed -i 's/echo ok/echo $UNQUOTED/' "$workflow_path" + if run_actionlint > inline-shell.log 2>&1; then + echo 'actionlint did not run ShellCheck on the inline script' >&2 + exit 1 + fi + grep -F 'SC2086' inline-shell.log + ) + # The newline hook sees all tracked files and uses raw path records, not # Git's quoted display or shell word splitting. mkdir newline-repo diff --git a/docs/best-practices.md b/docs/best-practices.md index a9cf8a5..9242664 100644 --- a/docs/best-practices.md +++ b/docs/best-practices.md @@ -132,6 +132,29 @@ Customize the generated `.cargo-crap.toml` by setting with `cargo-crap.enable = false`. +## Check YAML and GitHub Actions workflows + +Enable either or both checks in your Flakebox configuration: + +```nix +yamllint.enable = true; +actionlint.enable = true; +``` + +- `yamllint` checks tracked `.yaml` and `.yml` files for syntax and duplicate + keys only. It does not apply formatting rules or load a project's + `yamllint` configuration. +- `actionlint` checks tracked `.yaml` and `.yml` files directly under + `.github/workflows/`. It uses ShellCheck for inline shell scripts even if + Flakebox's standalone `shellcheck` integration is disabled. + +Both checks are off by default. They use the pre-commit hook's tracked text +file inventory, respect `FLAKEBOX_GIT_LS_IGNORE` and +`FLAKEBOX_GIT_LS_TEXT_IGNORE`, and skip symbolic links and files Git doesn't +track. Run `flakebox install` to regenerate the hook, then run +`bash misc/git-hooks/pre-commit` to check files manually. + + ## Enforce structural rules with ast-grep Flakebox installs [ast-grep](https://ast-grep.github.io/) and runs diff --git a/lib/modules/actionlint.nix b/lib/modules/actionlint.nix new file mode 100644 index 0000000..0448079 --- /dev/null +++ b/lib/modules/actionlint.nix @@ -0,0 +1,40 @@ +{ + lib, + config, + pkgs, + ... +}: +{ + options.actionlint.enable = lib.mkEnableOption "GitHub Actions workflow checks, including ShellCheck for inline shell scripts, in the pre-commit hook"; + + config = lib.mkIf config.actionlint.enable { + env.shellPackages = [ + pkgs.actionlint + pkgs.shellcheck + ]; + + git.pre-commit.hooks.actionlint = '' + local workflows=() path shellcheck_path + while LC_ALL=C IFS= read -r -d "" path; do + case "$path" in + .github/workflows/*/*) ;; + .github/workflows/*.yaml | .github/workflows/*.yml) + if [[ -f "$path" ]]; then + workflows+=("$path") + fi + ;; + esac + done < "$FLAKEBOX_GIT_LS_TEXT_FILE" + + if (( ''${#workflows[@]} == 0 )); then + return 0 + fi + shellcheck_path="$(command -v shellcheck)" || { + echo "flakebox: actionlint requires shellcheck on PATH" >&2 + return 1 + } + printf '%s\0' "''${workflows[@]}" | + xargs -0 actionlint -shellcheck "$shellcheck_path" -pyflakes= -- + ''; + }; +} diff --git a/lib/modules/yamllint.nix b/lib/modules/yamllint.nix new file mode 100644 index 0000000..02925fe --- /dev/null +++ b/lib/modules/yamllint.nix @@ -0,0 +1,23 @@ +{ + lib, + config, + pkgs, + ... +}: +{ + options.yamllint.enable = lib.mkEnableOption "YAML syntax and duplicate-key checks for tracked YAML files in the pre-commit hook"; + + config = lib.mkIf config.yamllint.enable { + env.shellPackages = [ pkgs.yamllint ]; + + git.pre-commit.hooks.yamllint = '' + local path + while LC_ALL=C IFS= read -r -d "" path; do + if [[ "$path" == *.yaml || "$path" == *.yml ]] && [[ -f "$path" ]]; then + printf '%s\0' "$path" + fi + done < "$FLAKEBOX_GIT_LS_TEXT_FILE" | + xargs -0 -r yamllint -d '{rules: {key-duplicates: enable}}' -- + ''; + }; +}