use stdenv on nixos-deploy derivation

This commit is contained in:
William 2025-07-13 18:44:55 -03:00
parent 8a1af741dd
commit 79a2576dfd

View file

@ -1,14 +1,26 @@
{
lib,
writeShellScript,
nixos-rebuild,
openssh,
coreutils,
gnugrep,
gawk,
{ lib
, stdenv
, nixos-rebuild
, openssh
, coreutils
, gnugrep
, gawk
}:
writeShellScript "nixos-deploy" ''
stdenv.mkDerivation rec {
pname = "nixos-deploy";
version = "1.0";
src = lib.fakeSha256; # will be ignored since we're using `installPhase`
dontUnpack = true;
buildInputs = [ ];
installPhase = ''
mkdir -p $out/bin
cat > $out/bin/nixos-deploy << 'EOF'
#!/usr/bin/env bash
set -euo pipefail
LOCAL_BUILD=false
@ -79,17 +91,17 @@ writeShellScript "nixos-deploy" ''
fi
if [[ "$TARGET_HOST" == *"@"* ]]; then
SSH_USER="''${TARGET_HOST%@*}"
SSH_HOST="''${TARGET_HOST#*@}"
SSH_USER="${TARGET_HOST%@*}"
SSH_HOST="${TARGET_HOST#*@}"
else
SSH_USER="$(${coreutils}/bin/whoami)"
SSH_USER="$("${coreutils}/bin/whoami")"
SSH_HOST="$TARGET_HOST"
fi
echo "Deploying $FLAKE_URI to $SSH_HOST as user $SSH_USER (action: $ACTION)"
if [[ "$LOCAL_BUILD" != "true" ]]; then
GC_ROOT_PATH="/tmp/nixos-deploy-$SSH_HOST-$"
GC_ROOT_PATH="/tmp/nixos-deploy-$SSH_HOST-$$"
fi
REBUILD_CMD="${nixos-rebuild}/bin/nixos-rebuild $ACTION --flake $FLAKE_URI --target-host $TARGET_HOST"
@ -108,4 +120,15 @@ writeShellScript "nixos-deploy" ''
echo -e "Running: $REBUILD_CMD"
exec $REBUILD_CMD
''
EOF
chmod +x $out/bin/nixos-deploy
'';
meta = with lib; {
description = "Tool to deploy a NixOS flake to a remote host using nixos-rebuild";
license = licenses.mit;
maintainers = with maintainers; [ ];
platforms = platforms.unix;
};
}