From 0ff35842087b3b16e7e41caa6f794151b47f304a Mon Sep 17 00:00:00 2001 From: seth Date: Fri, 8 Nov 2024 20:47:25 -0500 Subject: Clean up Nix Flake and packaging (#24) * refactor(nix): split package.nix from flake.nix * chore(nix): split checks; add more * ci: run all nix checks * ci: build in debug mode * chore(nix): use versionCheckHook this makes sure the version test is always run, and on the right package * chore(nix): nix-filter -> lib.fileset * feat(nix): support stable nix * ci: fix clippy check --- .github/workflows/ci.yaml | 10 +-- .github/workflows/clippy.yaml | 2 +- default.nix | 17 +++++ flake.lock | 16 ----- flake.nix | 156 ++++++++++++------------------------------ nix/package.nix | 64 +++++++++++++++++ overlay.nix | 3 + 7 files changed, 132 insertions(+), 136 deletions(-) create mode 100644 default.nix create mode 100644 nix/package.nix create mode 100644 overlay.nix diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index b2d5df4..0ba71ce 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -41,10 +41,10 @@ jobs: - name: Run build run: | - nix build --print-build-logs .#check-version-test + nix build --print-build-logs .#nix-forecast-debug - format: - name: Formatting + format-and-lint: + name: Format and lint runs-on: ubuntu-latest @@ -60,11 +60,11 @@ jobs: - name: Run flake checks run: | - nix build --print-build-logs .#check-formatting + nix flake check --print-build-logs --show-trace release-gate: name: CI Release gate - needs: [build, format] + needs: [build, format-and-lint] if: ${{ always() }} diff --git a/.github/workflows/clippy.yaml b/.github/workflows/clippy.yaml index 32668a8..32f2f11 100644 --- a/.github/workflows/clippy.yaml +++ b/.github/workflows/clippy.yaml @@ -40,7 +40,7 @@ jobs: - name: Run clippy id: clippy-run run: | - nix build --print-build-logs .#check-clippy + nix build --print-build-logs .#checks.x86_64-linux.clippy [ -L result ] || exit 1 echo "sarif-file=$(readlink -f result)" >> "$GITHUB_OUTPUT" diff --git a/default.nix b/default.nix new file mode 100644 index 0000000..3730822 --- /dev/null +++ b/default.nix @@ -0,0 +1,17 @@ +{ + pkgs ? import nixpkgs { + inherit system; + config = { }; + overlays = [ ]; + }, + lib ? pkgs.lib, + nixpkgs ? , + system ? builtins.currentSystem, +}: + +let + nixForecastPackages = lib.makeScope pkgs.newScope (lib.flip (import ./overlay.nix) pkgs); +in +{ + inherit (nixForecastPackages) nix-forecast; +} diff --git a/flake.lock b/flake.lock index 3e87197..9191749 100644 --- a/flake.lock +++ b/flake.lock @@ -1,20 +1,5 @@ { "nodes": { - "nix-filter": { - "locked": { - "lastModified": 1730207686, - "narHash": "sha256-SCHiL+1f7q9TAnxpasriP6fMarWE5H43t25F5/9e28I=", - "owner": "numtide", - "repo": "nix-filter", - "rev": "776e68c1d014c3adde193a18db9d738458cd2ba4", - "type": "github" - }, - "original": { - "owner": "numtide", - "repo": "nix-filter", - "type": "github" - } - }, "nixpkgs": { "locked": { "lastModified": 1730958623, @@ -33,7 +18,6 @@ }, "root": { "inputs": { - "nix-filter": "nix-filter", "nixpkgs": "nixpkgs" } } diff --git a/flake.nix b/flake.nix index 040f5a3..33193cf 100644 --- a/flake.nix +++ b/flake.nix @@ -1,16 +1,12 @@ { description = "Check the forecast for today's Nix builds"; - inputs = { - nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; - nix-filter.url = "github:numtide/nix-filter"; - }; + inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; outputs = { self, nixpkgs, - nix-filter, }: let inherit (nixpkgs) lib; @@ -36,16 +32,24 @@ let pkgs = nixpkgsFor.${system}; packages = self.packages.${system}; + + mkCheck = + name: deps: script: + pkgs.runCommand name { nativeBuildInputs = deps; } '' + ${script} + touch $out + ''; in lib.optionalAttrs (lib.elem system supportedSystems) { - version-test = packages.nix-forecast.tests.version; - - clippy = packages.nix-forecast.overrideAttrs (oldAttrs: { + clippy = packages.nix-forecast.overrideAttrs { pname = "check-clippy"; - nativeBuildInputs = oldAttrs.nativeBuildInputs ++ [ + nativeBuildInputs = [ + pkgs.cargo pkgs.clippy pkgs.clippy-sarif + pkgs.rustPlatform.cargoSetupHook + pkgs.rustc pkgs.sarif-fmt ]; @@ -57,36 +61,32 @@ --tests \ --message-format=json \ | clippy-sarif | tee $out | sarif-fmt + runHook postBuild ''; dontInstall = true; doCheck = false; + doInstallCheck = false; dontFixup = true; passthru = { }; meta = { }; - }); - - formatting = - pkgs.runCommand "check-formatting" - { - nativeBuildInputs = [ - pkgs.cargo - pkgs.nixfmt-rfc-style - pkgs.rustfmt - ]; - } - '' - cd ${self} - - echo "Running cargo fmt" - cargo fmt -- --check - - echo "Running nixfmt..." - nixfmt --check . - - touch $out - ''; + }; + + rustfmt = mkCheck "check-cargo-fmt" [ + pkgs.cargo + pkgs.rustfmt + ] "cd ${self} && cargo fmt -- --check"; + + actionlint = mkCheck "check-actionlint" [ + pkgs.actionlint + ] "actionlint ${self}/.github/workflows/*"; + + deadnix = mkCheck "check-deadnix" [ pkgs.deadnix ] "deadnix --fail ${self}"; + + nixfmt = mkCheck "check-nixfmt" [ pkgs.nixfmt-rfc-style ] "nixfmt --check ${self}"; + + statix = mkCheck "check-statix" [ pkgs.statix ] "statix check ${self}"; } ); @@ -120,96 +120,24 @@ formatter = forAllSystems (system: nixpkgsFor.${system}.nixfmt-rfc-style); - # for CI - legacyPackages = forAllSystems ( - system: - lib.optionalAttrs (lib.elem system supportedSystems) ( - lib.mapAttrs' (name: lib.nameValuePair "check-${name}") self.checks.${system} - ) - ); + legacyPackages = forAllSystems (system: { + nix-forecast-debug = self.packages.${system}.nix-forecast.overrideAttrs ( + finalAttrs: _: { + cargoBuildType = "debug"; + cargoCheckType = finalAttrs.cargoBuildType; + } + ); + }); packages = forAllSystems ( system: let pkgs = nixpkgsFor.${system}; - nixForecastPackages = lib.makeScope pkgs.newScope (final: self.overlays.default final pkgs); + pkgs' = import ./default.nix { inherit pkgs; }; in - { - inherit (nixForecastPackages) nix-forecast; - default = self.packages.${system}.nix-forecast; - } + pkgs' // { default = pkgs'.nix-forecast; } ); - overlays.default = final: _: { - nix-forecast = final.callPackage ( - { - lib, - stdenv, - rustPlatform, - darwin, - installShellFiles, - makeBinaryWrapper, - nix, - nix-forecast, - testers, - }: - - rustPlatform.buildRustPackage rec { - pname = "nix-forecast"; - inherit (passthru.cargoTOML.package) version; - - src = nix-filter { - root = self; - include = [ - ./Cargo.toml - ./Cargo.lock - ./build.rs - "src" - ]; - }; - - cargoLock.lockFile = ./Cargo.lock; - - nativeBuildInputs = [ - installShellFiles - makeBinaryWrapper - ]; - - buildInputs = lib.optionals stdenv.hostPlatform.isDarwin [ - darwin.apple_sdk.frameworks.CoreFoundation - darwin.apple_sdk.frameworks.SystemConfiguration - darwin.libiconv - ]; - - postInstall = '' - wrapProgram $out/bin/nix-forecast --suffix PATH : "${lib.makeBinPath [ nix ]}" - - installShellCompletion --cmd nix-forecast \ - --bash completions/nix-forecast.bash \ - --fish completions/nix-forecast.fish \ - --zsh completions/_nix-forecast - ''; - - env = { - COMPLETION_DIR = "completions"; - }; - - passthru = { - cargoTOML = lib.importTOML ./Cargo.toml; - - tests.version = testers.testVersion { package = nix-forecast; }; - }; - - meta = { - description = "Check the forecast for today's Nix builds"; - homepage = "https://github.com/getchoo/nix-forecast"; - changelog = "https://github.com/getchoo/nix-forecast/releases/tag/${version}"; - license = lib.licenses.mpl20; - maintainers = with lib.maintainers; [ getchoo ]; - mainProgram = "nix-forecast"; - }; - } - ) { }; - }; + overlays.default = final: prev: import ./overlay.nix final prev; }; } diff --git a/nix/package.nix b/nix/package.nix new file mode 100644 index 0000000..cc60573 --- /dev/null +++ b/nix/package.nix @@ -0,0 +1,64 @@ +{ + lib, + installShellFiles, + makeBinaryWrapper, + nix, + rustPlatform, + versionCheckHook, +}: + +let + fs = lib.fileset; +in +rustPlatform.buildRustPackage rec { + pname = "nix-forecast"; + inherit (passthru.cargoTOML.package) version; + + src = fs.toSource { + root = ../.; + fileset = fs.intersection (fs.gitTracked ../.) ( + fs.unions [ + ../Cargo.lock + ../Cargo.toml + ../build.rs + ../src + ] + ); + }; + + cargoLock.lockFile = ../Cargo.lock; + + nativeBuildInputs = [ + installShellFiles + makeBinaryWrapper + ]; + + doInstallCheck = true; + nativeInstallCheckInputs = [ versionCheckHook ]; + + postInstall = '' + wrapProgram $out/bin/nix-forecast --suffix PATH : ${lib.makeBinPath [ nix ]} + + installShellCompletion --cmd nix-forecast \ + --bash completions/nix-forecast.bash \ + --fish completions/nix-forecast.fish \ + --zsh completions/_nix-forecast + ''; + + env = { + COMPLETION_DIR = "completions"; + }; + + passthru = { + cargoTOML = lib.importTOML ../Cargo.toml; + }; + + meta = { + description = "Check the forecast for today's Nix builds"; + homepage = "https://github.com/getchoo/nix-forecast"; + changelog = "https://github.com/getchoo/nix-forecast/releases/tag/${version}"; + license = lib.licenses.mpl20; + maintainers = with lib.maintainers; [ getchoo ]; + mainProgram = "nix-forecast"; + }; +} diff --git a/overlay.nix b/overlay.nix new file mode 100644 index 0000000..8bdda9d --- /dev/null +++ b/overlay.nix @@ -0,0 +1,3 @@ +final: _: { + nix-forecast = final.callPackage ./nix/package.nix { }; +} -- cgit v1.2.3