#!/usr/bin/env bash # Requires: curl, docker, grep working_dir_name=".nix-docker-build-slave" working_dir="$HOME/$working_dir_name" mkdir -p "$working_dir" ssh_id_file="$working_dir/insecure_rsa" remote_sys_conf="$working_dir/remote-systems.conf" ssh_config="$HOME/.ssh/config" docker_machine_name="nix-docker-build-slave" # -- Display info and troubleshooting tips -- echo "## Use Docker Container as Nix Build Slave" echo "##" echo "## Usage: source $0" echo "##" echo "## Note: if you experience issues, you can clean up the build slave artifacts and start over by:" echo "## 1. rm -r ~/$working_dir_name" echo "## 2. Delete entry in ~/.ssh/config for" echo "## Host \"$docker_machine_name\"" echo "## 3. Delete the docker container named" echo "## $docker_machine_name" echo # -- Download SSH credentials for docker container -- echo ">>> Downloading SSH credentials for the docker container" rm -f "$ssh_id_file" "$ssh_id_file.pub" curl -fsSL https://raw.githubusercontent.com/LnL7/nix-docker/master/ssh/insecure_rsa -o "$ssh_id_file" curl -fsSL https://raw.githubusercontent.com/LnL7/nix-docker/master/ssh/insecure_rsa.pub -o "$ssh_id_file.pub" chmod 600 "$ssh_id_file" # -- Set up SSH configuration -- [ -f "$ssh_config" ] || touch "$ssh_config" if ! grep "$docker_machine_name" "$HOME/.ssh/config" > /dev/null; then echo ">>> Adding an entry to $ssh_config for $docker_machine_name" cat >> "$ssh_config" <>> SSH configuration already contains an entry for $docker_machine_name in $ssh_config" fi # -- Start docker container -- echo ">>> Starting docker container: $docker_machine_name" echo " (This may fail if the container was already created.)" docker run --restart always --name "$docker_machine_name" -d -p 3022:22 lnl7/nix:ssh # -- Write remote systems configuration -- echo ">>> Writing remote systems configuration to $remote_sys_conf" rm -f "$remote_sys_conf" cat > "$remote_sys_conf" <>> Running SSH test" ssh "$docker_machine_name" echo "SSH connection is working." || echo "SSH connection failed." # -- Export environment -- echo ">>> Setting \$NIX_REMOTE_SYSTEMS to use $remote_sys_conf" export NIX_REMOTE_SYSTEMS="$remote_sys_conf"