mirror of
https://github.com/elitak/nixos-infect.git
synced 2024-11-01 00:29:57 +03:00
Added cachix and removed lagacy code.
This commit is contained in:
parent
976db93859
commit
5f1f41e58c
1 changed files with 259 additions and 223 deletions
482
nixos-infect
482
nixos-infect
|
@ -5,29 +5,29 @@
|
||||||
set -ex -o pipefail
|
set -ex -o pipefail
|
||||||
|
|
||||||
makeConf() {
|
makeConf() {
|
||||||
# Skip everything if main config already present
|
# 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
|
# 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
|
# 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
|
# 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'
|
local IFS=$'\n'
|
||||||
for trypath in /root/.ssh/authorized_keys /home/$SUDO_USER/.ssh/authorized_keys $HOME/.ssh/authorized_keys; do
|
for trypath in /root/.ssh/authorized_keys /home/$SUDO_USER/.ssh/authorized_keys $HOME/.ssh/authorized_keys; do
|
||||||
[[ -r "$trypath" ]] \
|
[[ -r "$trypath" ]] &&
|
||||||
&& keys=$(sed -E 's/^.*((ssh|ecdsa)-[^[:space:]]+)[[:space:]]+([^[:space:]]+)([[:space:]]*.*)$/\1 \3\4/' "$trypath") \
|
keys=$(sed -E 's/^.*((ssh|ecdsa)-[^[:space:]]+)[[:space:]]+([^[:space:]]+)([[:space:]]*.*)$/\1 \3\4/' "$trypath") &&
|
||||||
&& [[ ! -z "$keys" ]] \
|
[[ ! -z "$keys" ]] &&
|
||||||
&& break
|
break
|
||||||
done
|
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
|
||||||
|
@ -39,12 +39,10 @@ makeConf() {
|
||||||
experimental-features = "nix-command flakes";
|
experimental-features = "nix-command flakes";
|
||||||
# Deduplicate and optimize nix store
|
# Deduplicate and optimize nix store
|
||||||
auto-optimise-store = true;
|
auto-optimise-store = true;
|
||||||
|
trusted-users = [ "root" "dev" ];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
boot.tmp.cleanOnBoot = true;
|
||||||
boot.cleanTmpDir = true;
|
|
||||||
# boot.loader.systemd-boot.enable = true;
|
|
||||||
# boot.loader.efi.canTouchEfiVariables = true;
|
|
||||||
zramSwap.enable = ${zramswap};
|
zramSwap.enable = ${zramswap};
|
||||||
|
|
||||||
networking.hostName = "$(hostname -s)";
|
networking.hostName = "$(hostname -s)";
|
||||||
|
@ -103,8 +101,9 @@ makeConf() {
|
||||||
}
|
}
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
if isEFI; then
|
if isEFI; then
|
||||||
bootcfg=$(cat << EOF
|
bootcfg=$(
|
||||||
|
cat <<EOF
|
||||||
boot.loader.grub = {
|
boot.loader.grub = {
|
||||||
efiSupport = true;
|
efiSupport = true;
|
||||||
efiInstallAsRemovable = true;
|
efiInstallAsRemovable = true;
|
||||||
|
@ -112,21 +111,22 @@ EOF
|
||||||
};
|
};
|
||||||
fileSystems."/boot" = { device = "$esp"; fsType = "vfat"; };
|
fileSystems."/boot" = { device = "$esp"; fsType = "vfat"; };
|
||||||
EOF
|
EOF
|
||||||
)
|
)
|
||||||
else
|
else
|
||||||
bootcfg=$(cat << EOF
|
bootcfg=$(
|
||||||
|
cat <<EOF
|
||||||
boot.loader.grub.device = "$grubdev";
|
boot.loader.grub.device = "$grubdev";
|
||||||
EOF
|
EOF
|
||||||
)
|
)
|
||||||
fi
|
fi
|
||||||
|
|
||||||
availableKernelModules=('"ata_piix"' '"uhci_hcd"' '"xen_blkfront"')
|
availableKernelModules=('"ata_piix"' '"uhci_hcd"' '"xen_blkfront"')
|
||||||
if isX86_64; then
|
if isX86_64; then
|
||||||
availableKernelModules+=('"vmw_pvscsi"')
|
availableKernelModules+=('"vmw_pvscsi"')
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# If you rerun this later, be sure to prune the filesSystems attr
|
# 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, ... }:
|
{ modulesPath, ... }:
|
||||||
{
|
{
|
||||||
imports = [ (modulesPath + "/profiles/qemu-guest.nix") ];
|
imports = [ (modulesPath + "/profiles/qemu-guest.nix") ];
|
||||||
|
@ -138,12 +138,12 @@ $bootcfg
|
||||||
}
|
}
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
[[ -n "$doNetConf" ]] && makeNetworkingConf || true
|
[[ -n "$doNetConf" ]] && makeNetworkingConf || true
|
||||||
}
|
}
|
||||||
|
|
||||||
makeLightsailConf() {
|
makeLightsailConf() {
|
||||||
mkdir -p /etc/nixos
|
mkdir -p /etc/nixos
|
||||||
cat > /etc/nixos/configuration.nix << EOF
|
cat >/etc/nixos/configuration.nix <<EOF
|
||||||
{ config, pkgs, modulesPath, ... }:
|
{ config, pkgs, modulesPath, ... }:
|
||||||
{
|
{
|
||||||
imports = [ "\${modulesPath}/virtualisation/amazon-image.nix" ];
|
imports = [ "\${modulesPath}/virtualisation/amazon-image.nix" ];
|
||||||
|
@ -152,21 +152,22 @@ EOF
|
||||||
}
|
}
|
||||||
|
|
||||||
makeNetworkingConf() {
|
makeNetworkingConf() {
|
||||||
# XXX It'd be better if we used procfs for all this...
|
# XXX It'd be better if we used procfs for all this...
|
||||||
local IFS=$'\n'
|
local IFS=$'\n'
|
||||||
eth0_name=$(ip address show | grep '^2:' | awk -F': ' '{print $2}')
|
eth0_name=$(ip address show | grep '^2:' | awk -F': ' '{print $2}')
|
||||||
eth0_ip4s=$(ip address show dev "$eth0_name" | grep 'inet ' | sed -r 's|.*inet ([0-9.]+)/([0-9]+).*|{ address="\1"; prefixLength=\2; }|')
|
eth0_ip4s=$(ip address show dev "$eth0_name" | grep 'inet ' | sed -r 's|.*inet ([0-9.]+)/([0-9]+).*|{ address="\1"; prefixLength=\2; }|')
|
||||||
eth0_ip6s=$(ip address show dev "$eth0_name" | grep 'inet6 ' | sed -r 's|.*inet6 ([0-9a-f:]+)/([0-9]+).*|{ address="\1"; prefixLength=\2; }|' || '')
|
eth0_ip6s=$(ip address show dev "$eth0_name" | grep 'inet6 ' | sed -r 's|.*inet6 ([0-9a-f:]+)/([0-9]+).*|{ address="\1"; prefixLength=\2; }|' || '')
|
||||||
gateway=$(ip route show dev "$eth0_name" | grep default | sed -r 's|default via ([0-9.]+).*|\1|')
|
gateway=$(ip route show dev "$eth0_name" | grep default | sed -r 's|default via ([0-9.]+).*|\1|')
|
||||||
gateway6=$(ip -6 route show dev "$eth0_name" | grep default | sed -r 's|default via ([0-9a-f:]+).*|\1|' || true)
|
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|')
|
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
|
eth1_name=$(ip address show | grep '^3:' | awk -F': ' '{print $2}') || true
|
||||||
if [ -n "$eth1_name" ];then
|
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_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=$(cat << EOF
|
interfaces1=$(
|
||||||
|
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)
|
||||||
|
@ -176,24 +177,24 @@ makeNetworkingConf() {
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
EOF
|
EOF
|
||||||
)
|
)
|
||||||
extraRules1="ATTR{address}==\"${ether1}\", NAME=\"${eth1_name}\""
|
extraRules1="ATTR{address}==\"${ether1}\", NAME=\"${eth1_name}\""
|
||||||
else
|
else
|
||||||
interfaces1=""
|
interfaces1=""
|
||||||
extraRules1=""
|
extraRules1=""
|
||||||
fi
|
fi
|
||||||
|
|
||||||
readarray nameservers < <(grep ^nameserver /etc/resolv.conf | sed -r \
|
readarray nameservers < <(grep ^nameserver /etc/resolv.conf | sed -r \
|
||||||
-e 's/^nameserver[[:space:]]+([0-9.a-fA-F:]+).*/"\1"/' \
|
-e 's/^nameserver[[:space:]]+([0-9.a-fA-F:]+).*/"\1"/' \
|
||||||
-e 's/127[0-9.]+/8.8.8.8/' \
|
-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
|
if [[ "$eth0_name" = eth* ]]; then
|
||||||
predictable_inames="usePredictableInterfaceNames = lib.mkForce false;"
|
predictable_inames="usePredictableInterfaceNames = lib.mkForce false;"
|
||||||
else
|
else
|
||||||
predictable_inames="usePredictableInterfaceNames = lib.mkForce true;"
|
predictable_inames="usePredictableInterfaceNames = lib.mkForce true;"
|
||||||
fi
|
fi
|
||||||
cat > /etc/nixos/networking.nix << EOF
|
cat >/etc/nixos/networking.nix <<EOF
|
||||||
{ lib, ... }: {
|
{ lib, ... }: {
|
||||||
# This file was populated at runtime with the networking
|
# This file was populated at runtime with the networking
|
||||||
# details gathered from the active system.
|
# details gathered from the active system.
|
||||||
|
@ -229,204 +230,239 @@ EOF
|
||||||
}
|
}
|
||||||
|
|
||||||
checkExistingSwap() {
|
checkExistingSwap() {
|
||||||
SWAPSHOW=$(swapon --show --noheadings --raw)
|
SWAPSHOW=$(swapon --show --noheadings --raw)
|
||||||
zramswap=true
|
zramswap=true
|
||||||
swapcfg=""
|
swapcfg=""
|
||||||
if [[ -n "$SWAPSHOW" ]]; then
|
if [[ -n "$SWAPSHOW" ]]; then
|
||||||
SWAP_DEVICE="${SWAPSHOW%% *}"
|
SWAP_DEVICE="${SWAPSHOW%% *}"
|
||||||
if [[ "$SWAP_DEVICE" == "/dev/"* ]]; then
|
if [[ "$SWAP_DEVICE" == "/dev/"* ]]; then
|
||||||
zramswap=false
|
zramswap=false
|
||||||
swapcfg="swapDevices = [ { device = \"${SWAP_DEVICE}\"; } ];"
|
swapcfg="swapDevices = [ { device = \"${SWAP_DEVICE}\"; } ];"
|
||||||
NO_SWAP=true
|
NO_SWAP=true
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
makeSwap() {
|
makeSwap() {
|
||||||
swapFile=$(mktemp /tmp/nixos-infect.XXXXX.swp)
|
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"
|
chmod 0600 "$swapFile"
|
||||||
mkswap "$swapFile"
|
mkswap "$swapFile"
|
||||||
swapon -v "$swapFile"
|
swapon -v "$swapFile"
|
||||||
}
|
}
|
||||||
|
|
||||||
removeSwap() {
|
removeSwap() {
|
||||||
swapoff -a
|
swapoff -a
|
||||||
rm -vf /tmp/nixos-infect.*.swp
|
rm -vf /tmp/nixos-infect.*.swp
|
||||||
}
|
}
|
||||||
|
|
||||||
isX86_64() {
|
isX86_64() {
|
||||||
[[ "$(uname -m)" == "x86_64" ]]
|
[[ "$(uname -m)" == "x86_64" ]]
|
||||||
}
|
}
|
||||||
|
|
||||||
isEFI() {
|
isEFI() {
|
||||||
[ -d /sys/firmware/efi ]
|
[ -d /sys/firmware/efi ]
|
||||||
}
|
}
|
||||||
|
|
||||||
findESP() {
|
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" ]] && { echo "ERROR: No ESP mount point found"; return 1; }
|
[[ -z "$esp" ]] && {
|
||||||
for uuid in /dev/disk/by-uuid/*; do
|
echo "ERROR: No ESP mount point found"
|
||||||
[[ $(readlink -f "$uuid") == "$esp" ]] && echo $uuid && return 0
|
return 1
|
||||||
done
|
}
|
||||||
|
for uuid in /dev/disk/by-uuid/*; do
|
||||||
|
[[ $(readlink -f "$uuid") == "$esp" ]] && echo $uuid && return 0
|
||||||
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
prepareEnv() {
|
prepareEnv() {
|
||||||
# $esp and $grubdev are used in makeConf()
|
# $esp and $grubdev are used in makeConf()
|
||||||
if isEFI; then
|
if isEFI; then
|
||||||
esp="$(findESP)"
|
esp="$(findESP)"
|
||||||
else
|
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
|
fi
|
||||||
|
|
||||||
# Retrieve root fs block device
|
# Retrieve root fs block device
|
||||||
# (get root mount) (get partition or logical volume)
|
# (get root mount) (get partition or logical volume)
|
||||||
rootfsdev=$(mount | grep "on / type" | awk '{print $1;}')
|
rootfsdev=$(mount | grep "on / type" | awk '{print $1;}')
|
||||||
rootfstype=$(df $rootfsdev --output=fstype | sed 1d)
|
rootfstype=$(df $rootfsdev --output=fstype | sed 1d)
|
||||||
|
|
||||||
# DigitalOcean doesn't seem to set USER while running user data
|
# DigitalOcean doesn't seem to set USER while running user data
|
||||||
export USER="root"
|
export USER="root"
|
||||||
export HOME="/root"
|
export HOME="/root"
|
||||||
|
|
||||||
# Nix installer tries to use sudo regardless of whether we're already uid 0
|
# Nix installer tries to use sudo regardless of whether we're already uid 0
|
||||||
#which sudo || { sudo() { eval "$@"; }; export -f sudo; }
|
#which sudo || { sudo() { eval "$@"; }; export -f sudo; }
|
||||||
# shellcheck disable=SC2174
|
# shellcheck disable=SC2174
|
||||||
mkdir -p -m 0755 /nix
|
mkdir -p -m 0755 /nix
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
(
|
||||||
for arg in "$@"; do
|
local isStdout=1
|
||||||
case "$arg" in
|
for arg in "$@"; do
|
||||||
"-o")
|
case "$arg" in
|
||||||
echo "-O";
|
"-o")
|
||||||
isStdout=0
|
echo "-O"
|
||||||
;;
|
isStdout=0
|
||||||
"-O")
|
;;
|
||||||
isStdout=0
|
"-O")
|
||||||
;;
|
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() {
|
||||||
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() {
|
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
|
# 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 || { echo "ERROR: Missing both curl and wget"; return 1; }
|
req curl || req wget || {
|
||||||
req bzcat || { echo "ERROR: Missing bzcat"; return 1; }
|
echo "ERROR: Missing both curl and wget"
|
||||||
req xzcat || { echo "ERROR: Missing xzcat"; return 1; }
|
return 1
|
||||||
req groupadd || { echo "ERROR: Missing groupadd"; return 1; }
|
}
|
||||||
req useradd || { echo "ERROR: Missing useradd"; return 1; }
|
req bzcat || {
|
||||||
req ip || { echo "ERROR: Missing ip"; return 1; }
|
echo "ERROR: Missing bzcat"
|
||||||
req awk || { echo "ERROR: Missing awk"; return 1; }
|
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
|
||||||
chmod 600 /etc/ssh/ssh_host_*_key
|
chmod 600 /etc/ssh/ssh_host_*_key
|
||||||
}
|
}
|
||||||
|
|
||||||
infect() {
|
infect() {
|
||||||
# Add nix build users
|
# Add nix build users
|
||||||
# FIXME run only if necessary, rather than defaulting true
|
# FIXME run only if necessary, rather than defaulting true
|
||||||
groupadd nixbld -g 30000 || true
|
groupadd nixbld -g 30000 || true
|
||||||
for i in {1..10}; do
|
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
|
useradd -c "Nix build user $i" -d /var/empty -g nixbld -G nixbld -M -N -r -s "$(which nologin)" "nixbld$i" || true
|
||||||
done
|
done
|
||||||
# TODO use addgroup and adduser as fallbacks
|
# TODO use addgroup and adduser as fallbacks
|
||||||
#addgroup nixbld -g 30000 || true
|
#addgroup nixbld -g 30000 || true
|
||||||
#for i in {1..10}; do adduser -DH -G nixbld nixbld$i || true; done
|
#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
|
# shellcheck disable=SC1090
|
||||||
source ~/.nix-profile/etc/profile.d/nix.sh
|
source ~/.nix-profile/etc/profile.d/nix.sh
|
||||||
|
|
||||||
[[ -z "$NIX_CHANNEL" ]] && NIX_CHANNEL="nixos-23.05"
|
[[ -z "$NIX_CHANNEL" ]] && NIX_CHANNEL="nixos-23.05"
|
||||||
nix-channel --remove nixpkgs
|
nix-channel --remove nixpkgs
|
||||||
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* ]]
|
if [[ $NIXOS_CONFIG = http* ]]; then
|
||||||
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
|
|
||||||
|
|
||||||
export NIXOS_CONFIG="${NIXOS_CONFIG:-/etc/nixos/configuration.nix}"
|
export NIXOS_CONFIG="${NIXOS_CONFIG:-/etc/nixos/configuration.nix}"
|
||||||
|
|
||||||
nix-env --set \
|
nix-env --set \
|
||||||
-I nixpkgs=$HOME/.nix-defexpr/channels/nixos \
|
-I nixpkgs=$HOME/.nix-defexpr/channels/nixos \
|
||||||
-f '<nixpkgs/nixos>' \
|
-f '<nixpkgs/nixos>' \
|
||||||
-p /nix/var/nix/profiles/system \
|
-p /nix/var/nix/profiles/system \
|
||||||
-A system
|
-A system
|
||||||
|
|
||||||
# Remove nix installed with curl | bash
|
# Remove nix installed with curl | bash
|
||||||
rm -fv /nix/var/nix/profiles/default*
|
rm -fv /nix/var/nix/profiles/default*
|
||||||
/nix/var/nix/profiles/system/sw/bin/nix-collect-garbage
|
/nix/var/nix/profiles/system/sw/bin/nix-collect-garbage
|
||||||
|
|
||||||
# Reify 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
|
[[ -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
|
# Set label of root partition
|
||||||
if [ -n "$newrootfslabel" ]; then
|
if [ -n "$newrootfslabel" ]; then
|
||||||
echo "Setting label of $rootfsdev to $newrootfslabel"
|
echo "Setting label of $rootfsdev to $newrootfslabel"
|
||||||
e2label "$rootfsdev" "$newrootfslabel"
|
e2label "$rootfsdev" "$newrootfslabel"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Stage the Nix coup d'état
|
# Stage the Nix coup d'état
|
||||||
touch /etc/NIXOS
|
touch /etc/NIXOS
|
||||||
echo etc/nixos >> /etc/NIXOS_LUSTRATE
|
echo etc/nixos >>/etc/NIXOS_LUSTRATE
|
||||||
echo etc/resolv.conf >> /etc/NIXOS_LUSTRATE
|
echo etc/resolv.conf >>/etc/NIXOS_LUSTRATE
|
||||||
echo root/.nix-defexpr/channels >> /etc/NIXOS_LUSTRATE
|
echo root/.nix-defexpr/channels >>/etc/NIXOS_LUSTRATE
|
||||||
(cd / && ls etc/ssh/ssh_host_*_key* || true) >> /etc/NIXOS_LUSTRATE
|
(cd / && ls etc/ssh/ssh_host_*_key* || true) >>/etc/NIXOS_LUSTRATE
|
||||||
|
|
||||||
rm -rf /boot.bak
|
rm -rf /boot.bak
|
||||||
isEFI && umount "$esp"
|
isEFI && umount "$esp"
|
||||||
|
|
||||||
mv -v /boot /boot.bak || { cp -a /boot /boot.bak ; rm -rf /boot/* ; umount /boot ; }
|
mv -v /boot /boot.bak || {
|
||||||
if isEFI; then
|
cp -a /boot /boot.bak
|
||||||
mkdir -p /boot
|
rm -rf /boot/*
|
||||||
mount "$esp" /boot
|
umount /boot
|
||||||
find /boot -depth ! -path /boot -exec rm -rf {} +
|
}
|
||||||
fi
|
if isEFI; then
|
||||||
/nix/var/nix/profiles/system/bin/switch-to-configuration boot
|
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
|
[ "$PROVIDER" = "digitalocean" ] && doNetConf=y # digitalocean requires detailed network config to be generated
|
||||||
|
@ -439,14 +475,14 @@ checkEnv
|
||||||
prepareEnv
|
prepareEnv
|
||||||
checkExistingSwap
|
checkExistingSwap
|
||||||
if [[ -z "$NO_SWAP" ]]; then
|
if [[ -z "$NO_SWAP" ]]; then
|
||||||
makeSwap # smallest (512MB) droplet needs extra memory!
|
makeSwap # smallest (512MB) droplet needs extra memory!
|
||||||
fi
|
fi
|
||||||
makeConf
|
makeConf
|
||||||
infect
|
infect
|
||||||
if [[ -z "$NO_SWAP" ]]; then
|
if [[ -z "$NO_SWAP" ]]; then
|
||||||
removeSwap
|
removeSwap
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ -z "$NO_REBOOT" ]]; then
|
if [[ -z "$NO_REBOOT" ]]; then
|
||||||
reboot
|
reboot
|
||||||
fi
|
fi
|
||||||
|
|
Loading…
Reference in a new issue