From 5c6b697e55e95e13f009cbcc4dcef4d070e83aee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tristan=20Dani=C3=ABl=20Maat?= <tm@tlater.net> Date: Tue, 20 May 2025 20:37:05 +0800 Subject: [PATCH] feat(checks): Add linters --- checks/default.nix | 52 ++++++++++++++++++++++++++++++++++++++++++++++ checks/lints.nu | 39 ++++++++++++++++++++++++++++++++++ flake.nix | 2 +- 3 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 checks/default.nix create mode 100644 checks/lints.nu diff --git a/checks/default.nix b/checks/default.nix new file mode 100644 index 0000000..80279ba --- /dev/null +++ b/checks/default.nix @@ -0,0 +1,52 @@ +{ + self, + nixpkgs, + deploy-rs, + system, + ... +}: +let + pkgs = nixpkgs.legacyPackages.${system}; + + runNuCheck = + { + name, + packages, + check, + }: + pkgs.stdenvNoCC.mkDerivation { + inherit name; + + src = nixpkgs.lib.cleanSourceWith { + src = self; + filter = nixpkgs.lib.cleanSourceFilter; + }; + + dontPatch = true; + dontConfigure = true; + dontBuild = true; + dontInstall = true; + dontFixup = true; + doCheck = true; + + checkInputs = nixpkgs.lib.singleton pkgs.nushell ++ packages; + + checkPhase = '' + nu ${check} + ''; + }; +in +nixpkgs.lib.recursiveUpdate { + lints = runNuCheck { + name = "lints"; + + packages = [ + pkgs.deadnix + pkgs.nixfmt-rfc-style + pkgs.shellcheck + pkgs.statix + ]; + + check = ./lints.nu; + }; +} (deploy-rs.lib.${system}.deployChecks self.deploy) diff --git a/checks/lints.nu b/checks/lints.nu new file mode 100644 index 0000000..ffc2047 --- /dev/null +++ b/checks/lints.nu @@ -0,0 +1,39 @@ +#!/usr/bin/env nu + +let shell_files = ls **/*.sh | get name +let nix_files = ls **/*.nix | where name !~ "hardware-configuration.nix|_sources" | get name + +let linters = [ + ([shellcheck] ++ $shell_files) + ([nixfmt --check --strict] ++ $nix_files) + ([deadnix --fail] ++ $nix_files) + ([statix check] ++ $nix_files) +] + +mkdir $env.out + +def run-linter [linterArgs: list<string>] { + print $'Running ($linterArgs.0)...' + + let exit_code = try { + ^$linterArgs.0 ...($linterArgs | skip 1) + $env.LAST_EXIT_CODE + } catch {|e| $e.exit_code} + + [$linterArgs.0, $exit_code] +} + +let results = $linters | each {|linter| run-linter $linter} + +print 'Linter results:' + +let success = $results | each {|result| + match $result.1 { + 0 => {print $'(ansi green)($result.0)(ansi reset)'} + _ => {print $'(ansi red)($result.0)(ansi reset)'} + } + + $result.1 +} | math sum + +exit $success diff --git a/flake.nix b/flake.nix index bdb60ed..6747c24 100644 --- a/flake.nix +++ b/flake.nix @@ -96,7 +96,7 @@ ######### # Tests # ######### - checks = builtins.mapAttrs (system: deployLib: deployLib.deployChecks self.deploy) deploy-rs.lib; + checks.${system} = import ./checks (inputs // { inherit system; }); ########################### # Garbage collection root #