-
-
Notifications
You must be signed in to change notification settings - Fork 125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
init nixos-remote #1
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6af2a66
init nixos-remote
Lassulus 5029d6a
add --kexec paramter to specify custom image
Lassulus 650b6db
ssh timeout
Lassulus b7c4b74
ssh: use timout only for waiting
Lassulus 416390f
check if tar exists on target system
Lassulus ab86ca7
add minimal README
Lassulus 28809e0
check target OS
Lassulus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,8 @@ | ||
# nixos-remote | ||
# nixos-remote - install nixos everywhere via ssh | ||
|
||
## Usage | ||
Needs a repo with your configurations with flakes. for a minimal example checkout https://github.com/Lassulus/flakes-testing. | ||
afterwards you can just run: | ||
``` | ||
./nixos-remote root@yourip --flake github:your-user/your-repo#your-system | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
#!/usr/bin/env bash | ||
set -eufo pipefail | ||
set -x | ||
|
||
showUsage() { | ||
cat <<USAGE | ||
Usage: $0 [options] ssh-host | ||
|
||
Options: | ||
|
||
* -f, --flake flake | ||
set the flake to install the system from | ||
* --arg name value | ||
pass value to nix-build. can be used to set disk-names for example | ||
* --argstr name value | ||
pass value to nix-build as string | ||
* --kexec url | ||
use another kexec tarball to bootstrap NixOS | ||
USAGE | ||
} | ||
|
||
abort() { | ||
echo "aborted: $*" >&2 | ||
exit 1 | ||
} | ||
|
||
nix_args=() | ||
kexec_url=https://github.com/nix-community/nixos-images/releases/download/nixos-22.05/nixos-kexec-installer-x86_64-linux.tar.gz | ||
|
||
while [[ $# -gt 0 ]]; do | ||
case "$1" in | ||
-f | --flake) | ||
flake=$2 | ||
shift | ||
;; | ||
--argstr | --arg) | ||
nix_args+=("$1" "$2" "$3") | ||
shift | ||
shift | ||
;; | ||
--help) | ||
showUsage | ||
exit 0 | ||
;; | ||
--kexec) | ||
kexec_url=$2 | ||
shift | ||
;; | ||
*) | ||
if [ -z ${ssh_connection+x} ]; then | ||
ssh_connection=$1 | ||
else | ||
showUsage | ||
exit 1 | ||
fi | ||
;; | ||
esac | ||
shift | ||
done | ||
|
||
# ssh wrapper | ||
timeout_ssh_() { | ||
timeout 10 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no "$ssh_connection" "$@" | ||
} | ||
ssh_() { | ||
ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no "$ssh_connection" "$@" | ||
} | ||
|
||
# first check if the remote system is kexec booted | ||
if $(ssh_ -- test -e /etc/is-kexec); then | ||
is_kexec=y | ||
fi | ||
|
||
|
||
echo ${is_kexec-n} | ||
if [ ${is_kexec-n} != "y" ]; then | ||
# TODO we probably need an architecture detection here | ||
ssh_ << SSH | ||
set -efux | ||
fetch(){ | ||
if command -v curl >/dev/null 2>&1; then | ||
curl --fail -Ss -L "\$1" | ||
elif command -v wget >/dev/null 2>&1; then | ||
wget "\$1" -O- | ||
else | ||
echo "no downloader (curl or wget) found, bailing out" | ||
exit 1 | ||
fi | ||
} | ||
if command -v tar >/dev/null 2>&1; then | ||
echo "no tar command found, but required to unpack kexec tarball" >&2 | ||
exit 1 | ||
fi | ||
Lassulus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
rm -rf /root/kexec | ||
mkdir -p /root/kexec | ||
Lassulus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
fetch "$kexec_url" | tar -C /root/kexec -xvzf- | ||
export TMPDIR=/root/kexec | ||
setsid /root/kexec/kexec/run | ||
SSH | ||
# wait for machine to become unreachable | ||
while timeout_ssh_ -- exit 0; do sleep 1; done | ||
|
||
# watiting for machine to become available again | ||
until ssh_ -o ConnectTimeout=10 -- exit 0; do sleep 5; done | ||
fi | ||
|
||
|
||
ssh_ << SSH | ||
set -efux | ||
$(declare -p nix_args) | ||
nix --extra-experimental-features nix-command --extra-experimental-features flakes \ | ||
run github:nix-community/disko \ | ||
--no-write-lock-file -- \ | ||
--debug -m create "\${nix_args[@]}" --flake "$flake" | ||
|
||
nix --extra-experimental-features nix-command --extra-experimental-features flakes \ | ||
run github:nix-community/disko \ | ||
--no-write-lock-file -- \ | ||
--debug -m mount "\${nix_args[@]}" --flake "$flake" | ||
|
||
nixos-install --flake "$flake" | ||
reboot | ||
SSH |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe we just make it dependant on uname -m from the target?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.