|
1 | 1 | #!/usr/bin/env bash |
2 | 2 | set -euo pipefail |
3 | 3 |
|
| 4 | +here=$(dirname "${BASH_SOURCE[0]}") |
| 5 | +kexecUrl="" |
| 6 | +kexecExtraFlags="" |
| 7 | +enableDebug="" |
| 8 | +nixOptions=( |
| 9 | + --extra-experimental-features 'nix-command flakes' |
| 10 | + "--no-write-lock-file" |
| 11 | +) |
| 12 | +SSH_PRIVATE_KEY=${SSH_PRIVATE_KEY-} |
| 13 | + |
| 14 | +declare -A phases |
| 15 | +phases[kexec]=1 |
| 16 | +phases[disko]=1 |
| 17 | +phases[install]=1 |
| 18 | +phases[reboot]=1 |
| 19 | + |
| 20 | +substituteOnDestination=y |
| 21 | +sshPrivateKeyFile= |
| 22 | +if [ -t 0 ]; then # stdin is a tty, we allow interactive input to ssh i.e. passwords |
| 23 | + sshTtyParam="-t" |
| 24 | +else |
| 25 | + sshTtyParam="-T" |
| 26 | +fi |
| 27 | +postKexecSshPort=22 |
| 28 | +buildOnRemote=n |
| 29 | +envPassword= |
| 30 | + |
| 31 | +declare -A diskEncryptionKeys |
| 32 | +declare -a nixCopyOptions |
| 33 | +declare -a sshArgs |
| 34 | + |
4 | 35 | showUsage() { |
5 | 36 | cat <<USAGE |
6 | 37 | Usage: nixos-anywhere [options] <ssh-host> |
@@ -67,170 +98,158 @@ step() { |
67 | 98 | echo "### $* ###" |
68 | 99 | } |
69 | 100 |
|
70 | | -here=$(dirname "${BASH_SOURCE[0]}") |
71 | | -kexecUrl="" |
72 | | -kexecExtraFlags="" |
73 | | -enableDebug="" |
74 | | -nixOptions=( |
75 | | - --extra-experimental-features 'nix-command flakes' |
76 | | - "--no-write-lock-file" |
77 | | -) |
78 | | -SSH_PRIVATE_KEY=${SSH_PRIVATE_KEY-} |
| 101 | +parseArgs() { |
| 102 | + while [[ $# -gt 0 ]]; do |
| 103 | + case "$1" in |
| 104 | + -f | --flake) |
| 105 | + flake=$2 |
| 106 | + shift |
| 107 | + ;; |
| 108 | + -i) |
| 109 | + sshPrivateKeyFile=$2 |
| 110 | + shift |
| 111 | + ;; |
| 112 | + -p | --ssh-port) |
| 113 | + sshArgs+=("-p" "$2") |
| 114 | + shift |
| 115 | + ;; |
| 116 | + --ssh-option) |
| 117 | + sshArgs+=("-o" "$2") |
| 118 | + shift |
| 119 | + ;; |
| 120 | + -L | --print-build-logs) |
| 121 | + printBuildLogs=y |
| 122 | + ;; |
| 123 | + -s | --store-paths) |
| 124 | + diskoScript=$(readlink -f "$2") |
| 125 | + nixosSystem=$(readlink -f "$3") |
| 126 | + shift |
| 127 | + shift |
| 128 | + ;; |
| 129 | + -t | --tty) |
| 130 | + echo "the '$1' flag is deprecated, a tty is now detected automatically" >&2 |
| 131 | + ;; |
| 132 | + --help) |
| 133 | + showUsage |
| 134 | + exit 0 |
| 135 | + ;; |
| 136 | + --kexec) |
| 137 | + kexecUrl=$2 |
| 138 | + shift |
| 139 | + ;; |
| 140 | + --kexec-extra-flags) |
| 141 | + kexecExtraFlags=$2 |
| 142 | + shift |
| 143 | + ;; |
| 144 | + --post-kexec-ssh-port) |
| 145 | + postKexecSshPort=$2 |
| 146 | + shift |
| 147 | + ;; |
| 148 | + --copy-host-keys) |
| 149 | + copyHostKeys=y |
| 150 | + ;; |
| 151 | + --debug) |
| 152 | + enableDebug="-x" |
| 153 | + printBuildLogs=y |
| 154 | + set -x |
| 155 | + ;; |
| 156 | + --extra-files) |
| 157 | + extraFiles=$2 |
| 158 | + shift |
| 159 | + ;; |
| 160 | + --disk-encryption-keys) |
| 161 | + diskEncryptionKeys["$2"]="$3" |
| 162 | + shift |
| 163 | + shift |
| 164 | + ;; |
| 165 | + --phases) |
| 166 | + phases[kexec]=0 |
| 167 | + phases[disko]=0 |
| 168 | + phases[install]=0 |
| 169 | + phases[reboot]=0 |
| 170 | + IFS=, read -r -a phaseList <<<"$2" |
| 171 | + for phase in "${phaseList[@]}"; do |
| 172 | + if [[ ${phases[$phase]:-unset} == unset ]]; then |
| 173 | + abort "Unknown phase: $phase" |
| 174 | + fi |
| 175 | + phases[$phase]=1 |
| 176 | + done |
| 177 | + shift |
| 178 | + ;; |
| 179 | + --stop-after-disko) |
| 180 | + echo "WARNING: --stop-after-disko is deprecated, use --phases=kexec,disko instead" 2>&1 |
| 181 | + phases[kexec]=1 |
| 182 | + phases[disko]=1 |
| 183 | + phases[install]=0 |
| 184 | + phases[reboot]=0 |
| 185 | + ;; |
| 186 | + --no-reboot) |
| 187 | + echo "WARNING: --no-reboot is deprecated, use --phases=kexec,disko,install instead" 2>&1 |
| 188 | + phases[kexec]=1 |
| 189 | + phases[disko]=1 |
| 190 | + phases[install]=1 |
| 191 | + phases[reboot]=0 |
| 192 | + ;; |
| 193 | + --from) |
| 194 | + nixCopyOptions+=("--from" "$2") |
| 195 | + shift |
| 196 | + ;; |
| 197 | + --option) |
| 198 | + key=$2 |
| 199 | + shift |
| 200 | + value=$2 |
| 201 | + shift |
| 202 | + nixOptions+=("--option" "$key" "$value") |
| 203 | + ;; |
| 204 | + --no-substitute-on-destination) |
| 205 | + substituteOnDestination=n |
| 206 | + ;; |
| 207 | + --build-on-remote) |
| 208 | + buildOnRemote=y |
| 209 | + ;; |
| 210 | + --env-password) |
| 211 | + envPassword=y |
| 212 | + ;; |
| 213 | + --vm-test) |
| 214 | + vmTest=y |
| 215 | + ;; |
| 216 | + *) |
| 217 | + if [[ -z ${sshConnection-} ]]; then |
| 218 | + sshConnection="$1" |
| 219 | + else |
| 220 | + showUsage |
| 221 | + exit 1 |
| 222 | + fi |
| 223 | + ;; |
| 224 | + esac |
| 225 | + shift |
| 226 | + done |
79 | 227 |
|
80 | | -declare -A phases |
81 | | -phases[kexec]=1 |
82 | | -phases[disko]=1 |
83 | | -phases[install]=1 |
84 | | -phases[reboot]=1 |
| 228 | + if [[ ${printBuildLogs-n} == "y" ]]; then |
| 229 | + nixOptions+=("-L") |
| 230 | + fi |
85 | 231 |
|
86 | | -substituteOnDestination=y |
87 | | -sshPrivateKeyFile= |
88 | | -if [ -t 0 ]; then # stdin is a tty, we allow interactive input to ssh i.e. passwords |
89 | | - sshTtyParam="-t" |
90 | | -else |
91 | | - sshTtyParam="-T" |
92 | | -fi |
93 | | -postKexecSshPort=22 |
94 | | -buildOnRemote=n |
95 | | -envPassword= |
| 232 | + if [[ ${substituteOnDestination-n} == "y" ]]; then |
| 233 | + nixCopyOptions+=("--substitute-on-destination") |
| 234 | + fi |
96 | 235 |
|
97 | | -declare -A diskEncryptionKeys |
98 | | -declare -a nixCopyOptions |
99 | | -declare -a sshArgs |
| 236 | + if [[ -z ${sshConnection-} ]]; then |
| 237 | + abort "ssh-host must be set" |
| 238 | + fi |
100 | 239 |
|
101 | | -while [[ $# -gt 0 ]]; do |
102 | | - case "$1" in |
103 | | - -f | --flake) |
104 | | - flake=$2 |
105 | | - shift |
106 | | - ;; |
107 | | - -i) |
108 | | - sshPrivateKeyFile=$2 |
109 | | - shift |
110 | | - ;; |
111 | | - -p | --ssh-port) |
112 | | - sshArgs+=("-p" "$2") |
113 | | - shift |
114 | | - ;; |
115 | | - --ssh-option) |
116 | | - sshArgs+=("-o" "$2") |
117 | | - shift |
118 | | - ;; |
119 | | - -L | --print-build-logs) |
120 | | - printBuildLogs=y |
121 | | - ;; |
122 | | - -s | --store-paths) |
123 | | - diskoScript=$(readlink -f "$2") |
124 | | - nixosSystem=$(readlink -f "$3") |
125 | | - shift |
126 | | - shift |
127 | | - ;; |
128 | | - -t | --tty) |
129 | | - echo "the '$1' flag is deprecated, a tty is now detected automatically" >&2 |
130 | | - ;; |
131 | | - --help) |
132 | | - showUsage |
133 | | - exit 0 |
134 | | - ;; |
135 | | - --kexec) |
136 | | - kexecUrl=$2 |
137 | | - shift |
138 | | - ;; |
139 | | - --kexec-extra-flags) |
140 | | - kexecExtraFlags=$2 |
141 | | - shift |
142 | | - ;; |
143 | | - --post-kexec-ssh-port) |
144 | | - postKexecSshPort=$2 |
145 | | - shift |
146 | | - ;; |
147 | | - --copy-host-keys) |
148 | | - copyHostKeys=y |
149 | | - ;; |
150 | | - --debug) |
151 | | - enableDebug="-x" |
152 | | - printBuildLogs=y |
153 | | - set -x |
154 | | - ;; |
155 | | - --extra-files) |
156 | | - extraFiles=$2 |
157 | | - shift |
158 | | - ;; |
159 | | - --disk-encryption-keys) |
160 | | - diskEncryptionKeys["$2"]="$3" |
161 | | - shift |
162 | | - shift |
163 | | - ;; |
164 | | - --phases) |
165 | | - phases[kexec]=0 |
166 | | - phases[disko]=0 |
167 | | - phases[install]=0 |
168 | | - phases[reboot]=0 |
169 | | - IFS=, read -r -a phaseList <<<"$2" |
170 | | - for phase in "${phaseList[@]}"; do |
171 | | - if [[ ${phases[$phase]:-unset} == unset ]]; then |
172 | | - abort "Unknown phase: $phase" |
173 | | - fi |
174 | | - phases[$phase]=1 |
175 | | - done |
176 | | - shift |
177 | | - ;; |
178 | | - --stop-after-disko) |
179 | | - echo "WARNING: --stop-after-disko is deprecated, use --phases=kexec,disko instead" 2>&1 |
180 | | - phases[kexec]=1 |
181 | | - phases[disko]=1 |
182 | | - phases[install]=0 |
183 | | - phases[reboot]=0 |
184 | | - ;; |
185 | | - --no-reboot) |
186 | | - echo "WARNING: --no-reboot is deprecated, use --phases=kexec,disko,install instead" 2>&1 |
187 | | - phases[kexec]=1 |
188 | | - phases[disko]=1 |
189 | | - phases[install]=1 |
190 | | - phases[reboot]=0 |
191 | | - ;; |
192 | | - --from) |
193 | | - nixCopyOptions+=("--from" "$2") |
194 | | - shift |
195 | | - ;; |
196 | | - --option) |
197 | | - key=$2 |
198 | | - shift |
199 | | - value=$2 |
200 | | - shift |
201 | | - nixOptions+=("--option" "$key" "$value") |
202 | | - ;; |
203 | | - --no-substitute-on-destination) |
204 | | - substituteOnDestination=n |
205 | | - ;; |
206 | | - --build-on-remote) |
207 | | - buildOnRemote=y |
208 | | - ;; |
209 | | - --env-password) |
210 | | - envPassword=y |
211 | | - ;; |
212 | | - --vm-test) |
213 | | - vmTest=y |
214 | | - ;; |
215 | | - *) |
216 | | - if [[ -z ${sshConnection-} ]]; then |
217 | | - sshConnection="$1" |
218 | | - else |
219 | | - showUsage |
| 240 | + if [[ -n ${flake-} ]]; then |
| 241 | + if [[ $flake =~ ^(.*)\#([^\#\"]*)$ ]]; then |
| 242 | + flake="${BASH_REMATCH[1]}" |
| 243 | + flakeAttr="${BASH_REMATCH[2]}" |
| 244 | + fi |
| 245 | + if [[ -z ${flakeAttr-} ]]; then |
| 246 | + echo "Please specify the name of the NixOS configuration to be installed, as a URI fragment in the flake-uri." >&2 |
| 247 | + echo 'For example, to use the output nixosConfigurations.foo from the flake.nix, append "#foo" to the flake-uri.' >&2 |
220 | 248 | exit 1 |
221 | 249 | fi |
222 | | - ;; |
223 | | - esac |
224 | | - shift |
225 | | -done |
226 | | - |
227 | | -if [[ ${printBuildLogs-n} == "y" ]]; then |
228 | | - nixOptions+=("-L") |
229 | | -fi |
| 250 | + fi |
230 | 251 |
|
231 | | -if [[ ${substituteOnDestination-n} == "y" ]]; then |
232 | | - nixCopyOptions+=("--substitute-on-destination") |
233 | | -fi |
| 252 | +} |
234 | 253 |
|
235 | 254 | # ssh wrapper |
236 | 255 | runSshTimeout() { |
|
484 | 503 | } |
485 | 504 |
|
486 | 505 | main() { |
487 | | - if [[ -z ${sshConnection-} ]]; then |
488 | | - abort "ssh-host must be set" |
489 | | - fi |
490 | | - |
491 | | - if [[ -n ${flake-} ]]; then |
492 | | - if [[ $flake =~ ^(.*)\#([^\#\"]*)$ ]]; then |
493 | | - flake="${BASH_REMATCH[1]}" |
494 | | - flakeAttr="${BASH_REMATCH[2]}" |
495 | | - fi |
496 | | - if [[ -z ${flakeAttr-} ]]; then |
497 | | - echo "Please specify the name of the NixOS configuration to be installed, as a URI fragment in the flake-uri." >&2 |
498 | | - echo 'For example, to use the output nixosConfigurations.foo from the flake.nix, append "#foo" to the flake-uri.' >&2 |
499 | | - exit 1 |
500 | | - fi |
501 | | - fi |
| 506 | + parseArgs "$@" |
502 | 507 |
|
503 | 508 | if [[ -n ${vmTest-} ]]; then |
504 | 509 | runVmTest |
@@ -582,4 +587,4 @@ main() { |
582 | 587 | step "Done!" |
583 | 588 | } |
584 | 589 |
|
585 | | -main |
| 590 | +main "$@" |
0 commit comments