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"
-I nixpkgs=$HOME/.nix-defexpr/channels/nixos \ nix-channel --remove nixpkgs
-f '<nixpkgs/nixos>' \ nix-channel --add "https://nixos.org/channels/$NIX_CHANNEL" nixos
-p /nix/var/nix/profiles/system \ nix-channel --update
-A system
# Remove nix installed with curl | bash export NIXOS_CONFIG=/etc/nixos/configuration.nix
rm -fv /nix/var/nix/profiles/default*
/nix/var/nix/profiles/system/sw/bin/nix-collect-garbage
# Follow the symlinks nix-env --set \
[ -L /etc/resolv.conf ] && mv -v /etc/resolv.conf /etc/resolv.conf.lnk && cat /etc/resolv.conf.lnk > /etc/resolv.conf -I nixpkgs=$HOME/.nix-defexpr/channels/nixos \
-f '<nixpkgs/nixos>' \
-p /nix/var/nix/profiles/system \
-A system
# Staging for the Nix coup d'état # Remove nix installed with curl | bash
touch /etc/NIXOS rm -fv /nix/var/nix/profiles/default*
cat > /etc/NIXOS_LUSTRATE << EOF /nix/var/nix/profiles/system/sw/bin/nix-collect-garbage
etc/nixos
etc/resolv.conf
root/.nix-defexpr/channels
EOF
rm -rf /boot.bak && mv -v /boot /boot.bak && # Follow the symlinks
/nix/var/nix/profiles/system/bin/switch-to-configuration boot [ -L /etc/resolv.conf ] && mv -v /etc/resolv.conf /etc/resolv.conf.lnk && cat /etc/resolv.conf.lnk > /etc/resolv.conf
reboot # Staging for the Nix coup d'état
touch /etc/NIXOS
cat > /etc/NIXOS_LUSTRATE << EOF
etc/nixos
etc/resolv.conf
root/.nix-defexpr/channels
EOF
rm -rf /boot.bak && mv -v /boot /boot.bak &&
/nix/var/nix/profiles/system/bin/switch-to-configuration boot
}
prepareEnv
checkEnv && infect && reboot