mirror of
https://git.sr.ht/~bryan_bennett/flake_env
synced 2025-12-16 14:41:24 +01:00
68 lines
2.6 KiB
Text
68 lines
2.6 KiB
Text
# The below code is taken largely from nix-direnv.
|
|
# nix-direnv's license is replicated below:
|
|
#
|
|
# Copyright (c) 2019 Nix community projects
|
|
#
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
# of this software and associated documentation files (the "Software"), to deal
|
|
# in the Software without restriction, including without limitation the rights
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
# copies of the Software, and to permit persons to whom the Software is
|
|
# furnished to do so, subject to the following conditions:
|
|
#
|
|
# The above copyright notice and this permission notice shall be included in all
|
|
# copies or substantial portions of the Software.
|
|
#
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
# SOFTWARE.
|
|
|
|
_nix_export_or_unset() {
|
|
local key=$1 value=$2
|
|
if [[ $value == __UNSET__ ]]; then
|
|
unset "$key"
|
|
else
|
|
export "$key=$value"
|
|
fi
|
|
}
|
|
|
|
use_flake_env() {
|
|
local old_nix_build_top=${NIX_BUILD_TOP:-__UNSET__}
|
|
local old_tmp=${TMP:-__UNSET__}
|
|
local old_tmpdir=${TMPDIR:-__UNSET__}
|
|
local old_temp=${TEMP:-__UNSET__}
|
|
local old_tempdir=${TEMPDIR:-__UNSET__}
|
|
local old_xdg_data_dirs=${XDG_DATA_DIRS:-}
|
|
|
|
local ld=$(direnv_layout_dir)
|
|
eval $(flake_env "$1" "$ld")
|
|
|
|
# `nix print-dev-env` will create a temporary directory and use it as TMPDIR
|
|
# We cannot rely on this directory being available at all times,
|
|
# as it may be garbage collected.
|
|
# Instead - just remove it immediately.
|
|
# Use recursive & force as it may not be empty.
|
|
if [[ -n ${NIX_BUILD_TOP+x} && $NIX_BUILD_TOP == */nix-shell.* && -d $NIX_BUILD_TOP ]]; then
|
|
rm -rf "$NIX_BUILD_TOP"
|
|
fi
|
|
|
|
_nix_export_or_unset NIX_BUILD_TOP "$old_nix_build_top"
|
|
_nix_export_or_unset TMP "$old_tmp"
|
|
_nix_export_or_unset TMPDIR "$old_tmpdir"
|
|
_nix_export_or_unset TEMP "$old_temp"
|
|
_nix_export_or_unset TEMPDIR "$old_tempdir"
|
|
local new_xdg_data_dirs=${XDG_DATA_DIRS:-}
|
|
export XDG_DATA_DIRS=
|
|
local IFS=:
|
|
for dir in $new_xdg_data_dirs${old_xdg_data_dirs:+:}$old_xdg_data_dirs; do
|
|
dir="${dir%/}" # remove trailing slashes
|
|
if [[ :$XDG_DATA_DIRS: == *:$dir:* ]]; then
|
|
continue # already present, skip
|
|
fi
|
|
XDG_DATA_DIRS="$XDG_DATA_DIRS${XDG_DATA_DIRS:+:}$dir"
|
|
done
|
|
}
|