selfprivacy-nixos-infect/nixos-infect

370 lines
11 KiB
Plaintext
Raw Normal View History

2020-12-07 09:09:48 +00:00
#! /usr/bin/env bash
# More info at: https://github.com/elitak/nixos-infect
set -e -o pipefail
makeConf() {
# Skip everything if main config already present
[[ -e /etc/nixos/configuration.nix ]] && return 0
2022-04-06 12:50:45 +00:00
if [[ $PASSWORD == null ]]; then
export PASSWORD=$(printf $ENCODED_PASSWORD | base64 --decode)
2022-04-06 16:49:24 +00:00
fi
2022-04-06 12:50:45 +00:00
export ESCAPED_PASSWORD=$(printf $ENCODED_PASSWORD | base64 --decode | jq -Rs .)
export HASHED_PASSWORD=$( mkpasswd -m sha-512 "$PASSWORD" )
2020-12-07 09:09:48 +00:00
# NB <<"EOF" quotes / $ ` in heredocs, <<EOF does not
2021-11-16 07:00:26 +00:00
mkdir -p /etc/nixos
2021-11-17 17:37:35 +00:00
git clone https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-config.git /etc/nixos
2020-12-21 13:18:22 +00:00
2020-12-07 09:09:48 +00:00
# Prevent grep for sending error code 1 (and halting execution) when no lines are selected : https://www.unix.com/man-page/posix/1P/grep
2021-11-16 07:00:26 +00:00
local IFS=$'\n'
for trypath in /root/.ssh/authorized_keys /home/$SUDO_USER/.ssh/authorized_keys $HOME/.ssh/authorized_keys; do
2020-12-07 09:09:48 +00:00
[[ -r "$trypath" ]] \
&& keys=$(sed -E 's/^.*((ssh|ecdsa)-[^[:space:]]+)[[:space:]]+([^[:space:]]+)([[:space:]]*.*)$/\1 \3\4/' "$trypath") \
&& break
done
local network_import=""
2021-11-18 17:24:49 +00:00
cat > /etc/nixos/userdata/userdata.json << EOF
{
"api": {
"token": "$API_TOKEN",
"skippedMigrations": ["migrate_to_selfprivacy_channel", "mount_volume"]
2021-11-18 17:24:49 +00:00
},
"backblaze": {
"accountId": "$BACKBLAZE_KEY_ID",
"accountKey": "$BACKBLAZE_ACCOUNT_KEY",
"bucket": "$BACKBLAZE_BUCKET_NAME"
},
"bitwarden": {
"enable": true,
"location": "sdb"
2021-11-18 17:24:49 +00:00
},
"cloudflare": {
"apiKey": "$CF_TOKEN"
},
"databasePassword": "$DB_PASSWORD",
"domain": "$DOMAIN",
"hashedMasterPassword": "$HASHED_PASSWORD",
"hostname": "$HOSTNAME",
2021-11-18 17:24:49 +00:00
"nextcloud": {
"enable": true,
2022-04-06 16:55:19 +00:00
"adminPassword": $ESCAPED_PASSWORD,
"databasePassword": $ESCAPED_PASSWORD,
"location": "sdb"
2021-11-18 17:24:49 +00:00
},
"gitea": {
"enable": true,
"location": "sdb"
2021-11-18 17:24:49 +00:00
},
"jitsi": {
"enable": true
},
"ocserv": {
"enable": false
2021-11-18 17:24:49 +00:00
},
"pleroma": {
"enable": false,
"location": "sdb"
2021-11-18 17:24:49 +00:00
},
"timezone": "Europe/Uzhgorod",
2022-04-06 16:55:19 +00:00
"resticPassword": $ESCAPED_PASSWORD,
"username": "$LUSER",
"volumes": [
{
"device": "/dev/sdb",
"mountPoint": "/volumes/sdb",
"fsType": "ext4"
}
],
"useBinds": true
2021-11-18 17:24:49 +00:00
}
EOF
2021-11-18 19:49:30 +00:00
chmod 0600 /etc/nixos/userdata/userdata.json
2020-12-07 09:09:48 +00:00
2021-11-16 07:00:26 +00:00
if isEFI; then
bootcfg=$(cat << EOF
boot.loader.grub = {
efiSupport = true;
efiInstallAsRemovable = true;
device = "nodev";
2020-12-23 15:52:14 +00:00
};
2021-11-16 07:00:26 +00:00
fileSystems."/boot" = { device = "$esp"; fsType = "vfat"; };
2020-12-07 09:09:48 +00:00
EOF
2021-11-16 07:00:26 +00:00
)
else
bootcfg=$(cat << EOF
2020-12-07 09:09:48 +00:00
boot.loader.grub.device = "$grubdev";
2021-02-10 09:43:06 +00:00
EOF
2021-11-16 07:00:26 +00:00
)
fi
2021-02-10 09:43:06 +00:00
2021-11-16 07:00:26 +00:00
# If you rerun this later, be sure to prune the filesSystems attr
cat > /etc/nixos/hardware-configuration.nix << EOF
{ modulesPath, ... }:
2021-02-10 09:43:06 +00:00
{
2021-11-16 07:00:26 +00:00
imports = [ (modulesPath + "/profiles/qemu-guest.nix") ];
$bootcfg
boot.initrd.kernelModules = [ "nvme" ];
fileSystems."/" = { device = "$rootfsdev"; fsType = "$rootfstype"; };
2021-04-09 13:46:32 +00:00
}
2020-12-07 09:09:48 +00:00
EOF
2021-01-08 10:04:06 +00:00
[[ -n "$doNetConf" ]] && makeNetworkingConf || true
2020-12-07 09:09:48 +00:00
}
makeNetworkingConf() {
# XXX It'd be better if we used procfs for all this...
local IFS=$'\n'
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_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|')
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_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=<< EOF
$eth1_name = {
ipv4.addresses = [$(for a in "${eth1_ip4s[@]}"; do echo -n "
$a"; done)
];
ipv6.addresses = [$(for a in "${eth1_ip6s[@]}"; do echo -n "
$a"; done)
];
EOF
extraRules1="ATTR{address}==\"${ether1}\", NAME=\"${eth1_name}\""
else
interfaces1=""
extraRules1=""
fi
2021-11-16 07:00:26 +00:00
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/' )
2020-12-07 09:09:48 +00:00
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
{ lib, ... }: {
# This file was populated at runtime with the networking
# details gathered from the active system.
networking = {
nameservers = [ ${nameservers[@]} ];
defaultGateway = "${gateway}";
defaultGateway6 = "${gateway6}";
dhcpcd.enable = false;
$predictable_inames
interfaces = {
$eth0_name = {
ipv4.addresses = [$(for a in "${eth0_ip4s[@]}"; do echo -n "
$a"; done)
];
ipv6.addresses = [$(for a in "${eth0_ip6s[@]}"; do echo -n "
$a"; done)
];
ipv4.routes = [ { address = "${gateway}"; prefixLength = 32; } ];
2021-11-16 07:00:26 +00:00
ipv6.routes = [ { address = "${gateway6}"; prefixLength = 128; } ];
2020-12-07 09:09:48 +00:00
};
$interfaces1
};
};
services.udev.extraRules = ''
ATTR{address}=="${ether0}", NAME="${eth0_name}"
$extraRules1
'';
}
EOF
}
makeSwap() {
# TODO check currently available swapspace first
swapFile=$(mktemp /tmp/nixos-infect.XXXXX.swp)
dd if=/dev/zero "of=$swapFile" bs=1M count=$((1*1024))
chmod 0600 "$swapFile"
mkswap "$swapFile"
swapon -v "$swapFile"
}
removeSwap() {
swapoff -a
rm -vf /tmp/nixos-infect.*.swp
}
2021-11-16 07:00:26 +00:00
isEFI() {
[ -d /sys/firmware/efi ]
}
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
done
[[ -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
}
2020-12-07 09:09:48 +00:00
prepareEnv() {
2021-11-16 07:00:26 +00:00
# $esp and $grubdev are used in makeConf()
if isEFI; then
esp="$(findESP)"
else
for grubdev in /dev/vda /dev/sda /dev/nvme0n1 ; do [[ -e $grubdev ]] && break; done
fi
2020-12-07 09:09:48 +00:00
# Retrieve root fs block device
# (get root mount) (get partition or logical volume)
rootfsdev=$(mount | grep "on / type" | awk '{print $1;}')
2021-11-16 07:00:26 +00:00
rootfstype=$(df $rootfsdev --output=fstype | sed 1d)
2020-12-07 09:09:48 +00:00
# DigitalOcean doesn't seem to set USER while running user data
export USER="root"
export HOME="/root"
# Nix installer tries to use sudo regardless of whether we're already uid 0
#which sudo || { sudo() { eval "$@"; }; export -f sudo; }
# shellcheck disable=SC2174
mkdir -p -m 0755 /nix
}
fakeCurlUsingWget() {
# Use adapted wget if curl is missing
which wget && { \
curl() {
eval "wget $(
(local isStdout=1
for arg in "$@"; do
case "$arg" in
"-o")
echo "-O";
isStdout=0
;;
"-O")
isStdout=0
;;
"-L")
;;
*)
echo "$arg"
;;
esac
done;
[[ $isStdout -eq 1 ]] && echo "-O-"
)| tr '\n' ' '
)"
}; export -f curl; }
}
req() {
type "$1" > /dev/null 2>&1 || which "$1" > /dev/null 2>&1
}
checkEnv() {
2021-11-16 07:00:26 +00:00
[[ "$(whoami)" == "root" ]] || { echo "ERROR: Must run as root"; return 1; }
2020-12-07 09:09:48 +00:00
# Perform some easy fixups before checking
# TODO prevent multiple calls to apt-get update
2021-11-16 07:00:26 +00:00
(which dnf && dnf install -y perl-Digest-SHA) || true # Fedora 24
2020-12-07 09:09:48 +00:00
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
2021-11-16 07:00:26 +00:00
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; }
2020-12-07 09:09:48 +00:00
}
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
curl -L https://nixos.org/nix/install | $SHELL
# shellcheck disable=SC1090
source ~/.nix-profile/etc/profile.d/nix.sh
nix-channel --remove nixpkgs
nix-channel --add "https://channel.selfprivacy.org/nixos-selfpricacy" nixos
2020-12-07 09:09:48 +00:00
nix-channel --update
export NIXOS_CONFIG=/etc/nixos/configuration.nix
nix-env --set \
-I nixpkgs=$HOME/.nix-defexpr/channels/nixos \
-f '<nixpkgs/nixos>' \
-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
# Reify resolv.conf
[[ -L /etc/resolv.conf ]] && mv -v /etc/resolv.conf /etc/resolv.conf.lnk && cat /etc/resolv.conf.lnk > /etc/resolv.conf
# 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
rm -rf /boot.bak
2021-11-16 07:00:26 +00:00
isEFI && umount "$esp"
2020-12-07 09:09:48 +00:00
mv -v /boot /boot.bak
2021-11-16 07:00:26 +00:00
if isEFI; then
mkdir /boot
mount "$esp" /boot
find /boot -depth ! -path /boot -exec rm -rf {} +
fi
2020-12-07 09:09:48 +00:00
/nix/var/nix/profiles/system/bin/switch-to-configuration boot
}
[ "$PROVIDER" = "digitalocean" ] && doNetConf=y # digitalocean requires detailed network config to be generated
2021-01-05 13:49:35 +00:00
apt update
2022-04-06 12:50:45 +00:00
apt install -y git tar wget curl whois jq
2021-11-16 07:00:26 +00:00
checkEnv
2020-12-07 09:09:48 +00:00
prepareEnv
makeSwap # smallest (512MB) droplet needs extra memory!
makeConf
infect
removeSwap
if [[ -z "$NO_REBOOT" ]]; then
reboot
fi