diff --git a/nix-docker b/nix-docker new file mode 100755 index 0000000..54c4536 --- /dev/null +++ b/nix-docker @@ -0,0 +1,217 @@ +#!/usr/bin/env bash + +# Sets up environment to proxy non-x86_64-darwin builds to a Docker image using Nix distributed builds. +# See: https://nixos.org/nix/manual/#chap-distributed-builds +# +# Requires Docker to be installed +# - For Mac users the easiest way to get Docker working is through Docker for Mac. Native Docker can have +# issues interacting with OS X. See: https://www.docker.com/docker-mac + +working_dir_name=".nix-docker" + +log () { + echo ">>> ${1}" +} + +usage () { + cat < [] + -h | --help print this message + +Commands: + +init Initial bootstrap for using Docker container as a Nix remote builder + -d | --dir specify the directory where configuration files will be placed (default: \${HOME}/${working_dir_name}) + -n | --name Docker container name (default: nix-docker) + -k | --key path to private SSH key to use to talk to container - assumes the public key is the same but with '.pub' appended (default: ./ssh/insecure_rsa) + -p | --port port to connect to Docker container (default: 3022) + +start Run Docker container and setup environment variables + -d | --dir specify the directory where configuration files are located (default: \${HOME}/${working_dir_name}) + -n | --name Docker container name (default: nix-docker) + +EOM +} + +remove_instructions () { + cat < /dev/null; then + log "Adding an entry to ${ssh_config} for ${docker_name}." + cat >> "${ssh_config}" < "${remote_sys_conf}" <>> 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"