Skip to content

Instantly share code, notes, and snippets.

@gilligan
Created June 6, 2018 13:07
Show Gist options
  • Save gilligan/84ccd08d7a032f8d27a0d9530f31e24a to your computer and use it in GitHub Desktop.
Save gilligan/84ccd08d7a032f8d27a0d9530f31e24a to your computer and use it in GitHub Desktop.
#!/bin/sh
set -e
echo "This script will:"
echo ""
echo "- retrieve and start lnl7/nix-docker container"
echo "- modify ~/.ssh/config ~root/.ssh/config /etc/nix/machines /etc/nix/nix.conf"
echo "- restart nix daemon"
echo "- test the remote builder"
echo ""
echo "Press Ctrl-C to cancel, any other key to continue."
read
SSH_KEY_FILE=$(mktemp)
SSH_ROOT_CONFIG=$(mktemp)
SSH_USER_CONFIG=$(mktemp)
NIX_MACHINES_CONFIG=$(mktemp)
NIX_CONFIG=$(mktemp)
cat >> $SSH_ROOT_CONFIG <<EOF
Host nix-docker
User root
HostName 127.0.0.1
Port 3022
IdentityFile /etc/nix/docker_rsa
EOF
cat >> $SSH_USER_CONFIG <<EOF
Host nix-docker
User root
HostName 127.0.0.1
Port 3022
IdentityFile ~/.ssh/docker_rsa
EOF
cat >> $NIX_MACHINES_CONFIG <<EOF
nix-docker x86_64-linux /etc/nix/docker_rsa 4 1
EOF
cat >> $NIX_CONFIG <<EOF
builders = @/etc/nix/machines
EOF
# -----------------------------------------------
echo "-- retrieving the nix-docker ssh key ..."
# -----------------------------------------------
curl --silent https://raw.githubusercontent.com/LnL7/nix-docker/master/ssh/insecure_rsa > $SSH_KEY_FILE
chmod 600 $SSH_KEY_FILE
echo "-- copying ssh key to /etc/nix/docker_rsa (requires sudo) ..."
sudo cp $SSH_KEY_FILE /etc/nix/docker_rsa
echo "-- copying ssh key to ~/.ssh/ ..."
cp $SSH_KEY_FILE ~/.ssh/docker_rsa
# -----------------------------------------------
echo "-- adding ssh config to ~/.ssh/config ..."
# -----------------------------------------------
mkdir -p ~/.ssh/
if ! grep -q "nix-docker" ~/.ssh/config ; then
cat $SSH_USER_CONFIG >> ~/.ssh/config
fi
# -----------------------------------------------
echo "-- adding ssh config to /var/root/.ssh/config ..."
# -----------------------------------------------
sudo mkdir -p /var/root/.ssh
sudo touch /var/root/.ssh/config
if ! sudo sh -c "grep -q 'nix-docker' /var/root/.ssh/config" ; then
sudo sh -c "cat $SSH_ROOT_CONFIG >> /var/root/.ssh/config"
fi
# -----------------------------------------------
echo "-- adding remote builder to nix config ..."
# -----------------------------------------------
sudo touch /etc/nix/machines
if ! grep -q "nix-docker" /etc/nix/machines ; then
sudo sh -c "cat $NIX_MACHINES_CONFIG >> /etc/nix/machines"
fi
# -----------------------------------------------
echo "-- configuring remote builders ..."
# -----------------------------------------------
if ! grep -q "builders = @/etc/nix/machines" /etc/nix/nix.conf ; then
sudo sh -c "cat $NIX_CONFIG >> /etc/nix/nix.conf"
fi
# -----------------------------------------------
echo "-- restarting nix daemon ..."
# -----------------------------------------------
sudo launchctl unload /Library/LaunchDaemons/org.nixos.nix-daemon.plist
sudo launchctl load /Library/LaunchDaemons/org.nixos.nix-daemon.plist
# -----------------------------------------------
echo "-- starting lnl7/nix-docker container ..."
# -----------------------------------------------
if ! docker ps | grep "nix-docker" >/dev/null ; then
docker run --restart always --name nix-docker -d -p 3022:22 lnl7/nix:ssh
fi
# -----------------------------------------------
echo "-- testing ssh connection ..."
# -----------------------------------------------
ssh nix-docker -C uname
sudo sh -c "ssh nix-docker -C uname"
# -----------------------------------------------
echo "-- testing builder ..."
# -----------------------------------------------
nix build '(with import <nixpkgs> {}; runCommand "unameNixDocker" {} "uname -a > $out")' --builders ssh://nix-docker --option system x86_64-linux
cat result
echo ""
echo ""
echo "All good, your builder is up and working!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment