2020-12-07 09:09:48 +00:00
|
|
|
#! /usr/bin/env bash
|
|
|
|
|
2023-11-18 02:47:34 +00:00
|
|
|
# More info at:
|
|
|
|
# - https://github.com/elitak/nixos-infect
|
|
|
|
# - https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-infect
|
|
|
|
|
2023-12-27 23:12:43 +00:00
|
|
|
# mandatory variables
|
2023-11-18 02:47:34 +00:00
|
|
|
: "${API_TOKEN:?API_TOKEN variable is not set}"
|
2023-12-27 14:08:03 +00:00
|
|
|
: "${DNS_PROVIDER_TOKEN:?DNS_PROVIDER_TOKEN variable is not set}"
|
|
|
|
: "${DNS_PROVIDER_TYPE:?DNS_PROVIDER_TYPE variable is not set}"
|
2023-11-18 02:47:34 +00:00
|
|
|
: "${DOMAIN:?DOMAIN variable is not set}"
|
2023-12-27 14:08:03 +00:00
|
|
|
: "${ENCODED_PASSWORD:?ENCODED_PASSWORD variable is not set}"
|
2023-11-18 02:47:34 +00:00
|
|
|
: "${HOSTNAME:?HOSTNAME variable is not set}"
|
2023-12-27 14:08:03 +00:00
|
|
|
: "${LUSER:?LUSER variable is not set}"
|
2023-11-18 02:47:34 +00:00
|
|
|
: "${PROVIDER:?PROVIDER variable is not set}"
|
2023-12-27 14:08:03 +00:00
|
|
|
|
2023-12-27 23:12:43 +00:00
|
|
|
# optional variables
|
|
|
|
: "${NIXOS_CONFIG_ID:=default}"
|
2023-12-05 01:28:15 +00:00
|
|
|
: "${SSH_AUTHORIZED_KEY:=}"
|
2023-12-27 23:12:43 +00:00
|
|
|
: "${VOLUME_DEV_PATH:=}"
|
2023-12-28 16:19:34 +00:00
|
|
|
: "${STAGING_ACME:=false}"
|
2023-11-18 02:47:34 +00:00
|
|
|
|
2024-03-12 13:28:08 +00:00
|
|
|
readonly CONFIG_URL="https://git.selfprivacy.org/api/v1/repos/SelfPrivacy/selfprivacy-nixos-template/archive/75eba2cf8dd3f992c501ea36eeec43b35605a3a3.tar.gz"
|
2023-12-28 16:16:33 +00:00
|
|
|
readonly NIX_VERSION="2.18.1"
|
|
|
|
|
2023-12-11 11:55:46 +00:00
|
|
|
readonly NL=$'\n'
|
2023-11-18 02:47:34 +00:00
|
|
|
readonly LOCAL_FLAKE_DIR="/etc/nixos"
|
|
|
|
readonly SECRETS_FILEPATH="/etc/selfprivacy/secrets.json"
|
2023-12-22 17:33:07 +00:00
|
|
|
readonly NIX="/root/.nix-profile/bin/nix"
|
|
|
|
readonly NIX_OPTS=(--extra-experimental-features "nix-command flakes")
|
2023-12-11 11:55:46 +00:00
|
|
|
DoNetConf=
|
2023-11-18 02:47:34 +00:00
|
|
|
|
2023-12-05 01:28:15 +00:00
|
|
|
genOptionalSsh() {
|
2023-12-28 16:00:44 +00:00
|
|
|
if [ -n "${SSH_AUTHORIZED_KEY}" ]; then
|
|
|
|
cat << EOF
|
2023-12-05 01:28:15 +00:00
|
|
|
"ssh": { "rootKeys": [ "${SSH_AUTHORIZED_KEY}" ] },
|
|
|
|
EOF
|
2023-12-28 16:00:44 +00:00
|
|
|
fi
|
2023-12-05 01:28:15 +00:00
|
|
|
}
|
|
|
|
|
2023-12-27 20:56:47 +00:00
|
|
|
# TODO receive disk device path from outside
|
2023-12-27 16:53:02 +00:00
|
|
|
determine2ndDisk() {
|
2023-12-27 23:12:43 +00:00
|
|
|
local volDevPath rootFsDev
|
|
|
|
if [ -n "$VOLUME_DEV_PATH" ]; then
|
|
|
|
volDevPath="$VOLUME_DEV_PATH"
|
|
|
|
else
|
|
|
|
case "$PROVIDER" in
|
2023-12-28 16:07:59 +00:00
|
|
|
HETZNER)
|
2023-12-27 23:12:43 +00:00
|
|
|
volDevPath="/dev/sdb"
|
|
|
|
;;
|
2023-12-28 16:07:59 +00:00
|
|
|
DIGITALOCEAN)
|
2023-12-27 23:12:43 +00:00
|
|
|
volDevPath="/dev/sda"
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
return 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
fi
|
2023-12-27 20:56:47 +00:00
|
|
|
rootFsDev="$(findmnt -no source -T /)"
|
2023-12-27 23:12:43 +00:00
|
|
|
[[ -b "$volDevPath" && "$volDevPath" != "$rootFsDev" ]] && echo "$volDevPath"
|
2023-12-27 16:53:02 +00:00
|
|
|
}
|
|
|
|
|
2023-11-18 02:47:34 +00:00
|
|
|
# Merge original userdata.json with deployment specific fields and print result.
|
|
|
|
genUserdata() {
|
2023-12-27 16:53:02 +00:00
|
|
|
local hashed_password diskDeviceName userdata_infect
|
|
|
|
hashed_password="$(mkpasswd -m sha-512 "$USER_PASS")"
|
|
|
|
diskDevice="$(determine2ndDisk)"
|
2023-12-28 17:06:35 +00:00
|
|
|
diskDeviceName="${diskDevice##/dev/}"
|
2023-11-18 02:47:34 +00:00
|
|
|
|
|
|
|
userdata_infect=$(cat << EOF
|
|
|
|
{
|
2023-12-05 01:28:15 +00:00
|
|
|
$(genOptionalSsh)
|
2023-11-18 02:47:34 +00:00
|
|
|
"dns": {
|
|
|
|
"provider": "$DNS_PROVIDER_TYPE",
|
2023-11-18 04:35:23 +00:00
|
|
|
"useStagingACME": $STAGING_ACME
|
2023-11-18 02:47:34 +00:00
|
|
|
},
|
|
|
|
"server": {
|
|
|
|
"provider": "$PROVIDER"
|
|
|
|
},
|
2023-11-20 21:18:51 +00:00
|
|
|
"domain": "$DOMAIN",
|
2023-12-27 16:53:02 +00:00
|
|
|
"hashedMasterPassword": "$hashed_password",
|
2023-11-20 21:18:51 +00:00
|
|
|
"hostname": "$HOSTNAME",
|
2023-12-27 16:53:02 +00:00
|
|
|
"username": "$LUSER",
|
|
|
|
"volumes": [
|
|
|
|
{
|
|
|
|
"device": "$diskDevice",
|
|
|
|
"mountPoint": "/volumes/$diskDeviceName",
|
|
|
|
"fsType": "ext4"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"modules": {
|
|
|
|
"bitwarden": {
|
|
|
|
"location": "$diskDeviceName"
|
|
|
|
},
|
|
|
|
"gitea": {
|
|
|
|
"location": "$diskDeviceName"
|
|
|
|
},
|
|
|
|
"nextcloud": {
|
|
|
|
"location": "$diskDeviceName"
|
|
|
|
},
|
|
|
|
"pleroma": {
|
|
|
|
"location": "$diskDeviceName"
|
|
|
|
},
|
|
|
|
"simple-nixos-mailserver": {
|
|
|
|
"location": "$diskDeviceName"
|
|
|
|
}
|
|
|
|
}
|
2023-11-18 02:47:34 +00:00
|
|
|
}
|
|
|
|
EOF
|
|
|
|
)
|
2022-11-16 08:06:15 +00:00
|
|
|
|
2023-11-18 02:47:34 +00:00
|
|
|
jq -s '.[0] * .[1]' \
|
|
|
|
"${1:?no userdata.json given to merge with}" <(printf "%s" "$userdata_infect")
|
|
|
|
}
|
2020-12-21 13:18:22 +00:00
|
|
|
|
2023-11-18 02:47:34 +00:00
|
|
|
genSecrets() {
|
2023-12-27 14:01:57 +00:00
|
|
|
local dbpass
|
|
|
|
dbpass="$(shuf --random-source=/dev/urandom -erz -n32 {A..Z} {a..z} {0..9} | tr -d '\n')"
|
|
|
|
|
2023-11-18 02:47:34 +00:00
|
|
|
cat << EOF
|
2021-11-18 17:24:49 +00:00
|
|
|
{
|
|
|
|
"api": {
|
2022-10-10 14:40:46 +00:00
|
|
|
"token": "$API_TOKEN",
|
|
|
|
"skippedMigrations": ["migrate_to_selfprivacy_channel", "mount_volume"]
|
2021-11-18 17:24:49 +00:00
|
|
|
},
|
2023-12-27 14:01:57 +00:00
|
|
|
"databasePassword": "$dbpass",
|
2023-11-18 02:47:34 +00:00
|
|
|
"dns": {
|
2023-12-20 12:36:14 +00:00
|
|
|
"apiKey": "$DNS_PROVIDER_TOKEN"
|
2021-11-18 17:24:49 +00:00
|
|
|
},
|
2023-11-18 02:47:34 +00:00
|
|
|
"modules": {
|
|
|
|
"nextcloud": {
|
2023-11-29 04:53:23 +00:00
|
|
|
"adminPassword": "$USER_PASS",
|
|
|
|
"databasePassword": "$USER_PASS"
|
2023-11-18 02:47:34 +00:00
|
|
|
}
|
2023-12-16 05:44:02 +00:00
|
|
|
}
|
2021-11-18 17:24:49 +00:00
|
|
|
}
|
|
|
|
EOF
|
2023-11-18 02:47:34 +00:00
|
|
|
}
|
2020-12-07 09:09:48 +00:00
|
|
|
|
2023-11-18 02:47:34 +00:00
|
|
|
genHardwareConfiguration() {
|
|
|
|
local bootcfg
|
|
|
|
if ((isEFI)); then
|
2021-11-16 07:00:26 +00:00
|
|
|
bootcfg=$(cat << EOF
|
|
|
|
boot.loader.grub = {
|
|
|
|
efiSupport = true;
|
|
|
|
efiInstallAsRemovable = true;
|
|
|
|
device = "nodev";
|
2020-12-23 15:52:14 +00:00
|
|
|
};
|
2023-11-18 02:47:34 +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
|
2023-11-18 02:47:34 +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
|
|
|
|
2023-12-05 03:30:19 +00:00
|
|
|
local currentSystem
|
|
|
|
if ! currentSystem="$($NIX "${NIX_OPTS[@]}" eval --impure --raw --expr builtins.currentSystem)"
|
|
|
|
then
|
|
|
|
echo "cannot determine Nix currentSystem identifier"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2023-12-11 17:11:14 +00:00
|
|
|
# FIXME it's questionable whether these modules are needed at all...
|
|
|
|
declare -a availableKernelModules=()
|
|
|
|
|
2023-12-28 16:07:59 +00:00
|
|
|
[ "$PROVIDER" == "DIGITALOCEAN" ] \
|
2023-12-11 17:11:14 +00:00
|
|
|
&& availableKernelModules+=('"ata_piix"' '"uhci_hcd"' '"xen_blkfront"')
|
|
|
|
|
|
|
|
[ "$(uname -m)" == "x86_64" ] \
|
|
|
|
&& availableKernelModules+=('"vmw_pvscsi"')
|
|
|
|
|
2023-12-05 03:30:19 +00:00
|
|
|
# TODO try nixos-generate-config first, resorting to the way below if failed
|
2023-12-11 17:11:14 +00:00
|
|
|
# FIXME "nvme" is not needed for hetzner?
|
2023-12-05 03:30:19 +00:00
|
|
|
|
2023-11-18 02:47:34 +00:00
|
|
|
cat << EOF
|
2021-11-16 07:00:26 +00:00
|
|
|
{ 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" ];
|
2023-12-11 17:11:14 +00:00
|
|
|
boot.initrd.availableKernelModules = [ ${availableKernelModules[@]} ];
|
2023-11-18 02:47:34 +00:00
|
|
|
fileSystems."/" = { device = "$ROOTFSDEV"; fsType = "$ROOTFSTYPE"; };
|
2023-12-05 03:30:19 +00:00
|
|
|
nixpkgs.hostPlatform = "${currentSystem}";
|
|
|
|
}
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
genDeploymentConfiguration() {
|
|
|
|
local release
|
|
|
|
|
|
|
|
if ! release="$($NIX "${NIX_OPTS[@]}" eval --impure --raw --expr "(builtins.getFlake (builtins.toString ${LOCAL_FLAKE_DIR})).inputs.selfprivacy-nixos-config.inputs.nixpkgs.lib.trivial.release")"
|
|
|
|
then
|
|
|
|
echo "cannot determine NixOS release version"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
cat << EOF
|
2023-12-22 19:04:38 +00:00
|
|
|
{ lib, ... }: {
|
2023-12-05 03:30:19 +00:00
|
|
|
# The content below is static and belongs to this deployment only!
|
|
|
|
# Do not copy this configuration file to another NixOS installation!
|
|
|
|
|
2023-12-22 19:04:38 +00:00
|
|
|
system.stateVersion = lib.mkDefault "$release";`
|
2023-12-11 11:55:46 +00:00
|
|
|
`$(if [ "$DoNetConf" == "y" ]; then echo -e "$NL"; genNetworkingConf; fi)
|
2021-04-09 13:46:32 +00:00
|
|
|
}
|
2020-12-07 09:09:48 +00:00
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
2023-11-18 02:47:34 +00:00
|
|
|
setupConf() {
|
|
|
|
mkdir -p ${LOCAL_FLAKE_DIR}
|
2023-12-27 10:13:40 +00:00
|
|
|
if ! curl --fail "${CONFIG_URL}" \
|
2023-11-20 20:57:54 +00:00
|
|
|
| tar -xz -C ${LOCAL_FLAKE_DIR} --strip-components=1 --exclude=".*"
|
2023-11-18 02:47:34 +00:00
|
|
|
then
|
|
|
|
echo "Error downloading/extracting top level flake configuration!"
|
|
|
|
exit 1
|
2020-12-07 09:09:48 +00:00
|
|
|
fi
|
|
|
|
|
2023-11-18 02:47:34 +00:00
|
|
|
# generate and write hardware-configuration.nix
|
2023-12-05 03:30:19 +00:00
|
|
|
if ! genHardwareConfiguration > ${LOCAL_FLAKE_DIR}/hardware-configuration.nix
|
|
|
|
then
|
|
|
|
echo "error generating ${LOCAL_FLAKE_DIR}/hardware-configuration.nix"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# generate and write deployment.nix
|
|
|
|
if ! genDeploymentConfiguration > ${LOCAL_FLAKE_DIR}/deployment.nix
|
|
|
|
then
|
|
|
|
echo "error generating ${LOCAL_FLAKE_DIR}/deployment.nix"
|
|
|
|
exit 1
|
|
|
|
fi
|
2020-12-07 09:09:48 +00:00
|
|
|
|
2023-11-18 02:47:34 +00:00
|
|
|
# generate infected userdata based on original
|
|
|
|
local userdataInfected
|
|
|
|
userdataInfected="$(genUserdata ${LOCAL_FLAKE_DIR}/userdata.json)"
|
|
|
|
printf "%s" "$userdataInfected" > ${LOCAL_FLAKE_DIR}/userdata.json
|
|
|
|
|
|
|
|
# generate and write secrets
|
|
|
|
local secrets
|
|
|
|
secrets="$(genSecrets)"
|
|
|
|
install -m0600 <(printf "%s" "$secrets") -DT ${SECRETS_FILEPATH}
|
2020-12-07 09:09:48 +00:00
|
|
|
}
|
|
|
|
|
2023-12-11 11:55:46 +00:00
|
|
|
# shellcheck disable=SC2207
|
|
|
|
genNetworkingConf() {
|
|
|
|
# XXX It'd be better if we used procfs for all this...
|
|
|
|
|
|
|
|
local IFS=$'\n'
|
|
|
|
local eth0_name eth0_ip4s eth0_ip6s gateway gateway6 ether0 eth1_name
|
|
|
|
local interfaces1 extraRules1 predictable_inames
|
|
|
|
|
|
|
|
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; }|')) || true
|
|
|
|
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
|
|
|
|
local eth1_ip4s eth1_ip6s ether1
|
|
|
|
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; }|')" || true
|
|
|
|
ether1="$(ip address show dev "$eth1_name" | grep link/ether | sed -r 's|.*link/ether ([0-9a-f:]+) .*|\1|')"
|
|
|
|
interfaces1=$(cat << 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
|
|
|
|
|
|
|
|
if [[ "$eth0_name" = eth* ]]; then
|
|
|
|
predictable_inames="usePredictableInterfaceNames = lib.mkForce false;"
|
|
|
|
else
|
|
|
|
predictable_inames="usePredictableInterfaceNames = lib.mkForce true;"
|
|
|
|
fi
|
|
|
|
|
2024-03-12 14:43:02 +00:00
|
|
|
local defaultGateway6=${gateway6:+defaultGateway6 = \{ address = \"${gateway6}\"; interface = \"${eth0_name}\"; \};}
|
|
|
|
local ipv6routes=${gateway6:+ipv6.routes = \[ \{ address = \"${gateway6}\"; prefixLength = 128; \} \];}
|
2023-12-11 11:55:46 +00:00
|
|
|
cat << EOF
|
|
|
|
# Networking configuration was populated by nixos-infect with the networking
|
|
|
|
# details gathered from the running system.
|
|
|
|
networking = {
|
|
|
|
defaultGateway = "${gateway}";`
|
|
|
|
`${defaultGateway6:+
|
2024-03-12 14:43:02 +00:00
|
|
|
$defaultGateway6}
|
2023-12-11 11:55:46 +00:00
|
|
|
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; } ];`
|
|
|
|
`${ipv6routes:+
|
|
|
|
$ipv6routes}
|
|
|
|
};`
|
|
|
|
`${interfaces1:+
|
|
|
|
$interfaces1}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
services.udev.extraRules = ''
|
|
|
|
ATTR{address}=="${ether0}", NAME="${eth0_name}"`
|
|
|
|
`${extraRules1:+
|
|
|
|
$extraRules1}
|
|
|
|
'';
|
|
|
|
EOF
|
|
|
|
}
|
|
|
|
|
2020-12-07 09:09:48 +00:00
|
|
|
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
|
|
|
findESP() {
|
2023-11-18 02:47:34 +00:00
|
|
|
local esp
|
2021-11-16 07:00:26 +00:00
|
|
|
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
|
2023-11-18 02:47:34 +00:00
|
|
|
[[ $(readlink -f "$uuid") == "$esp" ]] && echo "$uuid" && return 0
|
2021-11-16 07:00:26 +00:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2020-12-07 09:09:48 +00:00
|
|
|
prepareEnv() {
|
2023-12-27 13:59:09 +00:00
|
|
|
if ! USER_PASS="$(base64 -d <<<"$ENCODED_PASSWORD")"; then
|
|
|
|
echo "Error decoding ENCODED_PASSWORD from Base64!"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
readonly USER_PASS
|
|
|
|
|
2023-11-18 02:47:34 +00:00
|
|
|
isEFI=0
|
2023-11-20 20:25:34 +00:00
|
|
|
[ -d /sys/firmware/efi ] && isEFI=1
|
2023-11-18 02:47:34 +00:00
|
|
|
|
|
|
|
if ((isEFI)); then
|
|
|
|
ESP="$(findESP)"
|
2021-11-16 07:00:26 +00:00
|
|
|
else
|
2023-11-18 02:47:34 +00:00
|
|
|
for GRUBDEV in /dev/vda /dev/sda /dev/nvme0n1; do
|
|
|
|
[[ -e $GRUBDEV ]] && break;
|
|
|
|
done
|
2021-11-16 07:00:26 +00:00
|
|
|
fi
|
2020-12-07 09:09:48 +00:00
|
|
|
|
|
|
|
# Retrieve root fs block device
|
|
|
|
# (get root mount) (get partition or logical volume)
|
2023-11-18 02:47:34 +00:00
|
|
|
ROOTFSDEV=$(mount | grep "on / type" | awk '{print $1;}')
|
|
|
|
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 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 xzcat || { echo "ERROR: Missing xzcat"; return 1; }
|
|
|
|
req awk || { echo "ERROR: Missing awk"; return 1; }
|
|
|
|
req cut || req df || { echo "ERROR: Missing coreutils (cut, df)"; return 1; }
|
2023-12-27 14:01:57 +00:00
|
|
|
req mkpasswd || { echo "ERROR: Missing mkpasswd"; return 1; }
|
|
|
|
req shuf || { echo "ERROR: Missing shuf"; return 1; }
|
2020-12-07 09:09:48 +00:00
|
|
|
}
|
|
|
|
|
2023-11-18 02:47:34 +00:00
|
|
|
# Download and execute the nix installer script.
|
2023-12-05 03:30:19 +00:00
|
|
|
|
2023-11-18 02:47:34 +00:00
|
|
|
installNix() {
|
2023-12-05 03:30:19 +00:00
|
|
|
# install multiuser (system-wide with nix-daemon) Nix in the current system
|
|
|
|
|
2023-11-18 02:47:34 +00:00
|
|
|
local nixReleaseBase='https://releases.nixos.org'
|
|
|
|
local installURL="${nixReleaseBase}/nix/nix-${NIX_VERSION}/install"
|
|
|
|
local shaURL="${installURL}.sha256"
|
|
|
|
local sha tmpNixInstall
|
|
|
|
|
|
|
|
# temporary destination for install script
|
|
|
|
tmpNixInstall="$(mktemp -t nix-install-XXXXXXXXXX)"
|
|
|
|
if [[ ! -f "${tmpNixInstall}" ]]; then
|
|
|
|
echo "Failed creating a temporary file for Nix install script!"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "Downloading install script from ${installURL}..."
|
2023-12-27 10:13:40 +00:00
|
|
|
if ! curl --fail "${installURL}" -o "${tmpNixInstall}" &>/dev/null; then
|
2023-11-18 02:47:34 +00:00
|
|
|
echo "Failure while downloading Nix install script!"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2023-12-27 10:13:40 +00:00
|
|
|
if ! sha="$(curl --fail "${shaURL}")"; then
|
2023-11-18 02:47:34 +00:00
|
|
|
echo "Failure while downloading Nix install script sha!"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "Validating Nix install script checksum..."
|
|
|
|
if ! echo "${sha} ${tmpNixInstall}" | sha256sum -c; then
|
|
|
|
echo "Checksum validation failed!"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "Running nix installer..."
|
2023-11-18 04:15:13 +00:00
|
|
|
if $SHELL "${tmpNixInstall}" \
|
|
|
|
--daemon --no-channel-add --daemon-user-count 4; then
|
2023-11-18 02:47:34 +00:00
|
|
|
echo "Nix is installed"
|
|
|
|
rm "${tmpNixInstall}"
|
|
|
|
else
|
|
|
|
echo "Nix installation script failed!"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2020-12-07 09:09:48 +00:00
|
|
|
infect() {
|
2023-12-27 09:10:35 +00:00
|
|
|
# this is needed solely for accepting the sp-module sub-flake
|
2023-11-22 18:26:58 +00:00
|
|
|
# see https://github.com/NixOS/nix/issues/3978#issuecomment-952418478
|
2023-12-28 00:32:31 +00:00
|
|
|
cd /etc/nixos || return 1
|
|
|
|
$NIX "${NIX_OPTS[@]}" flake lock ${LOCAL_FLAKE_DIR} \
|
|
|
|
--override-input sp-modules path:./sp-modules
|
2023-11-18 04:15:13 +00:00
|
|
|
|
2023-11-18 02:47:34 +00:00
|
|
|
echo "nix build the configuration flake..."
|
2023-12-27 09:10:35 +00:00
|
|
|
if ! $NIX "${NIX_OPTS[@]}" build \
|
2023-11-18 02:47:34 +00:00
|
|
|
--profile /nix/var/nix/profiles/system \
|
2023-12-27 10:05:09 +00:00
|
|
|
${LOCAL_FLAKE_DIR}/#nixosConfigurations."$NIXOS_CONFIG_ID".config.system.build.toplevel
|
2023-11-18 02:47:34 +00:00
|
|
|
then
|
|
|
|
echo "Failed!"
|
|
|
|
exit 1
|
|
|
|
fi
|
2020-12-07 09:09:48 +00:00
|
|
|
|
2023-11-18 02:47:34 +00:00
|
|
|
# Reify resolv.conf (???)
|
2020-12-07 09:09:48 +00:00
|
|
|
[[ -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
|
2023-11-21 13:28:21 +00:00
|
|
|
echo etc/nixos > /etc/NIXOS_LUSTRATE
|
|
|
|
echo etc/resolv.conf >> /etc/NIXOS_LUSTRATE
|
|
|
|
echo ${SECRETS_FILEPATH} >> /etc/NIXOS_LUSTRATE
|
2020-12-07 09:09:48 +00:00
|
|
|
|
|
|
|
rm -rf /boot.bak
|
2023-11-18 02:47:34 +00:00
|
|
|
((isEFI)) && umount "$ESP"
|
2020-12-07 09:09:48 +00:00
|
|
|
mv -v /boot /boot.bak
|
2023-11-18 02:47:34 +00:00
|
|
|
if ((isEFI)); then
|
2021-11-16 07:00:26 +00:00
|
|
|
mkdir /boot
|
2023-11-18 02:47:34 +00:00
|
|
|
mount "$ESP" /boot
|
2021-11-16 07:00:26 +00:00
|
|
|
find /boot -depth ! -path /boot -exec rm -rf {} +
|
|
|
|
fi
|
2023-11-18 02:47:34 +00:00
|
|
|
|
|
|
|
echo "make configuration boot by default..."
|
2023-11-18 13:43:36 +00:00
|
|
|
if ! /nix/var/nix/profiles/system/bin/switch-to-configuration boot; then
|
2023-11-18 02:47:34 +00:00
|
|
|
echo "Failed!"; exit 1
|
|
|
|
fi
|
2023-11-21 21:49:05 +00:00
|
|
|
|
|
|
|
# Remove nix installed by the "install" script.
|
2024-01-09 20:25:54 +00:00
|
|
|
rm -fv /nix/var/nix/profiles/default* /root/.nix-profile
|
|
|
|
rm -rv /nix/var/nix/profiles/per-user
|
2023-11-21 21:49:05 +00:00
|
|
|
/nix/var/nix/profiles/system/sw/bin/nix-collect-garbage
|
2020-12-07 09:09:48 +00:00
|
|
|
}
|
|
|
|
|
2023-12-27 14:01:57 +00:00
|
|
|
set -o errtrace
|
2023-11-18 02:47:34 +00:00
|
|
|
set -o nounset
|
2023-12-27 14:01:57 +00:00
|
|
|
set -o pipefail
|
2023-12-27 16:53:02 +00:00
|
|
|
set -o xtrace
|
2023-12-11 09:40:42 +00:00
|
|
|
shopt -s inherit_errexit
|
2023-12-27 14:01:57 +00:00
|
|
|
trap 'echo ${LINENO}: "$BASH_COMMAND"; exit 1' ERR
|
2020-12-07 09:09:48 +00:00
|
|
|
|
2023-12-11 11:55:46 +00:00
|
|
|
# digitalocean requires detailed network config to be generated
|
2023-12-28 16:07:59 +00:00
|
|
|
[ "$PROVIDER" == "DIGITALOCEAN" ] && DoNetConf="y"
|
2023-12-11 11:55:46 +00:00
|
|
|
|
2021-01-05 13:49:35 +00:00
|
|
|
apt update
|
2023-12-11 09:40:42 +00:00
|
|
|
apt install -y git tar curl whois jq
|
2023-12-05 03:30:19 +00:00
|
|
|
|
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!
|
2023-12-05 03:30:19 +00:00
|
|
|
installNix
|
2023-11-18 02:47:34 +00:00
|
|
|
setupConf
|
2020-12-07 09:09:48 +00:00
|
|
|
infect
|
|
|
|
removeSwap
|
|
|
|
|
2023-11-18 14:21:34 +00:00
|
|
|
if [[ -z "${NO_REBOOT+x}" ]]; then
|
2020-12-07 09:09:48 +00:00
|
|
|
reboot
|
2022-10-10 14:40:46 +00:00
|
|
|
fi
|