|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# SPDX-FileCopyrightText: 2022 The Standard Authors |
| 4 | +# SPDX-License-Identifier: Unlicense |
| 5 | + |
| 6 | +# shellcheck disable=SC2016 |
| 7 | +: ' |
| 8 | +USAGE: |
| 9 | +
|
| 10 | +Example (.envrc): |
| 11 | + source $(fetchurl https://raw.githubusercontent.com/paisano-nix/direnv/main/lib <hash>) |
| 12 | +
|
| 13 | +Find hash: |
| 14 | + direnv fetchurl https://raw.githubusercontent.com/paisano-nix/direnv/main/lib |
| 15 | +' |
| 16 | + |
| 17 | +# signature direnv & nixago output styling |
| 18 | +export DIRENV_LOG_FORMAT=$'\E[mdirenv: \E[38;5;8m%s\E[m' |
| 19 | + |
| 20 | +{ # PRJ Base Directory Specification: https://github.com/numtide/prj-spec |
| 21 | + # shellcheck source=/dev/null |
| 22 | + source "$( |
| 23 | + fetchurl \ |
| 24 | + "https://raw.githubusercontent.com/numtide/prj-spec/cdceb6106317b3fdaef9b63c54bba64e91076749/contrib/direnv" \ |
| 25 | + "sha256-df002uJaXkkBCKsXx5iUddh50h/+4UCV1M044KpCG58=" |
| 26 | + )" |
| 27 | + |
| 28 | + # numtide/devshell compat |
| 29 | + PRJ_DATA_DIR="${PRJ_DATA_HOME}" |
| 30 | + export PRJ_DATA_DIR |
| 31 | +} |
| 32 | + |
| 33 | +{ # Paisano Block |
| 34 | + |
| 35 | + registry="git+file://$PRJ_ROOT#__std" |
| 36 | + nix=("nix") |
| 37 | + nix_eval_args=( |
| 38 | + "--no-update-lock-file" |
| 39 | + "--no-write-lock-file" |
| 40 | + "--no-warn-dirty" |
| 41 | + "--accept-flake-config" |
| 42 | + ) |
| 43 | + nix_eval=("${nix[@]}" "eval" "${nix_eval_args[@]}") |
| 44 | + nix_build_args=( |
| 45 | + "--no-link" |
| 46 | + "--keep-outputs" |
| 47 | + "--build-poll-interval" "0" |
| 48 | + "--builders-use-substitutes" |
| 49 | + ) |
| 50 | + nix_build=("${nix[@]}" "build" "${nix_eval_args[@]}" "${nix_build_args[@]}") |
| 51 | + |
| 52 | + system="$("${nix_eval[@]}" 2>/dev/null --raw --impure --expr builtins.currentSystem)" |
| 53 | + cellsFrom="$("${nix_eval[@]}" 2>/dev/null --raw "$registry.cellsFrom")" |
| 54 | + |
| 55 | + # Usage: use env <target> |
| 56 | + # |
| 57 | + # Loads the environment determined by the given target |
| 58 | + # |
| 59 | + # use env //local/env/default |
| 60 | + use_env() { |
| 61 | + local input="$1" |
| 62 | + read -r cell block target <<<"${input//\// }" |
| 63 | + |
| 64 | + action="enter" |
| 65 | + |
| 66 | + local profile_path="${PRJ_STATE_HOME}/$cell/$block/$target" |
| 67 | + mkdir -p "${PRJ_STATE_HOME}/$cell/$block/$target" |
| 68 | + |
| 69 | + cmd=( |
| 70 | + "${nix_build[@]}" |
| 71 | + "--print-out-paths" |
| 72 | + "--profile" |
| 73 | + "$profile_path/enter-action" |
| 74 | + "$registry.actions.$system.$cell.$block.$target.$action" |
| 75 | + ) |
| 76 | + |
| 77 | + if ! enter="$("${cmd[@]}")"; then |
| 78 | + log_status "The follwing command just failed:" |
| 79 | + log_status "${cmd[@]}" |
| 80 | + read -rp "Rerun with --show-trace (y/N): " diagnose |
| 81 | + [[ "$diagnose" == "y" ]] && "${cmd[@]}" --show-trace |
| 82 | + exit 1 |
| 83 | + fi |
| 84 | + |
| 85 | + export STD_DIRENV=1 |
| 86 | + eval "$(<"$enter")" |
| 87 | + # this is not true |
| 88 | + unset IN_NIX_SHELL STD_DIRENV |
| 89 | + } |
| 90 | + |
| 91 | + _set_watchers() { |
| 92 | + local input="$1" |
| 93 | + read -r cell block target <<<"${input//\// }" |
| 94 | + local BLOCK_PATH="$cellsFrom/$cell/$block" |
| 95 | + local watchableFile= |
| 96 | + local watchableDir= |
| 97 | + |
| 98 | + if [[ -f "$BLOCK_PATH/$target.nix" ]]; then |
| 99 | + watchableFile="$BLOCK_PATH/$target.nix" |
| 100 | + elif [[ -f "$BLOCK_PATH/default.nix" ]]; then |
| 101 | + watchableFile="$BLOCK_PATH/default.nix" |
| 102 | + elif [[ -f "$BLOCK_PATH.nix" ]]; then |
| 103 | + watchableFile="$BLOCK_PATH.nix" |
| 104 | + fi |
| 105 | + |
| 106 | + if [[ -d "$BLOCK_PATH/$target" ]]; then |
| 107 | + watchableDir="$BLOCK_PATH/$target" |
| 108 | + fi |
| 109 | + |
| 110 | + if [[ -n $watchableFile ]]; then |
| 111 | + log_status "Watching: ${watchableFile}" |
| 112 | + watch_file "$watchableFile" |
| 113 | + fi |
| 114 | + if [[ -n $watchableDir ]]; then |
| 115 | + log_status "Watching: ${watchableDir} (recursively)" |
| 116 | + watch_dir "$watchableDir" |
| 117 | + fi |
| 118 | + |
| 119 | + } |
| 120 | + |
| 121 | + use_envreload() { |
| 122 | + for target in "${@}"; do |
| 123 | + _set_watchers "$target" |
| 124 | + done |
| 125 | + use_env "$1" |
| 126 | + } |
| 127 | +} |
0 commit comments