1 # Checks pkgs/by-name (see pkgs/by-name/README.md)
2 # using the nixpkgs-check-by-name tool (see pkgs/test/nixpkgs-check-by-name)
3 name: Check pkgs/by-name
5 # The pre-built tool is fetched from a channel,
6 # making it work predictable on all PRs.
8 # Using pull_request_target instead of pull_request avoids having to approve first time contributors
11 # The tool doesn't need any permissions, it only outputs success or not based on the checkout
16 # This is x86_64-linux, for which the tool is always prebuilt on the nixos-* channels,
17 # as specified in nixos/release-combined.nix
18 runs-on: ubuntu-latest
20 - name: Resolving the merge commit
22 GH_TOKEN: ${{ github.token }}
24 # This checks for mergeability of a pull request as recommended in
25 # https://docs.github.com/en/rest/guides/using-the-rest-api-to-interact-with-your-git-database?apiVersion=2022-11-28#checking-mergeability-of-pull-requests
27 echo "Checking whether the pull request can be merged"
29 -H "Accept: application/vnd.github+json" \
30 -H "X-GitHub-Api-Version: 2022-11-28" \
31 /repos/"$GITHUB_REPOSITORY"/pulls/${{ github.event.pull_request.number }})
32 mergeable=$(jq -r .mergeable <<< "$prInfo")
33 mergedSha=$(jq -r .merge_commit_sha <<< "$prInfo")
35 if [[ "$mergeable" == "null" ]]; then
36 # null indicates that GitHub is still computing whether it's mergeable
37 # Wait a couple seconds before trying again
38 echo "GitHub is still computing whether this PR can be merged, waiting 5 seconds before trying again"
45 if [[ "$mergeable" == "true" ]]; then
46 echo "The PR can be merged, checking the merge commit $mergedSha"
48 echo "The PR cannot be merged, it has a merge conflict"
51 echo "mergedSha=$mergedSha" >> "$GITHUB_ENV"
52 - uses: actions/checkout@v4
54 # pull_request_target checks out the base branch by default
55 ref: ${{ env.mergedSha }}
56 # Fetches the merge commit and its parents
58 - name: Determining PR git hashes
60 # For pull_request_target this is the same as $GITHUB_SHA
61 echo "baseSha=$(git rev-parse HEAD^1)" >> "$GITHUB_ENV"
63 echo "headSha=$(git rev-parse HEAD^2)" >> "$GITHUB_ENV"
64 - uses: cachix/install-nix-action@v23
65 - name: Determining channel to use for dependencies
67 echo "Determining which channel to use for PR base branch $GITHUB_BASE_REF"
68 if [[ "$GITHUB_BASE_REF" =~ ^(release|staging|staging-next)-([0-9][0-9]\.[0-9][0-9])$ ]]; then
69 # Use the release channel for all PRs to release-XX.YY, staging-XX.YY and staging-next-XX.YY
70 channel=nixos-${BASH_REMATCH[2]}
71 echo "PR is for a release branch, using release channel $channel"
73 # Use the nixos-unstable channel for all other PRs
74 channel=nixos-unstable
75 echo "PR is for a non-release branch, using unstable channel $channel"
77 echo "channel=$channel" >> "$GITHUB_ENV"
78 - name: Fetching latest version of channel
80 echo "Fetching latest version of channel $channel"
81 # This is probably the easiest way to get Nix to output the path to a downloaded channel!
82 nixpkgs=$(nix-instantiate --find-file nixpkgs -I nixpkgs=channel:"$channel")
83 # This file only exists in channels
84 rev=$(<"$nixpkgs"/.git-revision)
85 echo "Channel $channel is at revision $rev"
86 echo "nixpkgs=$nixpkgs" >> "$GITHUB_ENV"
87 echo "rev=$rev" >> "$GITHUB_ENV"
88 - name: Fetching pre-built nixpkgs-check-by-name from the channel
90 echo "Fetching pre-built nixpkgs-check-by-name from channel $channel at revision $rev"
91 # Passing --max-jobs 0 makes sure that we won't build anything
92 nix-build "$nixpkgs" -A tests.nixpkgs-check-by-name --max-jobs 0
93 - name: Running nixpkgs-check-by-name
95 echo "Checking whether the check succeeds on the base branch $GITHUB_BASE_REF"
96 git checkout -q "$baseSha"
97 if baseOutput=$(result/bin/nixpkgs-check-by-name . 2>&1); then
102 printf "%s\n" "$baseOutput"
104 echo "Checking whether the check would succeed after merging this pull request"
105 git checkout -q "$mergedSha"
106 if mergedOutput=$(result/bin/nixpkgs-check-by-name . 2>&1); then
113 printf "%s\n" "$mergedOutput"
116 if [[ -n "$1" ]]; then
117 echo ":heavy_check_mark:"
123 # Print a markdown summary in GitHub actions
125 echo "| Nixpkgs version | Check result |"
127 echo "| Latest base commit | $(resultToEmoji "$baseSuccess") |"
128 echo "| After merging this PR | $(resultToEmoji "$mergedSuccess") |"
131 if [[ -n "$baseSuccess" ]]; then
132 if [[ -n "$mergedSuccess" ]]; then
133 echo "The check succeeds on both the base branch and after merging this PR"
135 echo "The check succeeds on the base branch, but would fail after merging this PR:"
142 if [[ -n "$mergedSuccess" ]]; then
143 echo "The check fails on the base branch, but this PR fixes it, nicely done!"
145 echo "The check fails on both the base branch and after merging this PR, unknown if only this PRs changes would satisfy the check, the base branch needs to be fixed first."
147 echo "Failure on the base branch:"
152 echo "Failure after merging this PR:"
161 echo "- nixpkgs-check-by-name tool:"
162 echo " - Channel: $channel"
163 echo " - Nixpkgs commit: [$rev](https://github.com/${GITHUB_REPOSITORY}/commit/$rev)"
164 echo " - Store path: \`$(realpath result)\`"
165 echo "- Tested Nixpkgs:"
166 echo " - Base branch: $GITHUB_BASE_REF"
167 echo " - Latest base branch commit: [$baseSha](https://github.com/${GITHUB_REPOSITORY}/commit/$baseSha)"
168 echo " - Latest PR commit: [$headSha](https://github.com/${GITHUB_REPOSITORY}/commit/$headSha)"
169 echo " - Merge commit: [$mergedSha](https://github.com/${GITHUB_REPOSITORY}/commit/$mergedSha)"
170 } >> "$GITHUB_STEP_SUMMARY"