config/pkgs/mk-nspawn-deployer/setup-nspawn.sh

45 lines
1012 B
Bash

#!/usr/bin/env bash
# TODO: assert correct system
NSPAWN=nixos-@hostname@
TARBALL=./"$NSPAWN".tar #"https://github.com/tfc/nspawn-nixos/releases/download/v1.0/nixos-system-x86_64-linux.tar.xz"
test $(id -u) -eq 0 || {
>&2 echo you must run this as root
exit 1
}
install_pkg() {
# TODO: use bash hashmaps to map from apt to other package managers
# * [x] apt
# * [ ] apk
# * [ ] pacman
# * [ ] dnf
DEBIAN_FRONTEND=noninteractive apt install -y "$@"
}
if ! >/dev/null command -v systemd-nspawn; then
# TODO: support more than ubuntu
install_pkg systemd-container
fi
machinectl remove "$NSPAWN" || true # TODO: interactive?
#machinectl pull-tar "$TARBALL" "$NSPAWN" --verify=no
machinectl import-tar "$TARBALL" "$NSPAWN"
# use host network
cat <<"EOF" > /etc/systemd/nspawn/"$NSPAWN".nspawn
[Network]
VirtualEthernet=no
EOF
machinectl enable "$NSPAWN"
machinectl start "$NSPAWN"
echo Setting root password...
machinectl shell "$NSPAWN" /usr/bin/env passwd
machinectl status "$NSPAWN"