|
1 | 1 | # shellcheck shell=bash
|
2 | 2 |
|
| 3 | +_nix_import_env() { |
| 4 | + local env=$1 |
| 5 | + |
| 6 | + local term_backup=$TERM path_backup=$PATH |
| 7 | + if [[ -n ${TMPDIR+x} ]]; then |
| 8 | + local tmp_backup=$TMPDIR |
| 9 | + fi |
| 10 | + local impure_ssl_cert_file=${SSL_CERT_FILE:-__UNSET__} |
| 11 | + local impure_nix_ssl_cert_file=${NIX_SSL_CERT_FILE:-__UNSET__} |
| 12 | + |
| 13 | + eval "$env" |
| 14 | + |
| 15 | + # `nix-shell --pure` sets invalid ssl certificate paths |
| 16 | + if [[ "${SSL_CERT_FILE:-}" = /no-cert-file.crt ]]; then |
| 17 | + if [[ $impure_ssl_cert_file == __UNSET__ ]]; then |
| 18 | + unset SSL_CERT_FILE |
| 19 | + else |
| 20 | + export SSL_CERT_FILE=$impure_nix_ssl_cert_file |
| 21 | + fi |
| 22 | + fi |
| 23 | + |
| 24 | + if [[ "${NIX_SSL_CERT_FILE:-}" = /no-cert-file.crt ]]; then |
| 25 | + if [[ $impure_nix_ssl_cert_file == __UNSET__ ]]; then |
| 26 | + unset NIX_SSL_CERT_FILE |
| 27 | + else |
| 28 | + export NIX_SSL_CERT_FILE=${impure_ssl_cert_file} |
| 29 | + fi |
| 30 | + fi |
| 31 | + |
| 32 | + export PATH=$PATH:$path_backup TERM=$term_backup |
| 33 | + if [[ -n ${tmp_backup+x} ]]; then |
| 34 | + export TMPDIR=${tmp_backup} |
| 35 | + else |
| 36 | + unset TMPDIR |
| 37 | + fi |
| 38 | + |
| 39 | + # misleading since we are in an impure shell now |
| 40 | + export IN_NIX_SHELL=impure |
| 41 | +} |
| 42 | + |
| 43 | +_nix_add_gcroot() { |
| 44 | + local storepath=$1 |
| 45 | + local symlink=$2 |
| 46 | + |
| 47 | + local stripped_pwd=${PWD/\//} |
| 48 | + local escaped_pwd=${stripped_pwd//-/--} |
| 49 | + local escaped_pwd=${escaped_pwd//\//-} |
| 50 | + ln -fs "$storepath" "$symlink" |
| 51 | + ln -fs "$symlink" "/nix/var/nix/gcroots/per-user/$USER/$escaped_pwd" |
| 52 | +} |
| 53 | + |
| 54 | +use_flake() { |
| 55 | + watch_file flake.nix |
| 56 | + watch_file flake.lock |
| 57 | + |
| 58 | + local profile="$(direnv_layout_dir)/flake-profile" |
| 59 | + local profile_rc="${profile}.rc" |
| 60 | + |
| 61 | + if [[ ! -e "$profile" ]] || \ |
| 62 | + [[ ! -e "$profile_rc" ]] || \ |
| 63 | + [[ "$HOME/.direnvrc" -nt "$profile_rc" ]] || \ |
| 64 | + [[ .envrc -nt "$profile_rc" ]] || \ |
| 65 | + [[ flake.nix -nt "$profile_rc" ]] || \ |
| 66 | + [[ flake.lock -nt "$profile_rc" ]]; |
| 67 | + then |
| 68 | + local tmp_profile="$(direnv_layout_dir)/flake-profile.$$" |
| 69 | + [[ -d "$(direnv_layout_dir)" ]] || mkdir "$(direnv_layout_dir)" |
| 70 | + local tmp_profile_rc=$(nix print-dev-env --profile "$tmp_profile") |
| 71 | + drv=$(realpath "$tmp_profile") |
| 72 | + echo "$tmp_profile_rc" > "$profile_rc" |
| 73 | + rm -f "$tmp_profile" "$tmp_profile"* |
| 74 | + _nix_add_gcroot "$drv" "$profile" |
| 75 | + log_status renewed cache |
| 76 | + else |
| 77 | + log_status using cached dev shell |
| 78 | + fi |
| 79 | + |
| 80 | + local old_nix_build_top=${NIX_BUILD_TOP:-__UNSET__} |
| 81 | + local old_tmp=${TMP:-__UNSET__} |
| 82 | + local old_tmpdir=${TMPDIR:-__UNSET__} |
| 83 | + local old_temp=${TEMP:-__UNSET__} |
| 84 | + local old_tempdir=${TEMPDIR:-__UNSET__} |
| 85 | + eval "$(< "$profile_rc")" |
| 86 | + # nix print-env-dev will create a temporary directory and use it a TMPDIR, |
| 87 | + # we cannot rely on this directory beeing not deleted at some point, |
| 88 | + # hence we are just removing it right away. |
| 89 | + if [[ "$NIX_BUILD_TOP" == */nix-shell.* && -d "$NIX_BUILD_TOP" ]]; then |
| 90 | + rmdir "$NIX_BUILD_TOP" |
| 91 | + fi |
| 92 | + |
| 93 | + if [[ "$old_nix_build_top" == __UNSET__ ]]; then |
| 94 | + unset NIX_BUILD_TOP |
| 95 | + else |
| 96 | + export NIX_BUILD_TOP=$old_nix_build_top |
| 97 | + fi |
| 98 | + if [[ "$old_tmp" == __UNSET__ ]]; then |
| 99 | + unset TMP |
| 100 | + else |
| 101 | + export TMP=$old_temp |
| 102 | + fi |
| 103 | + if [[ "$old_tmpdir" == __UNSET__ ]]; then |
| 104 | + unset TMPDIR |
| 105 | + else |
| 106 | + export TMPDIR=$old_tmpdir |
| 107 | + fi |
| 108 | + if [[ "$old_temp" == __UNSET__ ]]; then |
| 109 | + unset TEMP |
| 110 | + else |
| 111 | + export TEMP=$old_temp |
| 112 | + fi |
| 113 | + if [[ "$old_tempdir" == __UNSET__ ]]; then |
| 114 | + unset TEMPDIR |
| 115 | + else |
| 116 | + export TEMPDIR=$old_tempdir |
| 117 | + fi |
| 118 | +} |
| 119 | + |
3 | 120 | use_nix() {
|
4 | 121 | local path direnv_dir
|
5 | 122 | path=$(nix-instantiate --find-file nixpkgs)
|
@@ -42,57 +159,20 @@ use_nix() {
|
42 | 159 | else
|
43 | 160 | log_status using cached derivation
|
44 | 161 | fi
|
45 |
| - local term_backup=$TERM path_backup=$PATH |
46 |
| - if [[ -n ${TMPDIR+x} ]]; then |
47 |
| - local tmp_backup=$TMPDIR |
48 |
| - fi |
49 |
| - |
50 |
| - local impure_ssl_cert_file=${SSL_CERT_FILE:-__UNSET__} |
51 |
| - local impure_nix_ssl_cert_file=${NIX_SSL_CERT_FILE:-__UNSET__} |
52 | 162 |
|
53 | 163 | log_status eval "$cache"
|
54 | 164 | read -r cache_content < "$cache"
|
55 |
| - eval "$cache_content" |
56 |
| - export PATH=$PATH:$path_backup TERM=$term_backup |
57 |
| - if [[ -n ${tmp_backup+x} ]]; then |
58 |
| - export TMPDIR=${tmp_backup} |
59 |
| - else |
60 |
| - unset TMPDIR |
61 |
| - fi |
62 |
| - |
63 |
| - # `nix-shell --pure` sets IN_NIX_SHELL to pure which is |
64 |
| - # misleading since we are in an impure shell now |
65 |
| - export IN_NIX_SHELL=impure |
66 |
| - |
67 |
| - # `nix-shell --pure` sets invalid ssl certificate paths |
68 |
| - if [[ "${SSL_CERT_FILE:-}" = /no-cert-file.crt ]]; then |
69 |
| - if [[ ${impure_ssl_cert_file} == __UNSET__ ]]; then |
70 |
| - unset SSL_CERT_FILE |
71 |
| - else |
72 |
| - export SSL_CERT_FILE=${impure_ssl_cert_file} |
73 |
| - fi |
74 |
| - fi |
75 |
| - if [[ "${NIX_SSL_CERT_FILE:-}" = /no-cert-file.crt ]]; then |
76 |
| - if [[ ${impure_nix_ssl_cert_file} == __UNSET__ ]]; then |
77 |
| - unset NIX_SSL_CERT_FILE |
78 |
| - else |
79 |
| - export NIX_SSL_CERT_FILE=${impure_nix_ssl_cert_file} |
80 |
| - fi |
81 |
| - fi |
| 165 | + _nix_import_env "$cache_content" |
82 | 166 |
|
83 | 167 | # This part is based on https://discourse.nixos.org/t/what-is-the-best-dev-workflow-around-nix-shell/418/4
|
84 | 168 | if [[ "${out:-}" != "" ]] && (( update_drv )); then
|
85 | 169 | local drv_link="${direnv_dir}/drv" drv
|
86 | 170 | drv=$(nix show-derivation "$out" | grep -E -o -m1 '/nix/store/.*.drv')
|
87 |
| - local stripped_pwd=${PWD/\//} |
88 |
| - local escaped_pwd=${stripped_pwd//-/--} |
89 |
| - local escaped_pwd=${escaped_pwd//\//-} |
90 |
| - ln -fs "$drv" "$drv_link" |
91 |
| - ln -fs "$drv_link" "/nix/var/nix/gcroots/per-user/$USER/$escaped_pwd" |
| 171 | + _nix_add_gcroot "$drv" "$drv_link" |
92 | 172 | log_status renewed cache and derivation link
|
93 | 173 | fi
|
94 | 174 |
|
95 |
| - if [[ $# = 0 ]]; then |
| 175 | + if [[ "$#" == 0 ]]; then |
96 | 176 | watch_file default.nix
|
97 | 177 | watch_file shell.nix
|
98 | 178 | fi
|
|
0 commit comments