prepareEnv,checkEnv refactor; other tiny fixes

This commit is contained in:
Eric Litak 2017-02-13 17:30:42 -08:00
parent 7c2f3e8e2d
commit cad9b43b69

View file

@ -1,6 +1,6 @@
#! /usr/bin/env bash #! /usr/bin/env bash
# Use Digital Ocean Droplet image: # These are the only supported Digital Ocean images:
# #
# Fedora 24 x64 # Fedora 24 x64
# Ubuntu 16.04 x64 # Ubuntu 16.04 x64
@ -45,10 +45,7 @@
# simply didn't work for me! (old system was being because grub wasnt properly # simply didn't work for me! (old system was being because grub wasnt properly
# reinstalled) # reinstalled)
set -ex set -ex -o pipefail
export disk=$( (>/dev/null ls -l /dev/vda && echo vda) \
|| (>/dev/null ls -l /dev/sda && echo sda) )
makeConf() { makeConf() {
# NB <<"EOF" quotes / $ ` in heredocs, <<EOF does not # NB <<"EOF" quotes / $ ` in heredocs, <<EOF does not
@ -142,6 +139,7 @@ EOF
} }
makeSwap() { makeSwap() {
# TODO check currently available swapspace first
swapFile=`mktemp` swapFile=`mktemp`
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
@ -149,52 +147,71 @@ makeSwap() {
swapon $swapFile swapon $swapFile
} }
makeConf prepareEnv() {
makeSwap # smallest (512MB) droplet needs extra memory! which dnf && dnf install -y perl-Digest-SHA # Fedora 24
which bzcat || (which yum && yum install -y bzip2) # CentOS
which dnf && dnf install -y perl-Digest-SHA # Fedora 24 # $disk is used in makeConf()
which bzcat || (which yum && yum install -y bzip2) # CentOS disk=$( (test -e /dev/vda && echo vda)
|| (test -e /dev/sda && echo sda) )
# 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"
groupadd -r nixbld -g 30000 || true # FIXME run only if necessary
seq 1 10 | xargs -I{} useradd -c "Nix build user {}" -d /var/empty -g nixbld -G nixbld -M -N -r -s `which nologin` nixbld{} || true groupadd -r nixbld -g 30000 || true
seq 1 10 | xargs -I{} useradd -c "Nix build user {}" -d /var/empty -g nixbld -G nixbld -M -N -r -s `which nologin` nixbld{} || true
}
curl https://nixos.org/nix/install | sh checkEnv() {
# TODO: use wget -O- if available instead of curl. This involves patching the
# /nix/install script to not check for curl and use `wget -O` instead of
# `curl -L # -o`
( which curl || echo "ERROR: Missing curl" ) && \
( which bzcat || echo "ERROR: Missing bzcat" ) && \
( which perl || echo "ERROR: Missing perl" )
}
source ~/.nix-profile/etc/profile.d/nix.sh infect() {
makeConf
makeSwap # smallest (512MB) droplet needs extra memory!
[ -z "$NIX_CHANNEL"] && NIX_CHANNEL="nixos-16.09" curl https://nixos.org/nix/install | sh
nix-channel --remove nixpkgs
nix-channel --add "https://nixos.org/channels/$NIX_CHANNEL" nixos
nix-channel --update
export NIXOS_CONFIG=/etc/nixos/configuration.nix source ~/.nix-profile/etc/profile.d/nix.sh
nix-env --set \ [ -z "$NIX_CHANNEL"] && NIX_CHANNEL="nixos-16.09"
nix-channel --remove nixpkgs
nix-channel --add "https://nixos.org/channels/$NIX_CHANNEL" nixos
nix-channel --update
export NIXOS_CONFIG=/etc/nixos/configuration.nix
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
# Follow the symlinks # Follow the symlinks
[ -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
# Staging for the Nix coup d'état # Staging for the Nix coup d'état
touch /etc/NIXOS touch /etc/NIXOS
cat > /etc/NIXOS_LUSTRATE << EOF cat > /etc/NIXOS_LUSTRATE << EOF
etc/nixos etc/nixos
etc/resolv.conf etc/resolv.conf
root/.nix-defexpr/channels root/.nix-defexpr/channels
EOF EOF
rm -rf /boot.bak && mv -v /boot /boot.bak && rm -rf /boot.bak && mv -v /boot /boot.bak &&
/nix/var/nix/profiles/system/bin/switch-to-configuration boot /nix/var/nix/profiles/system/bin/switch-to-configuration boot
}
reboot prepareEnv
checkEnv && infect && reboot