mirror of
https://github.com/elitak/nixos-infect.git
synced 2024-11-01 08:39:55 +03:00
Pretiffied code.
This commit is contained in:
parent
eb99b96d43
commit
c392f82482
1 changed files with 201 additions and 247 deletions
126
nixos-infect
126
nixos-infect
|
@ -3,30 +3,21 @@
|
||||||
set -e -o pipefail
|
set -e -o pipefail
|
||||||
|
|
||||||
makeConf() {
|
makeConf() {
|
||||||
# Skip everything if main config already present
|
|
||||||
[[ -e /etc/nixos/configuration.nix ]] && return 0
|
[[ -e /etc/nixos/configuration.nix ]] && return 0
|
||||||
|
|
||||||
# Lightsail config is not like the others
|
|
||||||
if [ "$PROVIDER" = "lightsail" ]; then
|
if [ "$PROVIDER" = "lightsail" ]; then
|
||||||
makeLightsailConf
|
makeLightsailConf
|
||||||
return 0
|
return 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# NB <<"EOF" quotes / $ ` in heredocs, <<EOF does not
|
|
||||||
mkdir -p /etc/nixos
|
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=""
|
local network_import=""
|
||||||
|
|
||||||
[[ -n "$doNetConf" ]] && network_import="./networking.nix # generated at runtime by nixos-infect"
|
[[ -n "$doNetConf" ]] && network_import="./networking.nix # generated at runtime by nixos-infect"
|
||||||
cat > /etc/nixos/configuration.nix << EOF
|
cat > /etc/nixos/configuration.nix << EOF
|
||||||
{ config, pkgs, ... }: {
|
{ config, pkgs, ... }:
|
||||||
|
|
||||||
|
{
|
||||||
imports = [
|
imports = [
|
||||||
./hardware-configuration.nix
|
./hardware-configuration.nix
|
||||||
$network_import
|
$network_import
|
||||||
|
@ -34,13 +25,14 @@ makeConf() {
|
||||||
];
|
];
|
||||||
|
|
||||||
nix.settings = {
|
nix.settings = {
|
||||||
|
# Enable flakes and new 'nix' command
|
||||||
experimental-features = "nix-command flakes";
|
experimental-features = "nix-command flakes";
|
||||||
|
# Deduplicate and optimize nix store
|
||||||
auto-optimise-store = true;
|
auto-optimise-store = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
boot.loader.systemd-boot.enable = true;
|
boot.loader.systemd-boot.enable = true;
|
||||||
boot.loader.efi.canTouchEfiVariables = true;
|
boot.loader.efi.canTouchEfiVariables = true;
|
||||||
boot.cleanTmpDir = true;
|
|
||||||
|
|
||||||
zramSwap.enable = false;
|
zramSwap.enable = false;
|
||||||
networking.hostName = "$(hostname -s)";
|
networking.hostName = "$(hostname -s)";
|
||||||
|
@ -112,8 +104,7 @@ makeConf() {
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
if isEFI; then
|
if isEFI; then
|
||||||
bootcfg=$(
|
bootcfg=$(cat << EOF
|
||||||
cat <<EOF
|
|
||||||
boot.loader.grub = {
|
boot.loader.grub = {
|
||||||
efiSupport = true;
|
efiSupport = true;
|
||||||
efiInstallAsRemovable = true;
|
efiInstallAsRemovable = true;
|
||||||
|
@ -123,8 +114,7 @@ EOF
|
||||||
EOF
|
EOF
|
||||||
)
|
)
|
||||||
else
|
else
|
||||||
bootcfg=$(
|
bootcfg=$(cat << EOF
|
||||||
cat <<EOF
|
|
||||||
boot.loader.grub.device = "$grubdev";
|
boot.loader.grub.device = "$grubdev";
|
||||||
EOF
|
EOF
|
||||||
)
|
)
|
||||||
|
@ -176,8 +166,7 @@ makeNetworkingConf() {
|
||||||
eth1_ip4s=$(ip address show dev "$eth1_name" | grep 'inet ' | sed -r 's|.*inet ([0-9.]+)/([0-9]+).*|{ address="\1"; prefixLength=\2; }|')
|
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; }|' || '')
|
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|')
|
ether1=$(ip address show dev "$eth1_name" | grep link/ether | sed -r 's|.*link/ether ([0-9a-f:]+) .*|\1|')
|
||||||
interfaces1=$(
|
interfaces1=$(cat << EOF
|
||||||
cat <<EOF
|
|
||||||
$eth1_name = {
|
$eth1_name = {
|
||||||
ipv4.addresses = [$(for a in "${eth1_ip4s[@]}"; do echo -n "
|
ipv4.addresses = [$(for a in "${eth1_ip4s[@]}"; do echo -n "
|
||||||
$a"; done)
|
$a"; done)
|
||||||
|
@ -278,14 +267,11 @@ findESP() {
|
||||||
esp=""
|
esp=""
|
||||||
for d in /boot/EFI /boot/efi /boot; do
|
for d in /boot/EFI /boot/efi /boot; do
|
||||||
[[ ! -d "$d" ]] && continue
|
[[ ! -d "$d" ]] && continue
|
||||||
[[ "$d" == "$(df "$d" --output=target | sed 1d)" ]] &&
|
[[ "$d" == "$(df "$d" --output=target | sed 1d)" ]] \
|
||||||
esp="$(df "$d" --output=source | sed 1d)" &&
|
&& esp="$(df "$d" --output=source | sed 1d)" \
|
||||||
break
|
&& break
|
||||||
done
|
done
|
||||||
[[ -z "$esp" ]] && {
|
[[ -z "$esp" ]] && { echo "ERROR: No ESP mount point found"; return 1; }
|
||||||
echo "ERROR: No ESP mount point found"
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
for uuid in /dev/disk/by-uuid/*; do
|
for uuid in /dev/disk/by-uuid/*; do
|
||||||
[[ $(readlink -f "$uuid") == "$esp" ]] && echo $uuid && return 0
|
[[ $(readlink -f "$uuid") == "$esp" ]] && echo $uuid && return 0
|
||||||
done
|
done
|
||||||
|
@ -316,32 +302,30 @@ prepareEnv() {
|
||||||
|
|
||||||
fakeCurlUsingWget() {
|
fakeCurlUsingWget() {
|
||||||
# Use adapted wget if curl is missing
|
# Use adapted wget if curl is missing
|
||||||
which wget && {
|
which wget && { \
|
||||||
curl() {
|
curl() {
|
||||||
eval "wget $(
|
eval "wget $(
|
||||||
(
|
(local isStdout=1
|
||||||
local isStdout=1
|
|
||||||
for arg in "$@"; do
|
for arg in "$@"; do
|
||||||
case "$arg" in
|
case "$arg" in
|
||||||
"-o")
|
"-o")
|
||||||
echo "-O"
|
echo "-O";
|
||||||
isStdout=0
|
isStdout=0
|
||||||
;;
|
;;
|
||||||
"-O")
|
"-O")
|
||||||
isStdout=0
|
isStdout=0
|
||||||
;;
|
;;
|
||||||
"-L") ;;
|
"-L")
|
||||||
|
;;
|
||||||
*)
|
*)
|
||||||
echo "$arg"
|
echo "$arg"
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
done
|
done;
|
||||||
[[ $isStdout -eq 1 ]] && echo "-O-"
|
[[ $isStdout -eq 1 ]] && echo "-O-"
|
||||||
)| tr '\n' ' '
|
)| tr '\n' ' '
|
||||||
)"
|
)"
|
||||||
}
|
}; export -f curl; }
|
||||||
export -f curl
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
req() {
|
req() {
|
||||||
|
@ -349,56 +333,29 @@ req() {
|
||||||
}
|
}
|
||||||
|
|
||||||
checkEnv() {
|
checkEnv() {
|
||||||
[[ "$(whoami)" == "root" ]] || {
|
[[ "$(whoami)" == "root" ]] || { echo "ERROR: Must run as root"; return 1; }
|
||||||
echo "ERROR: Must run as root"
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
|
|
||||||
# Perform some easy fixups before checking
|
# Perform some easy fixups before checking
|
||||||
# TODO prevent multiple calls to apt-get update
|
# TODO prevent multiple calls to apt-get update
|
||||||
(which dnf && dnf install -y perl-Digest-SHA) || true # Fedora 24
|
(which dnf && dnf install -y perl-Digest-SHA) || true # Fedora 24
|
||||||
which bzcat || (which yum && yum install -y bzip2) ||
|
which bzcat || (which yum && yum install -y bzip2) \
|
||||||
(which apt-get && apt-get update && apt-get install -y bzip2) ||
|
|| (which apt-get && apt-get update && apt-get install -y bzip2) \
|
||||||
true
|
|| true
|
||||||
which xzcat || (which yum && yum install -y xz-utils) ||
|
which xzcat || (which yum && yum install -y xz-utils) \
|
||||||
(which apt-get && apt-get update && apt-get install -y xz-utils) ||
|
|| (which apt-get && apt-get update && apt-get install -y xz-utils) \
|
||||||
true
|
|| true
|
||||||
which curl || fakeCurlUsingWget ||
|
which curl || fakeCurlUsingWget \
|
||||||
(which apt-get && apt-get update && apt-get install -y curl) ||
|
|| (which apt-get && apt-get update && apt-get install -y curl) \
|
||||||
true
|
|| true
|
||||||
|
|
||||||
req curl || req wget || {
|
req curl || req wget || { echo "ERROR: Missing both curl and wget"; return 1; }
|
||||||
echo "ERROR: Missing both curl and wget"
|
req bzcat || { echo "ERROR: Missing bzcat"; return 1; }
|
||||||
return 1
|
req xzcat || { echo "ERROR: Missing xzcat"; return 1; }
|
||||||
}
|
req groupadd || { echo "ERROR: Missing groupadd"; return 1; }
|
||||||
req bzcat || {
|
req useradd || { echo "ERROR: Missing useradd"; return 1; }
|
||||||
echo "ERROR: Missing bzcat"
|
req ip || { echo "ERROR: Missing ip"; return 1; }
|
||||||
return 1
|
req awk || { echo "ERROR: Missing awk"; return 1; }
|
||||||
}
|
req cut || req df || { echo "ERROR: Missing coreutils (cut, df)"; 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,
|
# On some versions of Oracle Linux these have the wrong permissions,
|
||||||
# which stops sshd from starting when NixOS boots
|
# 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 --add "https://nixos.org/channels/$NIX_CHANNEL" nixos
|
||||||
nix-channel --update
|
nix-channel --update
|
||||||
|
|
||||||
if [[ $NIXOS_CONFIG = http* ]]; then
|
if [[ $NIXOS_CONFIG = http* ]]
|
||||||
|
then
|
||||||
curl $NIXOS_CONFIG -o /etc/nixos/configuration.nix
|
curl $NIXOS_CONFIG -o /etc/nixos/configuration.nix
|
||||||
unset NIXOS_CONFIG
|
unset NIXOS_CONFIG
|
||||||
fi
|
fi
|
||||||
|
@ -462,11 +420,7 @@ infect() {
|
||||||
rm -rf /boot.bak
|
rm -rf /boot.bak
|
||||||
isEFI && umount "$esp"
|
isEFI && umount "$esp"
|
||||||
|
|
||||||
mv -v /boot /boot.bak || {
|
mv -v /boot /boot.bak || { cp -a /boot /boot.bak ; rm -rf /boot/* ; umount /boot ; }
|
||||||
cp -a /boot /boot.bak
|
|
||||||
rm -rf /boot/*
|
|
||||||
umount /boot
|
|
||||||
}
|
|
||||||
if isEFI; then
|
if isEFI; then
|
||||||
mkdir -p /boot
|
mkdir -p /boot
|
||||||
mount "$esp" /boot
|
mount "$esp" /boot
|
||||||
|
|
Loading…
Reference in a new issue