From cddef8fa682b6c305a6dfe69b8ae2fab4ed89db2 Mon Sep 17 00:00:00 2001 From: John Perez Date: Thu, 30 Mar 2023 15:56:57 -0500 Subject: [PATCH] refac(back): #1055 update lintnix - Separate arg from module - Make `lintNix` to fix files and fail if it has errors - Update docs Signed-off-by: John Perez --- docs/src/api/builtins/lint.md | 2 +- src/args/agnostic.nix | 1 + src/args/lint-nix/default.nix | 21 ++++++++++++++++++++ src/args/lint-nix/entrypoint.sh | 19 ++++++++++++++++++ src/evaluator/modules/lint-nix/builder.sh | 16 --------------- src/evaluator/modules/lint-nix/default.nix | 23 ++++------------------ 6 files changed, 46 insertions(+), 36 deletions(-) create mode 100644 src/args/lint-nix/default.nix create mode 100644 src/args/lint-nix/entrypoint.sh delete mode 100644 src/evaluator/modules/lint-nix/builder.sh diff --git a/docs/src/api/builtins/lint.md b/docs/src/api/builtins/lint.md index 800804ab..e4947831 100644 --- a/docs/src/api/builtins/lint.md +++ b/docs/src/api/builtins/lint.md @@ -192,7 +192,7 @@ Example: ## lintNix -Lints Nix code with [nix-linter](https://github.com/Synthetica9/nix-linter). +Lints Nix code with [statix](https://github.com/nerdypepper/statix). Types: diff --git a/src/args/agnostic.nix b/src/args/agnostic.nix index 4319695f..e0e90cfe 100644 --- a/src/args/agnostic.nix +++ b/src/args/agnostic.nix @@ -60,6 +60,7 @@ lintGitCommitMsg = import ./lint-git-commit-msg/default.nix self; lintGitMailMap = import ./lint-git-mailmap/default.nix self; lintMarkdown = import ./lint-markdown/default.nix self; + lintNix = import ./lint-nix/default.nix self; lintPython = import ./lint-python/default.nix self; lintPythonImports = import ./lint-python-imports/default.nix self; lintTerraform = import ./lint-terraform/default.nix self; diff --git a/src/args/lint-nix/default.nix b/src/args/lint-nix/default.nix new file mode 100644 index 00000000..920e7525 --- /dev/null +++ b/src/args/lint-nix/default.nix @@ -0,0 +1,21 @@ +{ + __nixpkgs__, + toBashArray, + makeScript, + ... +}: { + name, + targets, + ... +}: makeScript { + replace = { + __argTargets__ = toBashArray targets; + }; + name = "lint-nix-for-${name}"; + searchPaths = { + bin = [ + __nixpkgs__.statix + ]; + }; + entrypoint = ./entrypoint.sh; + } diff --git a/src/args/lint-nix/entrypoint.sh b/src/args/lint-nix/entrypoint.sh new file mode 100644 index 00000000..df110f67 --- /dev/null +++ b/src/args/lint-nix/entrypoint.sh @@ -0,0 +1,19 @@ +# shellcheck shell=bash + +function main { + export LANG=C.UTF-8 + source __argTargets__/template local targets + + info Linting Nix code \ + && for target in "${targets[@]}"; do + info Linting "${target}" \ + && if ! statix check "${args[@]}" "${target}"; then + info Source code is being fixed, the job will fail \ + && statix fix "${args[@]}" "${target}" \ + && error Failing as promissed... + fi \ + || return 1 + done +} + +main "${@}" diff --git a/src/evaluator/modules/lint-nix/builder.sh b/src/evaluator/modules/lint-nix/builder.sh deleted file mode 100644 index 196313b8..00000000 --- a/src/evaluator/modules/lint-nix/builder.sh +++ /dev/null @@ -1,16 +0,0 @@ -# shellcheck shell=bash - -function main { - export LANG=C.UTF-8 - source "${envTargets}/template" local targets - - info Linting Nix code \ - && for target in "${targets[@]}"; do - info Linting "${target}" \ - && statix check "${args[@]}" "${target}" \ - || return 1 - done \ - && touch "${out}" -} - -main "${@}" diff --git a/src/evaluator/modules/lint-nix/default.nix b/src/evaluator/modules/lint-nix/default.nix index 9279dca5..c2f66b24 100644 --- a/src/evaluator/modules/lint-nix/default.nix +++ b/src/evaluator/modules/lint-nix/default.nix @@ -1,8 +1,5 @@ { - __nixpkgs__, - toBashArray, - makeDerivation, - projectPath, + lintNix, ... }: { config, @@ -23,21 +20,9 @@ }; config = { outputs = { - "/lintNix" = lib.mkIf config.lintNix.enable (makeDerivation { - env = { - envTargets = - toBashArray - (builtins.map - projectPath - config.lintNix.targets); - }; - name = "lint-nix"; - searchPaths = { - bin = [ - __nixpkgs__.statix - ]; - }; - builder = ./builder.sh; + "/lintNix" = lib.mkIf config.lintNix.enable (lintNix { + name = "builtin"; + targets = builtins.map (rel: "." + rel) config.lintNix.targets; }); }; };