Skip to content

Commit

Permalink
Add pre-push hook, set up in activate.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
bryango committed Sep 30, 2024
1 parent 227e054 commit 805c1eb
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
15 changes: 13 additions & 2 deletions activate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@

export PATH="$HOME/.nix-profile/bin:$PATH"

CHEZNIX="$HOME/.config/home-manager"

# the following commands will be echoed
set -x

cd "$HOME" || exit
cd "$HOME" || exit 1
chezmoi init --ssh bryango/chezmoi

# ensure that `home-attrs` is cached
nix eval --raw cheznix#cheznix.inputs.home-attrs.outPath | cachix push chezbryan &

# chores
Expand All @@ -17,7 +21,14 @@ git diff --color=always --no-index \

# the following commands will be silent
set +x
nix profile list --json | jq > ~/.config/home-manager/profile.json
nix profile list --json | jq > "$CHEZNIX/profile.json"

>&2 echo
>&2 printf "## installing git hooks ... "
pushd "$CHEZNIX/.git/hooks" &>/dev/null || exit 1
ln -sf ../../pre-push pre-push
popd &>/dev/null || exit 1
>&2 echo "completed."

>&2 cat <<- EOF
Expand Down
47 changes: 47 additions & 0 deletions pre-push
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env nix
#! nix env shell nixpkgs#bash nixpkgs#gh nixpkgs#jq nixpkgs#gh --command bash
# check if the dependent `nixpkgs-config` is successfully built with github actions
# shellcheck shell=bash

# This hook is called with the following parameters:
#
# $1 -- Name of the remote to which the push is being done
# $2 -- URL to which the push is being done
#
# If pushing without using a named remote those arguments will be equal.
#
# Information about the commits which are being pushed is supplied as lines to
# the standard input in the form:
#
# <local ref> <local oid> <remote ref> <remote oid>

set -e

while read -r local_ref local_oid remote_ref remote_oid
do
>&2 printf "pre-push: %s\t%s\n" "$local_ref" "$local_oid"
>&2 printf " -> %s\t%s\n" "$remote_ref" "$remote_oid"

rev=$(nix flake metadata --json | jq --raw-output '.locks.nodes."nixpkgs-config".locked.rev')
>&2 echo "flake.lock: nixpkgs-config @ $rev"

cd nixpkgs-config || exit 1
gh_runs=$(gh run list --commit "$rev" --json attempt,conclusion,databaseId,displayTitle,headSha,status,updatedAt,url)
echo "$gh_runs" | jq 'sort_by(.databaseId) | reverse'

latest_status=$(echo "$gh_runs" | jq --raw-output 'sort_by(.databaseId) | reverse | .[0].status')
if [[ "$latest_status" != "completed" ]]; then
>&2 echo "nixpkgs-config: $latest_status, not completed"
exit 1
fi

latest_conclusion=$(echo "$gh_runs" | jq --raw-output 'sort_by(.databaseId) | reverse | .[0].conclusion')
if [[ "$latest_conclusion" != "success" ]]; then
>&2 echo "nixpkgs-config: $latest_conclusion, not successful"
exit 1
fi
done

exit 0

# vim: ft=sh
2 changes: 1 addition & 1 deletion profile.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"active": true,
"priority": 5,
"storePaths": [
"/nix/store/775sjz37pzpmrdd3wzbmhcmjx3m2vk04-home-manager-path"
"/nix/store/mcdrzisivvzplw8asvf9wns1rwkmjx96-home-manager-path"
]
}
},
Expand Down

0 comments on commit 805c1eb

Please sign in to comment.