name: NixOS switch cloud-pc

on:
  workflow_dispatch:
  push:
    paths:
      - ".gitea/workflows/nixos-switch-cloud-pc.yml"
      - "nixos/flake.nix"
      - "nixos/flake.lock"
      - "nixos/modules/**"
      - "nixos/hosts/cloud-pc/**"

jobs:
  switch:
    runs-on: cloud-pc
    steps:
      - name: Sync repo
        run: |
          git config --global --replace-all safe.directory /opt/services
          git -C /opt/services pull origin main --ff-only
          chown -R 1000:100 /opt/services

      - name: Switch NixOS generation
        env:
          NIXOS_DEPLOY_KEY: ${{ secrets.NIXOS_DEPLOY_KEY }}
        run: |
          KEY=$(mktemp)
          echo "$NIXOS_DEPLOY_KEY" > "$KEY"
          chmod 600 "$KEY"
          trap "rm -f $KEY" EXIT
          ssh -i "$KEY" -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null \
            ada@192.168.1.5 \
            "sudo /run/current-system/sw/bin/nixos-rebuild switch --flake /opt/services/nixos#cloud-pc"
