diff --git a/nixos-infect b/nixos-infect index 5b38eff..65a2d46 100644 --- a/nixos-infect +++ b/nixos-infect @@ -5,29 +5,29 @@ set -ex -o pipefail makeConf() { - # Skip everything if main config already present - [[ -e /etc/nixos/configuration.nix ]] && return 0 + # Skip everything if main config already present + [[ -e /etc/nixos/configuration.nix ]] && return 0 - # Lightsail config is not like the others - if [ "$PROVIDER" = "lightsail" ]; then - makeLightsailConf - return 0 - fi + # Lightsail config is not like the others + if [ "$PROVIDER" = "lightsail" ]; then + makeLightsailConf + return 0 + fi - # NB <<"EOF" quotes / $ ` in heredocs, < /etc/nixos/configuration.nix << EOF + [[ -n "$doNetConf" ]] && network_import="./networking.nix # generated at runtime by nixos-infect" + cat >/etc/nixos/configuration.nix < /etc/nixos/hardware-configuration.nix << EOF + # If you rerun this later, be sure to prune the filesSystems attr + cat >/etc/nixos/hardware-configuration.nix < /etc/nixos/configuration.nix << EOF + mkdir -p /etc/nixos + cat >/etc/nixos/configuration.nix < /etc/nixos/networking.nix << EOF + if [[ "$eth0_name" = eth* ]]; then + predictable_inames="usePredictableInterfaceNames = lib.mkForce false;" + else + predictable_inames="usePredictableInterfaceNames = lib.mkForce true;" + fi + cat >/etc/nixos/networking.nix < /dev/null 2>&1 || which "$1" > /dev/null 2>&1 + type "$1" >/dev/null 2>&1 || which "$1" >/dev/null 2>&1 } checkEnv() { - [[ "$(whoami)" == "root" ]] || { echo "ERROR: Must run as root"; return 1; } + [[ "$(whoami)" == "root" ]] || { + echo "ERROR: Must run as root" + return 1 + } - # Perform some easy fixups before checking - # TODO prevent multiple calls to apt-get update - (which dnf && dnf install -y perl-Digest-SHA) || true # Fedora 24 - which bzcat || (which yum && yum install -y bzip2) \ - || (which apt-get && apt-get update && apt-get install -y bzip2) \ - || true - which xzcat || (which yum && yum install -y xz-utils) \ - || (which apt-get && apt-get update && apt-get install -y xz-utils) \ - || true - which curl || fakeCurlUsingWget \ - || (which apt-get && apt-get update && apt-get install -y curl) \ - || true + # Perform some easy fixups before checking + # TODO prevent multiple calls to apt-get update + (which dnf && dnf install -y perl-Digest-SHA) || true # Fedora 24 + which bzcat || (which yum && yum install -y bzip2) || + (which apt-get && apt-get update && apt-get install -y bzip2) || + true + which xzcat || (which yum && yum install -y xz-utils) || + (which apt-get && apt-get update && apt-get install -y xz-utils) || + true + which curl || fakeCurlUsingWget || + (which apt-get && apt-get update && apt-get install -y curl) || + true - req curl || req wget || { echo "ERROR: Missing both curl and wget"; return 1; } - req bzcat || { echo "ERROR: Missing bzcat"; return 1; } - req xzcat || { echo "ERROR: Missing xzcat"; return 1; } - req groupadd || { echo "ERROR: Missing groupadd"; return 1; } - req useradd || { echo "ERROR: Missing useradd"; return 1; } - req ip || { echo "ERROR: Missing ip"; return 1; } - req awk || { echo "ERROR: Missing awk"; return 1; } - req cut || req df || { echo "ERROR: Missing coreutils (cut, df)"; return 1; } + req curl || req wget || { + echo "ERROR: Missing both curl and wget" + return 1 + } + req bzcat || { + echo "ERROR: Missing bzcat" + return 1 + } + req xzcat || { + echo "ERROR: Missing xzcat" + return 1 + } + req groupadd || { + echo "ERROR: Missing groupadd" + return 1 + } + req useradd || { + echo "ERROR: Missing useradd" + return 1 + } + req ip || { + echo "ERROR: Missing ip" + return 1 + } + req awk || { + echo "ERROR: Missing awk" + return 1 + } + req cut || req df || { + echo "ERROR: Missing coreutils (cut, df)" + return 1 + } - # On some versions of Oracle Linux these have the wrong permissions, - # which stops sshd from starting when NixOS boots - chmod 600 /etc/ssh/ssh_host_*_key + # On some versions of Oracle Linux these have the wrong permissions, + # which stops sshd from starting when NixOS boots + chmod 600 /etc/ssh/ssh_host_*_key } infect() { - # Add nix build users - # FIXME run only if necessary, rather than defaulting true - groupadd nixbld -g 30000 || true - for i in {1..10}; do - useradd -c "Nix build user $i" -d /var/empty -g nixbld -G nixbld -M -N -r -s "$(which nologin)" "nixbld$i" || true - done - # TODO use addgroup and adduser as fallbacks - #addgroup nixbld -g 30000 || true - #for i in {1..10}; do adduser -DH -G nixbld nixbld$i || true; done + # Add nix build users + # FIXME run only if necessary, rather than defaulting true + groupadd nixbld -g 30000 || true + for i in {1..10}; do + useradd -c "Nix build user $i" -d /var/empty -g nixbld -G nixbld -M -N -r -s "$(which nologin)" "nixbld$i" || true + done + # TODO use addgroup and adduser as fallbacks + #addgroup nixbld -g 30000 || true + #for i in {1..10}; do adduser -DH -G nixbld nixbld$i || true; done - curl -L https://nixos.org/nix/install | sh -s -- --no-channel-add + curl -L https://nixos.org/nix/install | sh -s -- --no-channel-add - # shellcheck disable=SC1090 - source ~/.nix-profile/etc/profile.d/nix.sh + # shellcheck disable=SC1090 + source ~/.nix-profile/etc/profile.d/nix.sh - [[ -z "$NIX_CHANNEL" ]] && NIX_CHANNEL="nixos-23.05" - nix-channel --remove nixpkgs - nix-channel --add "https://nixos.org/channels/$NIX_CHANNEL" nixos - nix-channel --update + [[ -z "$NIX_CHANNEL" ]] && NIX_CHANNEL="nixos-23.05" + nix-channel --remove nixpkgs + nix-channel --add "https://nixos.org/channels/$NIX_CHANNEL" nixos + nix-channel --update - if [[ $NIXOS_CONFIG = http* ]] - then - curl $NIXOS_CONFIG -o /etc/nixos/configuration.nix - unset NIXOS_CONFIG - fi + if [[ $NIXOS_CONFIG = http* ]]; then + curl $NIXOS_CONFIG -o /etc/nixos/configuration.nix + unset NIXOS_CONFIG + fi - export NIXOS_CONFIG="${NIXOS_CONFIG:-/etc/nixos/configuration.nix}" + export NIXOS_CONFIG="${NIXOS_CONFIG:-/etc/nixos/configuration.nix}" - nix-env --set \ - -I nixpkgs=$HOME/.nix-defexpr/channels/nixos \ - -f '' \ - -p /nix/var/nix/profiles/system \ - -A system + nix-env --set \ + -I nixpkgs=$HOME/.nix-defexpr/channels/nixos \ + -f '' \ + -p /nix/var/nix/profiles/system \ + -A system - # Remove nix installed with curl | bash - rm -fv /nix/var/nix/profiles/default* - /nix/var/nix/profiles/system/sw/bin/nix-collect-garbage + # Remove nix installed with curl | bash + rm -fv /nix/var/nix/profiles/default* + /nix/var/nix/profiles/system/sw/bin/nix-collect-garbage - # Reify resolv.conf - [[ -L /etc/resolv.conf ]] && mv -v /etc/resolv.conf /etc/resolv.conf.lnk && cat /etc/resolv.conf.lnk > /etc/resolv.conf + # Reify resolv.conf + [[ -L /etc/resolv.conf ]] && mv -v /etc/resolv.conf /etc/resolv.conf.lnk && cat /etc/resolv.conf.lnk >/etc/resolv.conf - # Set label of root partition - if [ -n "$newrootfslabel" ]; then - echo "Setting label of $rootfsdev to $newrootfslabel" - e2label "$rootfsdev" "$newrootfslabel" - fi + # Set label of root partition + if [ -n "$newrootfslabel" ]; then + echo "Setting label of $rootfsdev to $newrootfslabel" + e2label "$rootfsdev" "$newrootfslabel" + fi - # Stage the Nix coup d'état - touch /etc/NIXOS - echo etc/nixos >> /etc/NIXOS_LUSTRATE - echo etc/resolv.conf >> /etc/NIXOS_LUSTRATE - echo root/.nix-defexpr/channels >> /etc/NIXOS_LUSTRATE - (cd / && ls etc/ssh/ssh_host_*_key* || true) >> /etc/NIXOS_LUSTRATE + # Stage the Nix coup d'état + touch /etc/NIXOS + echo etc/nixos >>/etc/NIXOS_LUSTRATE + echo etc/resolv.conf >>/etc/NIXOS_LUSTRATE + echo root/.nix-defexpr/channels >>/etc/NIXOS_LUSTRATE + (cd / && ls etc/ssh/ssh_host_*_key* || true) >>/etc/NIXOS_LUSTRATE - rm -rf /boot.bak - isEFI && umount "$esp" + rm -rf /boot.bak + isEFI && umount "$esp" - mv -v /boot /boot.bak || { cp -a /boot /boot.bak ; rm -rf /boot/* ; umount /boot ; } - if isEFI; then - mkdir -p /boot - mount "$esp" /boot - find /boot -depth ! -path /boot -exec rm -rf {} + - fi - /nix/var/nix/profiles/system/bin/switch-to-configuration boot + mv -v /boot /boot.bak || { + cp -a /boot /boot.bak + rm -rf /boot/* + umount /boot + } + if isEFI; then + mkdir -p /boot + mount "$esp" /boot + find /boot -depth ! -path /boot -exec rm -rf {} + + fi + /nix/var/nix/profiles/system/bin/switch-to-configuration boot } [ "$PROVIDER" = "digitalocean" ] && doNetConf=y # digitalocean requires detailed network config to be generated @@ -439,14 +475,14 @@ checkEnv prepareEnv checkExistingSwap if [[ -z "$NO_SWAP" ]]; then - makeSwap # smallest (512MB) droplet needs extra memory! + makeSwap # smallest (512MB) droplet needs extra memory! fi makeConf infect if [[ -z "$NO_SWAP" ]]; then - removeSwap + removeSwap fi if [[ -z "$NO_REBOOT" ]]; then - reboot + reboot fi