#!/usr/bin/env bash
set -euf -o pipefail
repo_root="$(cd -- "$(dirname "$0")/../.." && pwd -P)"
work_dir="${WORK_DIR:-$repo_root}"
set -v

cache_dir="${NIX_STORE_CACHE_DIR:-"$work_dir/.ci/.cache/nix-store"}"

if [[ -e "$cache_dir" ]]; then
  nix copy \
    --from "$cache_dir" \
    --all \
    --derivation \
    --no-check-sigs
fi

nix copy \
  --to "$cache_dir" \
  --no-check-sigs  \
  .#devShells.x86_64-linux.default.inputDerivation
nix flake archive \
  --to "$cache_dir" \
  --no-use-registries --no-update-lock-file
