fix character escaping in nixos-deploy

This commit is contained in:
William 2025-07-13 18:46:39 -03:00
parent 79a2576dfd
commit 1ebbb7937d

View file

@ -1,134 +1,131 @@
{ lib {
, stdenv lib,
, nixos-rebuild stdenv,
, openssh nixos-rebuild,
, coreutils openssh,
, gnugrep coreutils,
, gawk gnugrep,
gawk,
}: }:
stdenv.mkDerivation rec { stdenv.mkDerivation rec {
pname = "nixos-deploy"; pname = "nixos-deploy";
version = "1.0"; version = "1.0";
src = lib.fakeSha256; # will be ignored since we're using `installPhase` src = null;
dontUnpack = true; dontUnpack = true;
buildInputs = [ ];
installPhase = '' installPhase = ''
mkdir -p $out/bin mkdir -p $out/bin
cat > $out/bin/nixos-deploy << 'EOF' cat > $out/bin/nixos-deploy << 'EOF'
#!/usr/bin/env bash #!/usr/bin/env bash
set -euo pipefail set -euo pipefail
LOCAL_BUILD=false LOCAL_BUILD=false
ACTION="switch" ACTION="switch"
FLAKE_URI="" FLAKE_URI=""
TARGET_HOST="" TARGET_HOST=""
SSH_USER="" SSH_USER=""
SSH_HOST="" SSH_HOST=""
show_usage() { show_usage() {
echo -e "Usage: nixos-deploy [--local-build] [--boot] <flake-uri> [user@]host" echo -e "Usage: nixos-deploy [--local-build] [--boot] <flake-uri> [user@]host"
echo "" echo ""
echo -e "Arguments:" echo -e "Arguments:"
echo " flake-uri Flake URI (e.g., .#hostname)" echo " flake-uri Flake URI (e.g., .#hostname)"
echo " [user@]host Target host, optionally with user" echo " [user@]host Target host, optionally with user"
echo "" echo ""
echo -e "Options:" echo -e "Options:"
echo " --local-build Build locally instead of on remote" echo " --local-build Build locally instead of on remote"
echo " --boot Use 'boot' instead of 'switch' action" echo " --boot Use 'boot' instead of 'switch' action"
echo "" echo ""
echo -e "Examples:" echo -e "Examples:"
echo " nixos-deploy .#hostname user@192.168.1.10" echo " nixos-deploy .#hostname user@192.168.1.10"
echo " nixos-deploy --local-build .#hostname 192.168.1.10" echo " nixos-deploy --local-build .#hostname 192.168.1.10"
echo " nixos-deploy --boot .#hostname 192.168.1.10" echo " nixos-deploy --boot .#hostname 192.168.1.10"
echo " nixos-deploy .#hostname 192.168.1.10 # uses current user" echo " nixos-deploy .#hostname 192.168.1.10 # uses current user"
exit 1 exit 1
} }
while [[ $# -gt 0 ]]; do while [[ $# -gt 0 ]]; do
case $1 in case $1 in
--local-build) --local-build)
LOCAL_BUILD=true LOCAL_BUILD=true
shift shift
;; ;;
--boot) --boot)
ACTION="boot" ACTION="boot"
shift shift
;; ;;
--help|-h) --help|-h)
show_usage
;;
-*)
echo -e "Unknown option: $1"
show_usage
;;
*)
if [[ -z "$FLAKE_URI" ]]; then
FLAKE_URI="$1"
elif [[ -z "$TARGET_HOST" ]]; then
TARGET_HOST="$1"
else
echo -e "Too many arguments"
show_usage show_usage
fi ;;
shift -*)
;; echo -e "Unknown option: $1"
esac show_usage
done ;;
*)
if [[ -z "$FLAKE_URI" ]]; then
FLAKE_URI="$1"
elif [[ -z "$TARGET_HOST" ]]; then
TARGET_HOST="$1"
else
echo -e "Too many arguments"
show_usage
fi
shift
;;
esac
done
if [[ -z "$FLAKE_URI" ]]; then if [[ -z "$FLAKE_URI" ]]; then
echo -e "flake-uri is required" echo -e "flake-uri is required"
show_usage show_usage
fi fi
if [[ -z "$TARGET_HOST" ]]; then if [[ -z "$TARGET_HOST" ]]; then
echo -e "target host is required" echo -e "target host is required"
show_usage show_usage
fi fi
if [[ "$TARGET_HOST" == *"@"* ]]; then if [[ "$TARGET_HOST" == *"@"* ]]; then
SSH_USER="${TARGET_HOST%@*}" SSH_USER="''${TARGET_HOST%@*}"
SSH_HOST="${TARGET_HOST#*@}" SSH_HOST="''${TARGET_HOST#*@}"
else else
SSH_USER="$("${coreutils}/bin/whoami")" SSH_USER="$("''${coreutils}/bin/whoami")"
SSH_HOST="$TARGET_HOST" SSH_HOST="$TARGET_HOST"
fi fi
echo "Deploying $FLAKE_URI to $SSH_HOST as user $SSH_USER (action: $ACTION)" echo "Deploying $FLAKE_URI to $SSH_HOST as user $SSH_USER (action: $ACTION)"
if [[ "$LOCAL_BUILD" != "true" ]]; then if [[ "$LOCAL_BUILD" != "true" ]]; then
GC_ROOT_PATH="/tmp/nixos-deploy-$SSH_HOST-$$" GC_ROOT_PATH="/tmp/nixos-deploy-$SSH_HOST-$$"
fi fi
REBUILD_CMD="${nixos-rebuild}/bin/nixos-rebuild $ACTION --flake $FLAKE_URI --target-host $TARGET_HOST" REBUILD_CMD="''${nixos-rebuild}/bin/nixos-rebuild $ACTION --flake $FLAKE_URI --target-host $TARGET_HOST"
if [[ "$LOCAL_BUILD" == "true" ]]; then if [[ "$LOCAL_BUILD" == "true" ]]; then
echo -e "Building locally and deploying to remote host" echo -e "Building locally and deploying to remote host"
else else
REBUILD_CMD="$REBUILD_CMD --build-host $SSH_HOST" REBUILD_CMD="$REBUILD_CMD --build-host $SSH_HOST"
echo -e "Building on remote host" echo -e "Building on remote host"
fi fi
if [[ "$SSH_USER" != "root" ]]; then if [[ "$SSH_USER" != "root" ]]; then
REBUILD_CMD="$REBUILD_CMD --use-remote-sudo" REBUILD_CMD="$REBUILD_CMD --use-remote-sudo"
echo -e "Using remote sudo for non-root user" echo -e "Using remote sudo for non-root user"
fi fi
echo -e "Running: $REBUILD_CMD" echo -e "Running: $REBUILD_CMD"
exec $REBUILD_CMD exec $REBUILD_CMD
EOF EOF
chmod +x $out/bin/nixos-deploy
chmod +x $out/bin/nixos-deploy
''; '';
meta = with lib; { meta = with lib; {
description = "Tool to deploy a NixOS flake to a remote host using nixos-rebuild"; description = "Tool to deploy a NixOS flake to a remote host using nixos-rebuild";
license = licenses.mit; license = licenses.mit;
maintainers = with maintainers; [ ];
platforms = platforms.unix; platforms = platforms.unix;
}; };
} }