aboutsummaryrefslogblamecommitdiff
path: root/flake/scripts/remote.nix
blob: c96aa8194223c158f04436efd826fdf00e5701c7 (plain) (tree)





































































































































                                                                           
{
  pkgs,
}:
[
  (pkgs.writeScriptBin "rbuild" ''
    #!${pkgs.bash}/bin/bash
    set -e

    # Check if host argument is provided
    if [ -z "$1" ]; then
      echo "❌ Error: Please specify a host"
      echo "Usage: rbuild <hostname>"
      echo "Example: rbuild rivendell"
      exit 1
    fi

    HOST="$1"
    echo "> Running nixos-rebuild build for $HOST..."

    ${pkgs.nixos-rebuild}/bin/nixos-rebuild build \
      --keep-going \
      --flake ".#$HOST" \
      --target-host "$HOST" \
      --fast \
      --use-remote-sudo \
      --use-substitutes

    echo "> nixos-rebuild build for $HOST was successful ✅"
  '')

  (pkgs.writeScriptBin "rswitch" ''
    #!${pkgs.bash}/bin/bash
    set -e

    # Check if host argument is provided
    if [ -z "$1" ]; then
      echo "❌ Error: Please specify a host"
      echo "Usage: rswitch <hostname>"
      echo "Example: rswitch rivendell"
      exit 1
    fi

    HOST="$1"
    echo "> Running nixos-rebuild switch for $HOST..."

    ${pkgs.nixos-rebuild}/bin/nixos-rebuild switch \
      --keep-going \
      --flake ".#$HOST" \
      --target-host "$HOST" \
      --fast \
      --use-remote-sudo \
      --use-substitutes

    echo "> NixOS config was successfully applied to $HOST 🚀"
  '')

  (pkgs.writeScriptBin "rdeploy" ''
    #!${pkgs.bash}/bin/bash
    set -e

    # Check if host argument is provided
    if [ -z "$1" ]; then
      echo "❌ Error: Please specify a host"
      echo "Usage: rdeploy <hostname>"
      echo "Example: rdeploy rivendell"
      exit 1
    fi

    HOST="$1"
    echo "> Deploying NixOS configuration to $HOST..."
    echo ""

    # First build
    echo "📦 Step 1/2: Building configuration..."
    ${pkgs.nixos-rebuild}/bin/nixos-rebuild build \
      --keep-going \
      --flake ".#$HOST" \
      --target-host "$HOST" \
      --fast \
      --use-remote-sudo \
      --use-substitutes

    echo ""
    echo "🔄 Step 2/2: Switching configuration..."
    ${pkgs.nixos-rebuild}/bin/nixos-rebuild switch \
      --keep-going \
      --flake ".#$HOST" \
      --target-host "$HOST" \
      --fast \
      --use-remote-sudo \
      --use-substitutes

    echo ""
    echo "> NixOS deployment to $HOST completed successfully! 🎉"
  '')

  (pkgs.writeScriptBin "rhosts" ''
    #!${pkgs.bash}/bin/bash
    echo "> Available NixOS hosts in your flake:"
    echo ""

    # This attempts to list nixosConfigurations from the flake
    # You might need to adjust this based on your flake structure
    nix flake show --json 2>/dev/null | \
      ${pkgs.jq}/bin/jq -r '.nixosConfigurations | keys[]' 2>/dev/null || \
      echo "Unable to list hosts automatically. Check your flake/hosts.nix"
  '')

  (pkgs.writeScriptBin "rtest" ''
    #!${pkgs.bash}/bin/bash
    set -e

    # Check if host argument is provided
    if [ -z "$1" ]; then
      echo "❌ Error: Please specify a host"
      echo "Usage: rtest <hostname>"
      echo "Example: rtest rivendell"
      exit 1
    fi

    HOST="$1"
    echo "> Running dry-run build for $HOST..."

    ${pkgs.nixos-rebuild}/bin/nixos-rebuild dry-build \
      --keep-going \
      --flake ".#$HOST" \
      --target-host "$HOST" \
      --fast \
      --use-remote-sudo \
      --use-substitutes

    echo "> Dry-run build for $HOST completed ✅"
  '')
]