Generalize bin/hey

It'll operate out of /etc/nixos or /etc/dotfiles, rather than where the
script lives. This allows it to be used for flakes instantiated from our
templates.
This commit is contained in:
Henrik Lissner 2021-01-22 04:18:57 -05:00
parent abf2aeb689
commit 64bec0f415

62
bin/hey
View file

@ -35,7 +35,14 @@
# hey -e htop # hey -e htop
set -e set -e
DOTFILES=$(cd "${0:a:h}/.." && pwd -P) if [[ -z $FLAKE ]]; then
for dir in /etc/nixos /etc/dotfiles; do
if [[ -f "$dir/flake.nix" ]]; then
FLAKE=$dir
break
fi
done
fi
# #
function main { function main {
@ -63,10 +70,10 @@ function main {
function _rebuild { function _rebuild {
# Move to $DOTFILES to prevent polluting the system with result links # Move to $DOTFILES to prevent polluting the system with result links
pushd $DOTFILES >/dev/null pushd $FLAKE >/dev/null
# REVIEW Use --impure instead when it's in a stable release of nix # REVIEW Use --impure instead when it's in a stable release of nix
_do nixos-rebuild \ _do ${SUDO:+sudo} nixos-rebuild \
--flake "${DOTFILES}#${HOSTNAME:-$HOST}" \ --flake "${FLAKE}#${HOSTNAME:-$HOST}" \
--option pure-eval no \ --option pure-eval no \
${@:-build} ${@:-build}
popd >/dev/null popd >/dev/null
@ -76,10 +83,10 @@ function main {
echo "Updating NixOS flake" echo "Updating NixOS flake"
if [[ "$@" ]]; then if [[ "$@" ]]; then
for flake in $@; do for flake in $@; do
_do nix flake update --update-input "$flake" "$DOTFILES" _do nix flake update --update-input "$flake" "$FLAKE"
done done
else else
_do nix flake update --recreate-lock-file "$DOTFILES" _do nix flake update --recreate-lock-file "$FLAKE"
fi fi
} }
@ -136,39 +143,54 @@ function main {
} }
### ###
eval set -- $(getopt -n hey -o df:h: -l dryrun,flake:,host: -- "$@")
while :
do
case "$1" in
-d|--dryrun) DRYRUN=1; shift ;;
-f|--flake) FLAKE=$2; shift 2 ;;
-h|--host) HOSTNAME=$2; shift 2 ;;
--) shift; break ;;
esac
done
if [[ ! -d $FLAKE ]]; then
>&2 echo "Couldn't find flake.nix in $FLAKE"
exit 1
fi
cmd="$1" cmd="$1"
[[ $# > 0 ]] && shift [[ $# > 0 ]] && shift
case "$cmd" in case "$cmd" in
sh|shell) _do nix develop "$DOTFILES" "$@" ;; sh|shell) _do nix develop "$FLAKE" "$@" ;;
repl) _do nix repl "$DOTFILES/flake.nix" "<nixpkgs>" "$@" ;; repl) _do nix repl "$FLAKE/flake.nix" "<nixpkgs>" "$@" ;;
ch|check) _do nix flake check "$@" "$DOTFILES" ;; ch|check) _do nix flake check "$@" "$FLAKE" ;;
sh|show) _do nix flake show "$@" "$DOTFILES" ;; sh|show) _do nix flake show "$@" "$FLAKE" ;;
theme) THEME="$1" sudo _rebuild --fast test ;; theme) THEME="$1" SUDO=1 _rebuild --fast test ;;
t|test) sudo _rebuild --fast test "$@" ;; t|test) SUDO=1 _rebuild --fast test "$@" ;;
re|rebuild) sudo _rebuild "${@:-switch}" ;; re|rebuild) SUDO=1 _rebuild "${@:-switch}" ;;
rollback) sudo _rebuild --rollback switch ;; rollback) SUDO=1 _rebuild --rollback switch ;;
vm) sudo _rebuild "$@" vm ;; vm) SUDO=1 _rebuild "$@" vm ;;
u|update) _update "$@" ;; u|update) _update "$@" ;;
up|upgrade) _update && sudo _rebuild switch ;; up|upgrade) _update && SUDO=1 _rebuild switch ;;
s|search) nix search nixpkgs "$@" ;; s|search) nix search nixpkgs "$@" ;;
swap) _swap $@ ;; swap) _swap "$@" ;;
gc) _gc "$@" ;; gc) _gc "$@" ;;
push) push)
target=$1 target=$1
cmd=${2:-test} cmd=${2:-test}
echo "Enabling root ssh access on $target" echo "Enabling root ssh access on $target"
if _do ssh -t "$target" "sudo mkdir -p /root/.ssh && sudo cp -f ~/.ssh/authorized_keys /root/.ssh/authorized_keys"; then if _do ssh -t "$target" "sudo mkdir -p /root/.ssh && sudo cp -f ~/.ssh/authorized_keys /root/.ssh/authorized_keys"; then
trap '_do ssh -t "root@$target" "rm -f /root/.ssh/authorized_keys"' EXIT
echo "Building $target ($cmd)" echo "Building $target ($cmd)"
HOSTNAME="${HOSTNAME:-$target}" _rebuild \ HOSTNAME="${HOSTNAME:-$target}" _rebuild \
--build-host localhost \ --build-host localhost \
--target-host "root@$target" \ --target-host "root@$target" \
$cmd $cmd
echo "Undoing root ssh access on $target" echo "Undoing root ssh access on $target"
_do ssh -t "root@$target" "rm -f /root/.ssh/authorized_keys"
fi fi
;; ;;