nixos-infect/nixos-infect

245 lines
7.8 KiB
Text
Raw Normal View History

2016-03-14 03:21:33 +03:00
#! /usr/bin/env bash
2016-03-14 12:59:18 +03:00
# More info at: https://github.com/elitak/nixos-infect
2016-03-14 12:59:18 +03:00
set -e -o pipefail
2016-03-14 08:09:30 +03:00
makeConf() {
# Skip everything if main config already present
[[ -e /etc/nixos/configuration.nix ]] && return 0
2016-04-09 22:39:35 +03:00
# NB <<"EOF" quotes / $ ` in heredocs, <<EOF does not
2016-03-14 12:59:18 +03:00
mkdir -p /etc/nixos
local IFS=$'\n'; keys=($(grep -vE '^[[:space:]]*(#|$)' /root/.ssh/authorized_keys))
2016-03-14 12:59:18 +03:00
cat > /etc/nixos/configuration.nix << EOF
2016-03-14 23:28:14 +03:00
{ ... }: {
2016-03-14 08:09:30 +03:00
imports = [
./hardware-configuration.nix
2016-03-14 23:28:14 +03:00
./networking.nix # generated at runtime by nixos-infect
$NIXOS_IMPORT
2016-03-14 08:09:30 +03:00
];
boot.cleanTmpDir = true;
2016-03-14 12:59:18 +03:00
networking.hostName = "$(hostname)";
2016-03-14 23:28:14 +03:00
networking.firewall.allowPing = true;
services.openssh.enable = true;
users.users.root.openssh.authorizedKeys.keys = [$(for key in "${keys[@]}"; do echo -n "
2016-03-14 23:28:14 +03:00
\"$key\""; done)
];
2016-03-14 08:09:30 +03:00
}
EOF
2017-02-17 05:51:05 +03:00
# If you rerun this later, be sure to prune the filesSystems attr
2016-03-14 12:59:18 +03:00
cat > /etc/nixos/hardware-configuration.nix << EOF
2016-06-22 22:45:38 +03:00
{ ... }:
{
2016-03-14 08:09:30 +03:00
imports = [ <nixpkgs/nixos/modules/profiles/qemu-guest.nix> ];
boot.loader.grub.device = "/dev/$disk";
fileSystems."/" = { device = "/dev/${disk}1"; fsType = "ext4"; };
2016-03-14 08:09:30 +03:00
}
2016-04-09 22:39:35 +03:00
EOF
2017-04-10 18:50:09 +03:00
2017-02-14 11:37:47 +03:00
# XXX It'd be better if we used procfs for all this...
2016-04-09 22:39:35 +03:00
local IFS=$'\n'
2017-04-10 18:50:09 +03:00
eth0_name=$(ip address show | grep '^2:' | awk -F': ' '{print $2}')
2017-04-15 19:55:08 +03:00
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|')
ether0=$(ip address show dev "$eth0_name" | grep link/ether | sed -r 's|.*link/ether ([0-9a-f:]+) .*|\1|')
2017-04-10 18:50:09 +03:00
eth1_name=$(ip address show | grep '^3:' | awk -F': ' '{print $2}')||true
if [ -n "$eth1_name" ];then
2017-04-15 19:55:08 +03:00
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|')
gateway6=$(ip -6 route show dev "$eth1_name" | grep default | sed -r 's|default via ([0-9a-f:]+).*|\1|' || true)
2017-04-10 18:50:09 +03:00
interfaces1=<< EOF
$eth1_name = {
ip4 = [$(for a in "${eth1_ip4s[@]}"; do echo -n "
2017-04-10 18:50:09 +03:00
$a"; done)
];
ip6 = [$(for a in "${eth1_ip6s[@]}"; do echo -n "
2017-04-10 18:50:09 +03:00
$a"; done)
];
EOF
extraRules1="ATTR{address}==\"${ether1}\", NAME=\"eth0\""
else
interfaces1=""
extraRules1=""
fi
2016-04-09 22:39:35 +03:00
nameservers=($(grep ^nameserver /etc/resolv.conf | cut -f2 -d' '))
cat > /etc/nixos/networking.nix << EOF
{ ... }: {
# This file was populated at runtime with the networking
# details gathered from the active system.
networking = {
nameservers = [$(for a in "${nameservers[@]}"; do echo -n "
2016-04-09 22:39:35 +03:00
\"$a\""; done)
];
defaultGateway = "${gateway}";
defaultGateway6 = "${gateway6}";
interfaces = {
2017-04-10 18:50:09 +03:00
$eth0_name = {
ip4 = [$(for a in "${eth0_ip4s[@]}"; do echo -n "
2016-04-09 22:39:35 +03:00
$a"; done)
];
ip6 = [$(for a in "${eth0_ip6s[@]}"; do echo -n "
2016-04-09 22:39:35 +03:00
$a"; done)
];
};
2017-04-10 18:50:09 +03:00
$interfaces1
2016-04-09 22:39:35 +03:00
};
};
services.udev.extraRules = ''
2017-02-17 05:51:05 +03:00
ATTR{address}=="${ether0}", NAME="eth0"
2017-04-10 18:50:09 +03:00
$extraRules1
2016-04-09 22:39:35 +03:00
'';
}
2016-03-14 08:09:30 +03:00
EOF
2016-03-14 12:59:18 +03:00
#! /usr/bin/env bash
# NB put your semi-sensitive (not posted to github) configuration in a separate
# file and include it via this customConfig() function. e.g.:
# customConfig() {
# cat > /etc/nixos/custom.nix << EOF
# { config, lib, pkgs, ... }: {
# }
# EOF
# }
#
# then you can add the files in configuration.nix's imports above and run something like:
# cat customConfig nixos-infect | root@targethost bash
if [[ "$(type -t customConfig)" == "function" ]]; then customConfig; fi
2016-03-14 08:09:30 +03: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() {
for swapFile in /tmp/nixos-infect.*.swp
do
swapoff -v "$swapFile"
rm -vf "$swapFile"
done
2016-03-14 08:09:30 +03:00
}
prepareEnv() {
# $disk is used in makeConf()
for disk in vda sda; do [[ -e /dev/$disk ]] && break; done
# DigitalOcean doesn't seem to set USER while running user data
export USER="root"
export HOME="/root"
2016-03-14 08:09:30 +03:00
# Use adapted wget if curl is missing
which curl || { \
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; }
# Nix installer tries to use sudo regardless of whether we're already uid 0
#which sudo || { sudo() { eval "$@"; }; export -f sudo; }
2017-04-15 19:57:55 +03:00
# shellcheck disable=SC2174
mkdir -p -m 0755 /nix
}
req() {
type "$1" > /dev/null 2>&1 || which "$1" > /dev/null 2>&1
}
2016-03-14 08:09:30 +03:00
checkEnv() {
# Perform some easy fixups before checking
which dnf && dnf install -y perl-Digest-SHA # Fedora 24
2017-04-10 18:50:09 +03:00
which bzcat || (which yum && yum install -y bzip2) \
|| (which apt-get && apt-get install bzip2) \
|| true
[[ "$(whoami)" == "root" ]] || { echo "ERROR: Must run as root"; return 1; }
req curl || req wget || { echo "ERROR: Missing both curl and wget"; return 1; }
req bzcat || { echo "ERROR: Missing bzcat"; 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 || { echo "ERROR: Missing cut"; return 1; }
}
2016-03-14 08:09:30 +03:00
infect() {
# Add nix build users
# FIXME run only if necessary, rather than defaulting true
2017-02-17 05:51:05 +03:00
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
2017-02-17 05:51:05 +03:00
# 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
2016-03-14 08:09:30 +03:00
curl https://nixos.org/nix/install | $SHELL
2016-09-07 21:36:01 +03:00
2017-04-15 19:57:55 +03:00
# shellcheck disable=SC1090
source ~/.nix-profile/etc/profile.d/nix.sh
2016-03-14 08:09:30 +03:00
2017-04-02 11:51:28 +03:00
[[ -z "$NIX_CHANNEL" ]] && NIX_CHANNEL="nixos-17.03"
nix-channel --remove nixpkgs
nix-channel --add "https://nixos.org/channels/$NIX_CHANNEL" nixos
nix-channel --update
2016-03-14 08:09:30 +03:00
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
mv -v /boot /boot.bak
/nix/var/nix/profiles/system/bin/switch-to-configuration boot
}
prepareEnv
checkEnv
makeConf
makeSwap # smallest (512MB) droplet needs extra memory!
infect
removeSwap
reboot