stages:
- devour
- generate
- packages
- cache

x86_64-linux:devour:
  stage: devour
  script:
  - nix build github:srid/devour-flake#packages.x86_64-linux.default -L --no-link --print-out-paths --override-input flake . --keep-going

aarch64-darwin:devour:
  stage: devour
  script:
  - nix build github:srid/devour-flake#packages.aarch64-darwin.default -L --no-link --print-out-paths --override-input flake . --keep-going

x86_64-linux:generate:
  stage: generate
  needs: ["x86_64-linux:devour"]
  script:
  - nix run "git+https://gitlab.horizon-haskell.net/haskell/flake-to-gitlab-ci?ref=refs/tags/0.2.5.0" > flake-ci.yml
  when: on_failure
  artifacts:
    paths:
      - flake-ci.yml

x86_64-linux:packages:
  stage: packages
  needs: ["x86_64-linux:devour", "x86_64-linux:generate"]
  when: on_failure
  trigger:
    include:
      - artifact: flake-ci.yml
        job: x86_64-linux:generate
    strategy: depend


aarch64-darwin:generate:
  stage: generate
  needs: ["aarch64-darwin:devour"]
  script:
  - nix run "git+https://gitlab.horizon-haskell.net/haskell/flake-to-gitlab-ci?ref=refs/tags/0.2.5.0" -- aarch64-darwin > flake-ci.yml
  when: on_failure
  artifacts:
    paths:
      - flake-ci.yml

aarch64-darwin:packages:
  stage: packages
  needs: ["aarch64-darwin:devour", "aarch64-darwin:generate"]
  when: on_failure
  trigger:
    include:
      - artifact: flake-ci.yml
        job: aarch64-darwin:generate
    strategy: depend

x86_64-linux:cachix:
  stage: cache
  rules:
    - if: $CI_COMMIT_REF_PROTECTED == "true"
  script:
  - nix-shell -p cachix --command "nix build github:srid/devour-flake -L --no-link --print-out-paths --override-input flake . | xargs cat | cachix push horizon"

aarch64-darwin:cachix:
  stage: cache
  rules:
    - if: $CI_COMMIT_REF_PROTECTED == "true"
  script:
  - nix-shell -p cachix --command "nix build github:srid/devour-flake#packages.aarch64-darwin.default -L --no-link --print-out-paths --override-input flake . | xargs cat | cachix push horizon"
