Skip to content

Commit 45b4628

Browse files
Mic92Qubasa
andcommitted
Add breakpoint debugging function
Adds an interactive debugging function that allows developers to drop into a shell with all variables available for inspection. Co-authored-by: Qubasa <consulting@qube.email>
1 parent af70cbe commit 45b4628

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

src/nixos-anywhere.sh

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,53 @@ declare -A extraFilesOwnership=()
6868
declare -a nixCopyOptions=()
6969
declare -a sshArgs=("-o" "IdentitiesOnly=yes" "-i" "$tempDir/nixos-anywhere" "-o" "UserKnownHostsFile=/dev/null" "-o" "StrictHostKeyChecking=no")
7070

71+
breakpoint() {
72+
(
73+
set +x
74+
echo "Breakpoint reached at line ${BASH_LINENO[0]}."
75+
76+
# Create a temporary directory for debug files
77+
debugTmpDir=$(mktemp -d /tmp/nixos-anywhere-debug.XXXXXX)
78+
chmod 700 "$debugTmpDir" # Secure the directory
79+
80+
# Set up cleanup trap
81+
# shellcheck disable=SC2064
82+
trap "rm -rf \"$debugTmpDir\"" EXIT
83+
84+
# Save all variables (local and exported) to a file
85+
(
86+
set -o posix
87+
set
88+
) >"$debugTmpDir/debug_vars.sh"
89+
90+
# Create the rcfile with explicit terminal handling
91+
cat >"$debugTmpDir/debug_rcfile.sh" <<EOF
92+
# Source all variables
93+
set +o posix
94+
source "$debugTmpDir/debug_vars.sh"
95+
96+
# Force output to terminal
97+
exec 2>/dev/tty
98+
exec 1>/dev/tty
99+
exec 0</dev/tty
100+
101+
# Show some helpful info
102+
echo "Debug shell started. All variables from parent scope are available."
103+
echo "Example: echo \\\$tempDir"
104+
echo "Type 'exit' to continue execution."
105+
106+
# Set a nice prompt
107+
PS1="[DEBUG]> "
108+
EOF
109+
110+
echo "Variables saved to $debugTmpDir/debug_vars.sh"
111+
echo "Starting debug shell (redirecting to /dev/tty for interactivity)..."
112+
113+
# Start an interactive shell with explicit terminal redirection
114+
bash --rcfile "$debugTmpDir/debug_rcfile.sh" </dev/tty >/dev/tty 2>&1
115+
)
116+
}
117+
71118
showUsage() {
72119
cat <<USAGE
73120
Usage: nixos-anywhere [options] [<ssh-host>]

0 commit comments

Comments
 (0)