fix bashism

[[ ]] is not available in POSIX sh, and test -o is not well defined
either.

Move the facts-gathering script out so it can be tested with shellcheck.
This commit is contained in:
zimbatm 2024-04-05 16:24:40 +02:00 committed by mergify[bot]
parent 88a60aa8b8
commit 3170c45be4
3 changed files with 26 additions and 27 deletions

View File

@ -37,13 +37,13 @@ stdenv.mkDerivation {
src = ./..;
nativeBuildInputs = [ makeWrapper ];
installPhase = ''
install -D -m 0755 src/nixos-anywhere.sh $out/bin/nixos-anywhere
install -D --target-directory=$out/libexec/nixos-anywhere/ -m 0755 src/*.sh
# We prefer the system's openssh over our own, since it might come with features not present in ours:
# https://github.com/nix-community/nixos-anywhere/issues/62
#
# We also prefer system rsync to prevent crashes between rsync and ssh.
wrapProgram $out/bin/nixos-anywhere \
makeShellWrapper $out/libexec/nixos-anywhere/nixos-anywhere.sh $out/bin/nixos-anywhere \
--prefix PATH : ${lib.makeBinPath runtimeDeps} --suffix PATH : ${lib.makeBinPath [ openssh ]}
'';

20
src/get-facts.sh Executable file
View File

@ -0,0 +1,20 @@
#!/bin/sh
set -efu "${enable_debug:-}"
has() {
command -v "$1" >/dev/null && echo "y" || echo "n"
}
is_nixos=$(if test -f /etc/os-release && grep -q ID=nixos /etc/os-release; then echo "y"; else echo "n"; fi)
cat <<FACTS
is_os=$(uname)
is_arch=$(uname -m)
is_kexec=$(if test -f /etc/is_kexec; then echo "y"; else echo "n"; fi)
is_nixos=$is_nixos
is_installer=$(if [ "$is_nixos" = "y" ] && grep -q VARIANT_ID=installer /etc/os-release; then echo "y"; else echo "n"; fi)
is_container=$(if [ "$(has systemd-detect-virt)" = "y" ]; then systemd-detect-virt --container; else echo "none"; fi)
has_tar=$(has tar)
has_sudo=$(has sudo)
has_doas=$(has doas)
has_wget=$(has wget)
has_curl=$(has curl)
has_setsid=$(has setsid)
FACTS

View File

@ -62,6 +62,7 @@ step() {
echo "### $* ###"
}
here=$(dirname "${BASH_SOURCE[0]}")
kexec_url=""
enable_debug=""
maybe_reboot="sleep 6 && reboot"
@ -321,29 +322,7 @@ done
import_facts() {
local facts filtered_facts
if ! facts=$(
ssh_ -o ConnectTimeout=10 sh -- <<SSH
set -efu ${enable_debug}
has(){
command -v "\$1" >/dev/null && echo "y" || echo "n"
}
is_nixos=\$(if test -f /etc/os-release && grep -q ID=nixos /etc/os-release; then echo "y"; else echo "n"; fi)
cat <<FACTS
is_os=\$(uname)
is_arch=\$(uname -m)
is_kexec=\$(if test -f /etc/is_kexec; then echo "y"; else echo "n"; fi)
is_nixos=\$is_nixos
is_installer=\$(if [[ "\$is_nixos" == "y" ]] && grep -q VARIANT_ID=installer /etc/os-release; then echo "y"; else echo "n"; fi)
is_container=\$(if [[ "\$(has systemd-detect-virt)" == "y" ]]; then systemd-detect-virt --container; else echo "none"; fi)
has_tar=\$(has tar)
has_sudo=\$(has sudo)
has_doas=\$(has doas)
has_wget=\$(has wget)
has_curl=\$(has curl)
has_setsid=\$(has setsid)
FACTS
SSH
); then
if ! facts=$(ssh_ -o ConnectTimeout=10 enable_debug=$enable_debug sh -- <"$here"/get-facts.sh); then
exit 1
fi
filtered_facts=$(echo "$facts" | grep -E '^(has|is)_[a-z0-9_]+=\S+')
@ -510,13 +489,13 @@ export PATH="\$PATH:/run/current-system/sw/bin"
# needed for installation if initrd-secrets are used
mkdir -p /mnt/tmp
chmod 777 /mnt/tmp
if [[ ${copy_host_keys-n} == "y" ]]; then
if [ ${copy_host_keys-n} = "y" ]; then
# NB we copy host keys that are in turn copied by kexec installer.
mkdir -m 755 -p /mnt/etc/ssh
for p in /etc/ssh/ssh_host_*; do
# Skip if the source file does not exist (i.e. glob did not match any files)
# or the destination already exists (e.g. copied with --extra-files).
if [ ! -e "\$p" -o -e "/mnt/\$p" ]; then
if [ ! -e "\$p" ] || [ -e "/mnt/\$p" ]; then
continue
fi
cp -a "\$p" "/mnt/\$p"