|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Sets up environment to proxy non-x86_64-darwin builds to a Docker image using Nix distributed builds. |
| 4 | +# See: https://nixos.org/nix/manual/#chap-distributed-builds |
| 5 | +# |
| 6 | +# Requires Docker to be installed |
| 7 | +# - For Mac users the easiest way to get Docker working is through Docker for Mac. Native Docker can have |
| 8 | +# issues interacting with OS X. See: https://www.docker.com/docker-mac |
| 9 | + |
| 10 | +working_dir_name=".nix-docker" |
| 11 | + |
| 12 | +log () { |
| 13 | + echo ">>> ${1}" |
| 14 | +} |
| 15 | + |
| 16 | +usage () { |
| 17 | + cat <<EOM |
| 18 | +nix-docker: Start up a Docker container to use as a Nix remote builder |
| 19 | +
|
| 20 | +Usage: source nix-docker [-h | --help] <command> [<args>] |
| 21 | + -h | --help print this message |
| 22 | +
|
| 23 | +Commands: |
| 24 | +
|
| 25 | +init Initial bootstrap for using Docker container as a Nix remote builder |
| 26 | + -d | --dir specify the directory where configuration files will be placed (default: \${HOME}/${working_dir_name}) |
| 27 | + -n | --name Docker container name (default: nix-docker) |
| 28 | + -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) |
| 29 | + -p | --port port to connect to Docker container (default: 3022) |
| 30 | +
|
| 31 | +run Run Docker container and setup environment variables |
| 32 | + -d | --dir specify the directory where configuration files are located (default: \${HOME}/${working_dir_name}) |
| 33 | + -n | --name Docker container name (default: nix-docker) |
| 34 | +
|
| 35 | +EOM |
| 36 | +} |
| 37 | + |
| 38 | +remove_instructions () { |
| 39 | + cat <<EOM |
| 40 | +To undo everything: |
| 41 | +- Remove the ${docker_name} entry in ${ssh_config} |
| 42 | +- Remove ${working_dir} |
| 43 | +- Stop and remove Docker containers |
| 44 | +
|
| 45 | +Example: |
| 46 | +
|
| 47 | +$ docker stop ${docker_name} |
| 48 | +$ docker rm ${docker_name} |
| 49 | +$ docker rmi lnl7/nix:ssh |
| 50 | +
|
| 51 | +EOM |
| 52 | +} |
| 53 | + |
| 54 | +finish_message () { |
| 55 | + cat <<EOM |
| 56 | +Setup complete. |
| 57 | +EOM |
| 58 | +} |
| 59 | + |
| 60 | +export_env () { |
| 61 | + log "Setting \$NIX_REMOTE_SYSTEMS to ${remote_sys_conf}" |
| 62 | + log "Old value: ${NIX_REMOTE_SYSTEMS}" |
| 63 | + export NIX_REMOTE_SYSTEMS="${remote_sys_conf}" |
| 64 | + echo |
| 65 | + |
| 66 | + nix_current_load="/tmp/nix/current-load" |
| 67 | + log "Setting \$NIX_CURRENT_LOAD to ${nix_current_load}" |
| 68 | + log "Old value: ${NIX_CURRENT_LOAD}" |
| 69 | + mkdir -p "${nix_current_load}" |
| 70 | + chmod a+rwX "${nix_current_load}" |
| 71 | + export NIX_CURRENT_LOAD="/tmp/nix/current-load" |
| 72 | + echo |
| 73 | + |
| 74 | + build_remote="${HOME}/.nix-profile/libexec/nix/build-remote.pl" |
| 75 | + log "Setting \$NIX_BUILD_HOOK to ${build_remote}" |
| 76 | + log "Old value: ${NIX_BUILD_HOOK}" |
| 77 | + export NIX_BUILD_HOOK="${build_remote}" |
| 78 | + echo |
| 79 | +} |
| 80 | + |
| 81 | +do_init () { |
| 82 | + ssh_config="${HOME}/.ssh/config" |
| 83 | + |
| 84 | + # Default values |
| 85 | + working_dir="${HOME}/${working_dir_name}" |
| 86 | + docker_name="nix-docker" |
| 87 | + key_file="./ssh/insecure_rsa" |
| 88 | + port_num="3022" |
| 89 | + |
| 90 | + while [[ $# -gt 0 ]]; do |
| 91 | + case "${1}" in |
| 92 | + -d|--dir) |
| 93 | + working_dir=${2} |
| 94 | + shift 2 |
| 95 | + ;; |
| 96 | + -n|--name) |
| 97 | + docker_name=${2} |
| 98 | + shift 2 |
| 99 | + ;; |
| 100 | + -k|--key) |
| 101 | + key_file=${2} |
| 102 | + shift 2 |
| 103 | + ;; |
| 104 | + -p|--port) |
| 105 | + port_num=${2} |
| 106 | + shift 2 |
| 107 | + ;; |
| 108 | + esac |
| 109 | + done |
| 110 | + |
| 111 | + # Set up environment |
| 112 | + mkdir -p "${working_dir}" |
| 113 | + |
| 114 | + # Copy SSH keys to working directory |
| 115 | + cp "${key_file}" "${working_dir}" |
| 116 | + cp "${key_file}.pub" "${working_dir}" |
| 117 | + |
| 118 | + key_file_name=$(basename "${key_file}") |
| 119 | + ssh_id_file="${working_dir}/${key_file_name}" |
| 120 | + chmod 600 "${ssh_id_file}" |
| 121 | + |
| 122 | + # Check to see if ~/.ssh/config exists - if not, create it |
| 123 | + [[ -f "${ssh_config}" ]] || touch "${ssh_config}" |
| 124 | + |
| 125 | + # Check if SSH config file has an entry for our Docker machine - if not, add it |
| 126 | + if ! grep "${docker_name}\$" "${ssh_config}" > /dev/null; then |
| 127 | + log "Adding an entry to ${ssh_config} for ${docker_name}." |
| 128 | + cat >> "${ssh_config}" <<CONF |
| 129 | +
|
| 130 | +Host ${docker_name} |
| 131 | + User root |
| 132 | + HostName 127.0.0.1 |
| 133 | + Port ${port_num} |
| 134 | + IdentityFile ${ssh_id_file} |
| 135 | +CONF |
| 136 | + else |
| 137 | + log "SSH configuration already contains an entry for ${docker_name}." |
| 138 | + fi |
| 139 | + echo |
| 140 | + |
| 141 | + # Start docker container |
| 142 | + log "Starting docker container: ${docker_name}" |
| 143 | + log "Note: This may fail if the container was already created." |
| 144 | + docker run --restart always --name "${docker_name}" -d -p ${port_num}:22 lnl7/nix:ssh |
| 145 | + echo |
| 146 | + |
| 147 | + # Write remote systems configuration |
| 148 | + remote_sys_conf="${working_dir}/remote-systems.conf" |
| 149 | + log "Writing remote systems configuration to ${remote_sys_conf}" |
| 150 | + |
| 151 | + cat > "${remote_sys_conf}" <<CONF |
| 152 | +${docker_name} x86_64-linux ${ssh_id_file} 1 |
| 153 | +CONF |
| 154 | + echo |
| 155 | + |
| 156 | + # Test connection |
| 157 | + log "Running SSH test" |
| 158 | + ssh "${docker_name}" echo "SSH connection succeeded!" || log "SSH connection failed." |
| 159 | + echo |
| 160 | + |
| 161 | + # Copy SSH key to container |
| 162 | + log "Copying SSH key to container" |
| 163 | + scp "${key_file}.pub" nix-docker:/root/.ssh/authorized_keys |
| 164 | + |
| 165 | + # Export environment |
| 166 | + export_env |
| 167 | + |
| 168 | + remove_instructions |
| 169 | + finish_message |
| 170 | +} |
| 171 | + |
| 172 | +do_start () { |
| 173 | + # Default values |
| 174 | + working_dir="${HOME}/${working_dir_name}" |
| 175 | + docker_name="nix-docker" |
| 176 | + |
| 177 | + while [[ $# -gt 0 ]]; do |
| 178 | + case "${1}" in |
| 179 | + -d|--dir) |
| 180 | + working_dir=${2} |
| 181 | + shift 2 |
| 182 | + ;; |
| 183 | + -n|--name) |
| 184 | + docker_name=${2} |
| 185 | + shift 2 |
| 186 | + ;; |
| 187 | + esac |
| 188 | + done |
| 189 | + |
| 190 | + remote_sys_conf="${working_dir}/remote-systems.conf" |
| 191 | + |
| 192 | + log "Starting Docker container: ${docker_name}" |
| 193 | + docker start ${docker_name} |
| 194 | + |
| 195 | + # Export environment |
| 196 | + export_env |
| 197 | + finish_message |
| 198 | +} |
| 199 | + |
| 200 | +log "NOTE: As this script exports variables, it should be run with 'source', e.g. 'source nix-docker'" |
| 201 | + |
| 202 | +while [[ $# -gt 0 ]]; do |
| 203 | + case "${1}" in |
| 204 | + -h|--help) |
| 205 | + usage |
| 206 | + shift |
| 207 | + ;; |
| 208 | + init) |
| 209 | + shift |
| 210 | + do_init "$@" |
| 211 | + ;; |
| 212 | + start) |
| 213 | + shift |
| 214 | + do_start "$@" |
| 215 | + ;; |
| 216 | + esac |
| 217 | +done |
0 commit comments