{
  pkgs,
  host,
  backupFiles ? [ ".config/mimeapps.list.backup" ],
  project,
  ...
}: let
  backupFilesString = pkgs.lib.strings.concatStringsSep " " backupFiles;
in
  pkgs.writeShellScriptBin "ncli" ''
    #!${pkgs.bash}/bin/bash
    set -euo pipefail

    # --- Configuration ---
    PROJECT="${project}"
    HOST="${host}"
    BACKUP_FILES_STR="${backupFilesString}"
    VERSION="2.0.0"
    FLAKE_NIX_PATH="$HOME/$PROJECT/flake.nix"

    read -r -a BACKUP_FILES <<< "$BACKUP_FILES_STR"

    # --- Read Colors file ---
    source /$HOME/$PROJECT/other/colors.sh

    # --- Helper Functions ---
    print_help() {
      echo "NixOS CLI Utility -- version $VERSION"
      echo ""
      echo "Usage: ncli [command]"
      echo ""
      echo "System Commands:"
      echo "  rebuild         - Rebuild the NixOS system configuration."
      echo "  update          - Update the flake and rebuild the system."
      echo ""
      echo "Maintenance Commands:"
      echo "  cleanup         - Clean up old system generations. Can specify a number to keep."
      echo "  diag            - Create a system diagnostic report (saves to ~/diag.txt)."
      echo "  list-gens       - List user and system generations."
      echo "  trim            - Trim filesystems to improve SSD performance."
      echo ""
      echo "Git Commands:"
      echo "  commit [msg]    - Add all changes and commit with message."
      echo "  push            - Push changes to origin."
      echo "  pull            - Pull latest changes from origin."
      echo "  status          - Show git status."
      echo ""
      echo "  help            - Show this help message."
      echo ""
    }

    handle_backups() {
      if [ ''${#BACKUP_FILES[@]} -eq 0 ]; then
        echo "No backup files configured to check."
        return
      fi

      echo "Checking for backup files to remove..."
      for file_path in "''${BACKUP_FILES[@]}"; do
        full_path="$HOME/$file_path"
        if [ -f "$full_path" ]; then
          echo "Removing stale backup file: $full_path"
          rm "$full_path"
        fi
      done
    }

    # --- Main Logic ---
    if [ "$#" -eq 0 ]; then
      echo "Error: No command provided." >&2
      print_help
      exit 1
    fi

    case "$1" in
      cleanup)
        echo "Warning! This will remove old generations of your system."
        read -p "How many generations to keep (default: all)? " keep_count

        if [ -z "$keep_count" ]; then
          read -p "This will remove all but the current generation. Continue (y/N)? " -n 1 -r
          echo
          if [[ $REPLY =~ ^[Yy]$ ]]; then
            nh clean all -v
          else
            echo "Cleanup cancelled."
          fi
        else
          read -p "This will keep the last $keep_count generations. Continue (y/N)? " -n 1 -r
          echo
          if [[ $REPLY =~ ^[Yy]$ ]]; then
            nh clean all -k "$keep_count" -v
          else
            echo "Cleanup cancelled."
          fi
        fi

        LOG_DIR="$HOME/ncli-cleanup-logs"
        mkdir -p "$LOG_DIR"
        LOG_FILE="$LOG_DIR/ncli-cleanup-$(date +%Y-%m-%d_%H-%M-%S).log"
        echo "Cleaning up old log files..." >> "$LOG_FILE"
        find "$LOG_DIR" -type f -mtime +3 -name "*.log" -delete >> "$LOG_FILE" 2>&1
        echo "Cleanup process logged to $LOG_FILE"
        ;;
      diag)
        echo "Generating system diagnostic report..."
        {
          echo "=== NixOS System Diagnostic Report ==="
          echo "Generated: $(date)"
          echo ""
          echo "=== System Information ==="
          inxi --full 2>/dev/null || echo "inxi not available"
          echo ""
          echo "=== Git Status ==="
          cd "$HOME/$PROJECT" 2>/dev/null && git status 2>/dev/null || echo "Git status not available"
          echo ""
        } > "$HOME/diag.txt"
        echo "Diagnostic report saved to $HOME/diag.txt"
        ;;
      help)
        print_help
        ;;
      list-gens)
        echo "--- User Generations ---"
        nix-env --list-generations | cat || echo "Could not list user generations."
        echo ""
        echo "--- System Generations ---"
        nix profile history --profile /nix/var/nix/profiles/system | cat || echo "Could not list system generations."
        ;;
      rebuild)
        handle_backups
        geno=$(sudo nix-env --list-generations --profile /nix/var/nix/profiles/system | grep current | awk '{print $1}')
        echo -e "Starting NixOS rebuild for current host: $HOST on generation: $YELLOW$geno$NOCOLOR"
        cd "$HOME/$PROJECT" || { echo "Error: Could not change to $HOME/$PROJECT"; exit 1; }

        current=""
        if [ -f /etc/nixos-tags ]; then
          current=$(cat /etc/nixos-tags)
        fi

        if sudo nixos-rebuild switch --flake .; then
          echo "✓ Rebuild finished successfully for $HOST"

          if [ -n "$current" ]; then
            sudo /run/current-system/specialisation/$current/bin/switch-to-configuration test
          else
            echo "No specialization tag found, staying on default system."
          fi
        
          genn=$(sudo nix-env --list-generations --profile /nix/var/nix/profiles/system | grep current | awk '{print $1}')
          echo -e "Running on new generation: $YELLOW $geno $NOCOLOR-> $GREEN$genn$NOCOLOR"

        else
          echo "✗ Rebuild failed for $HOST" >&2
          exit 1
        fi
        ;;
      update)
        handle_backups
        geno=$(sudo nix-env --list-generations --profile /nix/var/nix/profiles/system | grep current | awk '{print $1}')
        echo -e "Updating flake and rebuilding system for current host: $HOST on generation: $YELLOW$geno$NOCOLOR"
        cd "$HOME/$PROJECT" || { echo "Error: Could not change to $HOME/$PROJECT"; exit 1; }

        echo "Updating flake..."
        if nix flake update; then
          echo "✓ Flake updated successfully"
        else
          echo "✗ Flake update failed" >&2
          exit 1
        fi


        current=""
        if [ -f /etc/nixos-tags ]; then
          current=$(cat /etc/nixos-tags)
        fi

        if [ -n "$current" ]; then
          echo "Rebuilding system... Current specialization: $current"
        else
          echo "Rebuilding system... Staying on current specialization"
        fi

        if sudo nixos-rebuild switch --flake .; then
          echo "✓ Update and rebuild finished successfully for $HOST"

          if [ -n "$current" ]; then
            sudo /run/current-system/specialisation/$current/bin/switch-to-configuration test
          else
            echo "No specialization tag found, staying on default system."
          fi

          genn=$(sudo nix-env --list-generations --profile /nix/var/nix/profiles/system | grep current | awk '{print $1}')
          echo -e "Running on new generation: $YELLOW $geno $NOCOLOR-> $GREEN$genn$NOCOLOR"

        else
          echo "✗ Update and rebuild failed for $HOST" >&2
          exit 1
        fi
        ;;
      commit)
        cd "$HOME/$PROJECT" || { echo "Error: Could not change to $HOME/$PROJECT"; exit 1; }
        if [ "$#" -lt 2 ]; then
          read -p "Enter commit message: " commit_msg
        else
          shift
          commit_msg="$*"
        fi

        if [ -z "$commit_msg" ]; then
          echo "Error: Commit message cannot be empty" >&2
          exit 1
        fi

        git add -A && git commit -m "$commit_msg"
        ;;
      home-backups)
        ls -a ~ | grep backup
        ;;
      switch)
        current=""
        if [ -f /etc/nixos-tags ]; then
          current=$(cat /etc/nixos-tags)
        fi

        if [ "$#" -ge 2 ]; then
          spec_name="$2"
          if [ -n "$current" ]; then
            echo "Already on specialization: $current. Cannot switch directly to '$spec_name'. Please reboot or return to default first."
          else
            if [ -d "/run/current-system/specialisation/$spec_name" ]; then
              echo "Switching to specialization: $spec_name"
              sudo /run/current-system/specialisation/$spec_name/bin/switch-to-configuration test
            else
              echo "Error: Specialization '$spec_name' not found."
              echo "Available specializations:"
              ls /run/current-system/specialisation/
            fi
          fi
        else
          if [ -n "$current" ]; then
            echo "Already on a specialization: $current. To switch, please reboot, or use 'sudo nixos-rebuild switch --flake .' to get back to default, and then switch after."
          else
            specs=$(ls /run/current-system/specialisation/)
            echo "Specializations available:"
            echo "$specs"
            echo ""
            echo "To switch to a specialization, run: 'ncli switch <tag>'"
          fi
        fi
        ;;
      push)
        cd "$HOME/$PROJECT" || { echo "Error: Could not change to $HOME/$PROJECT"; exit 1; }
        git push origin $(git branch --show-current)
        ;;
      pull)
        cd "$HOME/$PROJECT" || { echo "Error: Could not change to $HOME/$PROJECT"; exit 1; }
        git pull origin $(git branch --show-current)
        ;;
      status)
        cd "$HOME/$PROJECT" || { echo "Error: Could not change to $HOME/$PROJECT"; exit 1; }
        git status
        ;;
      trim)
        echo "Running 'sudo fstrim -v /' may take a few minutes and impact system performance."
        read -p "Enter to run now or enter to exit (y/N): " -n 1 -r
        echo # move to a new line
        if [[ $REPLY =~ ^[Yy]$ ]]; then
          echo "Running fstrim..."
          sudo fstrim -v /
          echo "fstrim complete."
        else
          echo "Trim operation cancelled."
        fi
        ;;
      *)
        echo "Error: Invalid command '$1'" >&2
        print_help
        exit 1
        ;;
    esac
  ''
