bash interpolation whithin nix interpolation is hard

This commit is contained in:
William 2025-07-13 19:03:02 -03:00
parent 1ebbb7937d
commit df4aa25984
2 changed files with 15 additions and 17 deletions

View file

@ -1,24 +1,16 @@
{ {
lib, lib,
stdenv, stdenv,
writeShellScriptBin,
nixos-rebuild, nixos-rebuild,
openssh,
coreutils, coreutils,
gnugrep,
gawk,
}: }:
stdenv.mkDerivation rec { stdenv.mkDerivation rec {
pname = "nixos-deploy"; pname = "nixos-deploy";
version = "1.0"; version = "1.0";
src = null; passthru.script = writeShellScriptBin "nixos-deploy" ''
dontUnpack = true;
installPhase = ''
mkdir -p $out/bin
cat > $out/bin/nixos-deploy << 'EOF'
#!/usr/bin/env bash
set -euo pipefail set -euo pipefail
LOCAL_BUILD=false LOCAL_BUILD=false
@ -89,10 +81,10 @@ stdenv.mkDerivation rec {
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
@ -102,7 +94,7 @@ stdenv.mkDerivation rec {
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"
@ -118,14 +110,19 @@ stdenv.mkDerivation rec {
echo -e "Running: $REBUILD_CMD" echo -e "Running: $REBUILD_CMD"
exec $REBUILD_CMD exec $REBUILD_CMD
EOF '';
chmod +x $out/bin/nixos-deploy dontUnpack = true;
installPhase = ''
mkdir -p $out/bin
cp ${passthru.script}/bin/nixos-deploy $out/bin/
''; '';
meta = with lib; { meta = with lib; {
description = "Tool to deploy a NixOS flake to a remote host using nixos-rebuild"; description = "Deploy a NixOS flake to a remote host using nixos-rebuild";
license = licenses.mit; license = licenses.mit;
platforms = platforms.unix; platforms = platforms.unix;
maintainers = with maintainers; [ ];
}; };
} }

1
result Symbolic link
View file

@ -0,0 +1 @@
/nix/store/4mca2jn3fhpr86w88dnlpypdllsy9f3r-nixos-deploy-1.0