Pretiffied code.

This commit is contained in:
samoht9277 2024-02-07 11:51:40 -03:00
parent eb99b96d43
commit c392f82482
No known key found for this signature in database
GPG key ID: 612AC42768391756

View file

@ -3,30 +3,21 @@
set -e -o pipefail
makeConf() {
# 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
# NB <<"EOF" quotes / $ ` in heredocs, <<EOF does not
mkdir -p /etc/nixos
# Prevent grep for sending error code 1 (and halting execution) when no lines are selected : https://www.unix.com/man-page/posix/1P/grep
local IFS=$'\n'
for trypath in /root/.ssh/authorized_keys /home/$SUDO_USER/.ssh/authorized_keys $HOME/.ssh/authorized_keys; do
[[ -r "$trypath" ]] \
&& keys=$(sed -E 's/^.*((ssh|ecdsa)-[^[:space:]]+)[[:space:]]+([^[:space:]]+)([[:space:]]*.*)$/\1 \3\4/' "$trypath") \
&& [[ ! -z "$keys" ]] \
&& break
done
local network_import=""
[[ -n "$doNetConf" ]] && network_import="./networking.nix # generated at runtime by nixos-infect"
cat > /etc/nixos/configuration.nix << EOF
{ config, pkgs, ... }: {
{ config, pkgs, ... }:
{
imports = [
./hardware-configuration.nix
$network_import
@ -34,13 +25,14 @@ makeConf() {
];
nix.settings = {
# Enable flakes and new 'nix' command
experimental-features = "nix-command flakes";
# Deduplicate and optimize nix store
auto-optimise-store = true;
};
boot.loader.systemd-boot.enable = true;
boot.loader.efi.canTouchEfiVariables = true;
boot.cleanTmpDir = true;
zramSwap.enable = false;
networking.hostName = "$(hostname -s)";
@ -112,8 +104,7 @@ makeConf() {
EOF
if isEFI; then
bootcfg=$(
cat <<EOF
bootcfg=$(cat << EOF
boot.loader.grub = {
efiSupport = true;
efiInstallAsRemovable = true;
@ -121,13 +112,12 @@ EOF
};
fileSystems."/boot" = { device = "$esp"; fsType = "vfat"; };
EOF
)
)
else
bootcfg=$(
cat <<EOF
bootcfg=$(cat << EOF
boot.loader.grub.device = "$grubdev";
EOF
)
)
fi
availableKernelModules=('"ata_piix"' '"uhci_hcd"' '"xen_blkfront"')
@ -136,7 +126,7 @@ EOF
fi
# If you rerun this later, be sure to prune the filesSystems attr
cat >/etc/nixos/hardware-configuration.nix <<EOF
cat > /etc/nixos/hardware-configuration.nix << EOF
{ modulesPath, ... }:
{
imports = [ (modulesPath + "/profiles/qemu-guest.nix") ];
@ -153,7 +143,7 @@ EOF
makeLightsailConf() {
mkdir -p /etc/nixos
cat >/etc/nixos/configuration.nix <<EOF
cat > /etc/nixos/configuration.nix << EOF
{ config, pkgs, modulesPath, ... }:
{
imports = [ "\${modulesPath}/virtualisation/amazon-image.nix" ];
@ -171,13 +161,12 @@ makeNetworkingConf() {
gateway6=$(ip -6 route show dev "$eth0_name" | grep default | sed -r 's|default via ([0-9a-f:]+).*|\1|' || true)
ether0=$(ip address show dev "$eth0_name" | grep link/ether | sed -r 's|.*link/ether ([0-9a-f:]+) .*|\1|')
eth1_name=$(ip address show | grep '^3:' | awk -F': ' '{print $2}') || true
if [ -n "$eth1_name" ]; then
eth1_name=$(ip address show | grep '^3:' | awk -F': ' '{print $2}')||true
if [ -n "$eth1_name" ];then
eth1_ip4s=$(ip address show dev "$eth1_name" | grep 'inet ' | sed -r 's|.*inet ([0-9.]+)/([0-9]+).*|{ address="\1"; prefixLength=\2; }|')
eth1_ip6s=$(ip address show dev "$eth1_name" | grep 'inet6 ' | sed -r 's|.*inet6 ([0-9a-f:]+)/([0-9]+).*|{ address="\1"; prefixLength=\2; }|' || '')
ether1=$(ip address show dev "$eth1_name" | grep link/ether | sed -r 's|.*link/ether ([0-9a-f:]+) .*|\1|')
interfaces1=$(
cat <<EOF
interfaces1=$(cat << EOF
$eth1_name = {
ipv4.addresses = [$(for a in "${eth1_ip4s[@]}"; do echo -n "
$a"; done)
@ -187,7 +176,7 @@ makeNetworkingConf() {
];
};
EOF
)
)
extraRules1="ATTR{address}==\"${ether1}\", NAME=\"${eth1_name}\""
else
interfaces1=""
@ -197,14 +186,14 @@ EOF
readarray nameservers < <(grep ^nameserver /etc/resolv.conf | sed -r \
-e 's/^nameserver[[:space:]]+([0-9.a-fA-F:]+).*/"\1"/' \
-e 's/127[0-9.]+/8.8.8.8/' \
-e 's/::1/8.8.8.8/')
-e 's/::1/8.8.8.8/' )
if [[ "$eth0_name" = eth* ]]; then
predictable_inames="usePredictableInterfaceNames = lib.mkForce false;"
else
predictable_inames="usePredictableInterfaceNames = lib.mkForce true;"
fi
cat >/etc/nixos/networking.nix <<EOF
cat > /etc/nixos/networking.nix << EOF
{ lib, ... }: {
# This file was populated at runtime with the networking
# details gathered from the active system.
@ -255,7 +244,7 @@ checkExistingSwap() {
makeSwap() {
swapFile=$(mktemp /tmp/nixos-infect.XXXXX.swp)
dd if=/dev/zero "of=$swapFile" bs=1M count=$((1 * 1024))
dd if=/dev/zero "of=$swapFile" bs=1M count=$((1*1024))
chmod 0600 "$swapFile"
mkswap "$swapFile"
swapon -v "$swapFile"
@ -278,14 +267,11 @@ findESP() {
esp=""
for d in /boot/EFI /boot/efi /boot; do
[[ ! -d "$d" ]] && continue
[[ "$d" == "$(df "$d" --output=target | sed 1d)" ]] &&
esp="$(df "$d" --output=source | sed 1d)" &&
break
[[ "$d" == "$(df "$d" --output=target | sed 1d)" ]] \
&& esp="$(df "$d" --output=source | sed 1d)" \
&& break
done
[[ -z "$esp" ]] && {
echo "ERROR: No ESP mount point found"
return 1
}
[[ -z "$esp" ]] && { echo "ERROR: No ESP mount point found"; return 1; }
for uuid in /dev/disk/by-uuid/*; do
[[ $(readlink -f "$uuid") == "$esp" ]] && echo $uuid && return 0
done
@ -296,7 +282,7 @@ prepareEnv() {
if isEFI; then
esp="$(findESP)"
else
for grubdev in /dev/vda /dev/sda /dev/xvda /dev/nvme0n1; do [[ -e $grubdev ]] && break; done
for grubdev in /dev/vda /dev/sda /dev/xvda /dev/nvme0n1 ; do [[ -e $grubdev ]] && break; done
fi
# Retrieve root fs block device
@ -316,89 +302,60 @@ prepareEnv() {
fakeCurlUsingWget() {
# Use adapted wget if curl is missing
which wget && {
which wget && { \
curl() {
eval "wget $(
(
local isStdout=1
(local isStdout=1
for arg in "$@"; do
case "$arg" in
"-o")
echo "-O"
echo "-O";
isStdout=0
;;
"-O")
isStdout=0
;;
"-L") ;;
"-L")
;;
*)
echo "$arg"
;;
esac
done
done;
[[ $isStdout -eq 1 ]] && echo "-O-"
) | tr '\n' ' '
)| tr '\n' ' '
)"
}
export -f curl
}
}; export -f curl; }
}
req() {
type "$1" >/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
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
@ -426,7 +383,8 @@ infect() {
nix-channel --add "https://nixos.org/channels/$NIX_CHANNEL" nixos
nix-channel --update
if [[ $NIXOS_CONFIG = http* ]]; then
if [[ $NIXOS_CONFIG = http* ]]
then
curl $NIXOS_CONFIG -o /etc/nixos/configuration.nix
unset NIXOS_CONFIG
fi
@ -444,7 +402,7 @@ infect() {
/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
[[ -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
@ -454,19 +412,15 @@ infect() {
# 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
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"
mv -v /boot /boot.bak || {
cp -a /boot /boot.bak
rm -rf /boot/*
umount /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