Skip to content

Instantly share code, notes, and snippets.

@lheckemann
Created August 2, 2022 23:04
Show Gist options
  • Save lheckemann/59d4fa780f951b61e9b90769a0580a56 to your computer and use it in GitHub Desktop.
Save lheckemann/59d4fa780f951b61e9b90769a0580a56 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -exuo pipefail
PROGRAM_NAME="$0"
inst() {
local system="" host="" action="install" from="auto" mount=""
local -a nixCopyArgs
while [[ "$#" -gt 0 ]] ; do
case "$1" in
-s)
system="$2"
shift 2
;;
-h)
host="$2"
shift 2
;;
-a)
action="$2"
shift 2
;;
--substitute)
nixCopyArgs+=(-s)
shift
;;
-f)
from="$2"
shift 2
;;
-m)
mount=1
shift
;;
*)
echo 'Usage: $PROGRAM_NAME -s <system> -h <host> [-a <action>] [--substitute] [-f <store>]'
exit 1
;;
esac
done
: "${system:?pass system with -s}" "${host:?pass host with -h}"
system="$(readlink -f $system)"
if [[ "$system" = *.drv ]]; then
system=$(nix-store --store "$from" -r "$system")
fi
if ! [[ -e "$system"/kernel ]] ; then
echo "is '$system' really a nixos system?"
exit 1
fi
if [[ -n "$mount" && "$action" != "install" ]] ; then
echo "-m specified, but action isn't install"
exit 1
fi
case "$action" in
install)
if [[ -n "$mount" ]] ; then
ssh root@"$host" 'cat > /tmp/fstab' <"$system/etc/fstab"
ssh root@"$host" findmnt /mnt && ssh root@"$host" umount -Rlv /mnt
ssh root@"$host" mount -v -T /tmp/fstab --target-prefix /mnt -o X-mount.mkdir /
ssh root@"$host" mount -v -T /tmp/fstab --target-prefix /mnt -o X-mount.mkdir --all
fi
nix copy "${nixCopyArgs[@]}" --from "$from" --to "ssh://root@$host?remote-store=local?root=/mnt" "$system"
ssh root@"$host" nixos-install --no-root-passwd --no-channel-copy --system "$system"
;;
switch|boot)
nix copy "${nixCopyArgs[@]}" --from "$from" --to "ssh://root@$host" "$system"
ssh root@"$host" nix-env -p /nix/var/nix/profiles/system --set "$system"
;&
*)
nix copy "${nixCopyArgs[@]}" --from "$from" --to "ssh://root@$host" "$system"
ssh root@"$host" "$system/bin/switch-to-configuration $action"
;;
esac
}
inst "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment