name: Update flake inputs

on:
  workflow_dispatch:
  schedule:
    - cron: "0 0 * * 0"

jobs:
  update-flake:
    runs-on: ubuntu-latest

    steps:
      - name: Generate app token
        id: app-token
        uses: actions/create-github-app-token@v3
        with:
          client-id: ${{ secrets.CLIENT_ID }}
          private-key: ${{ secrets.APP_PRIVATE_KEY }}

      - uses: actions/checkout@v6
        with:
          token: ${{ steps.app-token.outputs.token }}

      - name: Install Nix
        uses: nixbuild/nix-quick-install-action@v34
        with:
          nix_conf: |
            keep-env-derivations = true
            keep-outputs = true

      - name: Restore and save Nix store
        uses: nix-community/cache-nix-action@v7
        with:
          # restore and save a cache using this key
          primary-key: nix-${{ hashFiles('**/*.nix', '**/flake.lock') }}
          # if there's no cache hit, restore a cache by this prefix
          restore-prefixes-first-match: nix-
          # collect garbage until the Nix store size (in bytes) is at most this number
          # before trying to save a new cache
          # 1G = 1073741824
          gc-max-store-size-linux: 1G
          # do purge caches
          purge: true
          # purge all versions of the cache
          purge-prefixes: nix-
          # created more than this number of seconds ago
          purge-created: 0
          # or, last accessed more than this number of seconds ago
          # relative to the start of the `Post Restore and save Nix store` phase
          purge-last-accessed: 0
          # except any version with the key that is the same as the `primary-key`
          purge-primary-key: never

      - name: Update flake inputs
        run: nix flake update

      - name: Attempt to build flake
        run: nix build

      - name: Test on Sway
        env:
          XDG_RUNTIME_DIR: /home/runner/runtime
          WLR_BACKENDS: headless
          WLR_LIBINPUT_NO_DEVICES: 1
          WAYLAND_DISPLAY: wayland-1
        run: |
          mkdir $XDG_RUNTIME_DIR
          chown $USER $XDG_RUNTIME_DIR
          chmod 0700 $XDG_RUNTIME_DIR

          nix profile install 'nixpkgs#sway'
          sway &
          sleep 3  # Give Sway some time to start
          result/bin/caelestia-shell -d
          sleep 3  # Give the shell some time to start (and die)
          pgrep .quickshell-wra  # Fail job if shell died

          result/bin/caelestia-shell kill
          killall sway  # Screw using IPC

      - name: Check for changes
        id: check
        run: echo modified=$(git diff --exit-code flake.lock &>/dev/null && echo 'false' || echo 'true') >> $GITHUB_OUTPUT

      - name: Commit and push changes
        if: steps.check.outputs.modified == 'true'
        uses: EndBug/add-and-commit@v10
        with:
          github_token: ${{ steps.app-token.outputs.token }}
          add: flake.lock
          default_author: github_actions
          message: "[CI] chore: update flake"
