Skip to content

Instantly share code, notes, and snippets.

@pbsds
Last active June 12, 2025 11:40
Show Gist options
  • Select an option

  • Save pbsds/99efaef277e3e3d75904c898065497fc to your computer and use it in GitHub Desktop.

Select an option

Save pbsds/99efaef277e3e3d75904c898065497fc to your computer and use it in GitHub Desktop.
#!/usr/bin/env nix-shell
#!/nix-shell --pure -i bash -p git git-wait nix gum
set -euo pipefail
export GIT_WAIT_TIMEOUT_MS=5000
trace() ( set -x; "$@"; )
die() { echo >&2 "ERROR:" "$@"; exit 1; }
git diff HEAD --quiet --exit-code || die "Your worktree is dirty, please commit your changes first."
# Fetch upstream branch chosen by user
upstream_alternatives=( master staging{,-next} {release,staging{,-next}}-25.05 )
upstream_branch="${upstream_alternatives[0]}"
[[ -t 0 ]] && upstream_branch=$(gum choose --header="Select target upstream branch" "${upstream_alternatives[@]}")
[[ -n "$upstream_branch" ]] || die "No target upstream branch selected"
trace git-wait fetch https://github.com/NixOS/nixpkgs.git "$upstream_branch"
upstream_commit=$(trace git-wait rev-parse FETCH_HEAD)
[[ -n "$upstream_commit" ]] || die "Could not determine upstream branch commit id"
echo >&2 "Upstream '$upstream_branch' commit id: $upstream_commit"
# Make temporary merge commit
merged_tree_id=$(trace git merge-tree --write-tree "$upstream_commit" HEAD ) \
&& [[ -n "$merged_tree_id" ]] || die "Unable to create merged tree"
merged_commit=$(
{
echo "Merge commit"
echo ""
echo "Merge of $upstream_commit and $(trace git branch --show-current || trace git rev-parse HEAD || echo HEAD)"
} | trace git-wait commit-tree "$merged_tree_id" -p "$upstream_commit" -p HEAD
) && [[ -n "$merged_commit" ]] || die "Unable to create merged commit"
echo >&2 "Temporary merge commit id: $merged_commit"
# checkout target upstream and merged commit as worktrees
trace git-wait worktree prune
# these could also be put in "${TMPDIR:-/tmp}", "${XDG_CACHE_HOME:-"$HOME/.cache/"}" or "$XDG_RUNTIME_DIR"
dot_git="$(realpath "$(trace git-wait rev-parse --git-common-dir)")"
trap 'trace rm -rf "$dot_git"/nixpkgs-ci-tmp/commit-{"$upstream_commit","$merged_commit"};' EXIT ERR
for commit_id in "$upstream_commit" "$merged_commit"; do
[[ -d "$dot_git"/nixpkgs-ci-tmp/commit-"$commit_id" ]] ||
trace git-wait worktree add "$dot_git"/nixpkgs-ci-tmp/commit-"$commit_id"/ "$commit_id"
done
trace nix-build ci \
--arg base "$dot_git"/nixpkgs-ci-tmp/commit-"$upstream_commit" \
--arg merged "$dot_git"/nixpkgs-ci-tmp/commit-"$merged_commit"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment