name: "Update nixpkgs"
on:
  schedule:
    - cron: "0 12 * * *"
  workflow_dispatch:
jobs:
  update-nixpkgs:
    name: Update nixpkgs (${{ matrix.branch }})
    runs-on: ubuntu-latest
    continue-on-error: true
    strategy:
      fail-fast: false
      matrix:
        include:
          - branch: master-custom
            upstream-branch: master
          - branch: unstable-custom
            upstream-branch: nixos-unstable
    steps:
      - uses: actions/checkout@v4
        with:
          repository: lopsided98/nixpkgs
          ref: ${{ matrix.branch }}
          filter: tree:0
          fetch-depth: 0
          token: ${{ secrets.NIXPKGS_GITHUB_TOKEN }}
      - name: Configure git user
        run: |
          git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
          git config --global user.name "github-actions[bot]"
      - name: Update nixpkgs
        run: |
          git remote add upstream https://github.com/NixOS/nixpkgs.git
          git fetch --filter=tree:0 upstream ${{ matrix.upstream-branch }}
          git rebase upstream/${{ matrix.upstream-branch }}
          git push --force-with-lease
  update-flake:
    name: Update flake inputs
    runs-on: ubuntu-latest
    needs: update-nixpkgs
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v4
        with:
          filter: tree:0
          fetch-depth: 0
      - uses: cachix/install-nix-action@v22
        with:
          github_access_token: ${{ secrets.GITHUB_TOKEN }}
      - name: Configure git user
        run: |
          git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
          git config --global user.name "github-actions[bot]"
      - name: Update flake inputs
        run: |
          nix flake lock \
            --update-input nixpkgs-master-custom \
            --update-input nixpkgs-unstable-custom \
            --commit-lock-file
          git push
