nixos-infect/nixos-infect

171 lines
6.1 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
# WARNING NB This script wipes out the targeted host's root filesystem when it
# runs to completion. Any errors halt execution. set -x is used to help debug,
# as often a failed run leaves the system in an inconsistent state, requiring a
# rebuild (in DigitalOcean panel: Droplet Settings -> "Destroy" -> "Rebuild
# from original").
#
# TO USE:
# - Add any custom config you want (see notes below)
# - Deploy a Debian 8.3 x64 droplet (enable ipv6; add your ssh key)
# - cat customConfig.optional nixos-infect | ssh root@targethost bash
#
# This was last tested with the DigitalOcean Debian 8.3 x64 image. Different
# versions as well as the Ubuntu images should work as well, but then, there's
# not much point in selecting something different if you intend to wipe out the
# fs as this does. You may need to make minor modifications to use in other
# templates, but basically all that will ever need tweaking should be:
# /etc/nixos/{,hardware-}configuration.nix, inline in this file
# /etc/nixso/networking.nix, generated at runtime (no ipv6? different number of adapters?)
#
# Motivation: nixos-assimilate should supplant this script entirely, if it's
# ever completed. nixos-in-place was quite broken when I tried it, and also
# took a pretty janky approach that was substantially more complex than this
# (although it supported more platforms): it didn't install to root (/nixos
# instead), left dregs of the old filesystem (almost always unnecessary since
# starting from a fresh deployment), and most importantly, simply didn't work for
# me! (old system was being because grub wasnt properly reinstalled)
set -ex
2016-03-14 08:09:30 +03:00
makeConf() {
2016-03-14 12:59:18 +03:00
# NB remember to escape / $ ` in heredocs!
# TODO use appended archive or some curl-able tarball?
mkdir -p /etc/nixos
cat > /etc/nixos/networking.nix << EOF
2016-03-14 08:09:30 +03:00
# This file will be populated at runtime with the
# networking details gathered from the active system.
{...}:{}
EOF
2016-03-14 12:59:18 +03:00
cat > /etc/nixos/configuration.nix << EOF
2016-03-14 08:09:30 +03:00
{ config, pkgs, ... }: {
imports = [
./hardware-configuration.nix
./networking.nix
];
time.timeZone = "America/Los_Angeles";
boot.cleanTmpDir = true;
services.tlsdated.enable = true;
2016-03-14 12:59:18 +03:00
services.openssh.enable = true;
2016-03-14 08:09:30 +03:00
networking.firewall.allowPing = true;
2016-03-14 12:59:18 +03:00
networking.hostName = "$(hostname)";
users.users.root.openssh.authorizedKeys.keys = [ ''
$(cat /root/.ssh/authorized_keys | head -n1)
'' ];
2016-03-14 08:09:30 +03:00
}
EOF
2016-03-14 12:59:18 +03:00
# (nixos-generate-config will add qemu-user and bind-mounts, so avoid)
cat > /etc/nixos/hardware-configuration.nix << EOF
2016-03-14 08:09:30 +03:00
{ config, lib, pkgs, ... }: {
imports = [ <nixpkgs/nixos/modules/profiles/qemu-guest.nix> ];
boot.loader.grub.devices = [ "/dev/vda" ];
fileSystems."/" = { device = "/dev/vda1"; fsType = "ext4"; };
}
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() {
if [[ ! -e /swap ]]; then
dd if=/dev/zero of=/swap bs=1M count=$((1024*2))
chmod 0600 /swap
mkswap /swap
swapon /swap
fi
}
makeConf
2016-03-14 12:59:18 +03:00
makeSwap # smallest (512MB) droplet needs extra memory!
2016-03-14 08:09:30 +03:00
apt-get install -y curl sudo rsync
groupadd -r nixbld
seq 1 10 | xargs -I{} useradd -c "Nix build user {}" -d /var/empty -g nixbld -G nixbld -M -N -r -s `which nologin` nixbld{}
curl https://nixos.org/nix/install | sh
source ~/.nix-profile/etc/profile.d/nix.sh
nix-channel --add https://nixos.org/channels/nixos-unstable nixos
nix-channel --update
newRootImg=`mktemp`
newRootMount=`mktemp -d`
oldRootMount=`mktemp -d`
export NIXOS_CONFIG=/etc/nixos/configuration.nix
nix-env -i -f /nix/var/nix/profiles/per-user/root/channels/nixpkgs/nixos \
-A config.system.build.nixos-install \
-A config.system.build.nixos-option \
-A config.system.build.nixos-generate-config
# XXX GOTCHA NB bindmount causes /bin/bash permission BUG on many
# versions (nix 1.10-1.11, nixpkgs 15-16), so we must use loopback image instead.
2016-03-14 08:09:30 +03:00
dd if=/dev/zero of=$newRootImg bs=1M count=$((1024*2))
mkfs.ext4 $newRootImg
mount $newRootImg $newRootMount
rsync -aR /./etc/nixos $newRootMount
nixos-install --root $newRootMount
swapoff /swap
2016-03-14 08:09:30 +03:00
mount -B / $oldRootMount
rsync -a --delete --exclude=$(dirname $newRootMount) $newRootMount/ $oldRootMount
2016-03-14 12:59:18 +03:00
# restore access to commands
2016-03-14 08:09:30 +03:00
/nix/var/nix/profiles/system/activate
source /nix/var/nix/profiles/system/etc/profile
# XXX temporary fix for name resolution
echo nameserver 8.8.4.4 > /etc/resolv.conf
ip4s=$(ip address show dev eth0 | grep 'inet ' | sed -r 's|.*inet ([0-9.]+)/([0-9]+).*|{ address="\1"; prefixLength=\2; }|')
ip6s=$(ip address show dev eth0 | grep 'inet6 .*global' | sed -r 's|.*inet6 ([0-9a-f:]+)/([0-9]+).*|{ address="\1"; prefixLength=\2; }|')
gateway=$(ip route show dev eth0 | grep default | sed -r 's|default via ([0-9.]+).*|\1|')
gateway6=$(ip -6 route show dev eth0 | grep default | sed -r 's|default via ([0-9a-f:]+).*|\1|')
ether0=$(ip address show dev eth0 | grep link/ether | sed -r 's|.*link/ether ([0-9a-f:]+) .*|\1|')
ether1=$(ip address show dev eth1 | grep link/ether | sed -r 's|.*link/ether ([0-9a-f:]+) .*|\1|')
2016-03-14 12:59:18 +03:00
cat > /etc/nixos/networking.nix << EOF
2016-03-14 08:09:30 +03:00
{ config, pkgs, ... }: {
networking = {
nameservers = [ "8.8.4.4" ];
defaultGateway = "$gateway";
defaultGateway6 = "$gateway6";
interfaces = {
eth0 = {
ip4 = [ $ip4s ];
ip6 = [ $ip6s ];
};
eth1.useDHCP = false;
};
};
2016-03-14 12:59:18 +03:00
services.udev.extraRules = ''
2016-03-14 08:09:30 +03:00
KERNEL=="eth*", ATTR{address}=="$ether0", NAME="eth0"
KERNEL=="eth*", ATTR{address}=="$ether1", NAME="eth1"
'';
}
EOF
# grub/initrd was probably installed incorrectly (using false root device), so we need a final rebuild
nixos-rebuild boot --install-grub
sync
2016-03-14 12:59:18 +03:00
echo "You may now Ctrl-C or otherwise terminate this process."
2016-03-14 08:09:30 +03:00
reboot -f