# shellcheck shell=bash
fallback_environment() {
  export NIX_CONF_ROOT="${NIX_CONF_ROOT:-$(pwd)}"
  PATH_add "$NIX_CONF_ROOT/scripts"
  log_status "Nix unavailable; using host tools (no reproducible toolchain loaded)"
}

watch_file flake/partitions.nix
watch_file flake/dev/flake.nix
watch_file flake/dev/formatter.nix
watch_file flake/dev/git-hooks.nix
watch_file flake/dev/shell.nix

if has nix_direnv_version; then
  use flake
elif command -v nix >/dev/null 2>&1; then
  if nix_dev_env="$(nix print-dev-env --accept-flake-config 2>/dev/null)" && eval "$nix_dev_env"; then
    log_status "Loaded reproducible dev environment with nix print-dev-env"
  else
    fallback_environment
  fi
else
  # direnv is supported on Windows through WSL/Git Bash. When Nix is not
  # installed, keep the project usable without pretending to provide tools.
  fallback_environment
fi
