Created
December 21, 2022 17:59
-
-
Save Mic92/89569d5a9036a1e0056fce66a3abb0c6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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