Skip to content

Commit

Permalink
fix bashism
Browse files Browse the repository at this point in the history
[[ ]] 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.
  • Loading branch information
zimbatm authored and mergify[bot] committed Apr 29, 2024
1 parent 88a60aa commit 3170c45
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]}
'';

Expand Down
20 changes: 20 additions & 0 deletions src/get-facts.sh
Original file line number Diff line number Diff line change
@@ -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
29 changes: 4 additions & 25 deletions src/nixos-anywhere.sh
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ step() {
echo "### $* ###"
}

here=$(dirname "${BASH_SOURCE[0]}")
kexec_url=""
enable_debug=""
maybe_reboot="sleep 6 && reboot"
Expand Down Expand Up @@ -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+')
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 3170c45

Please sign in to comment.