nix-conf/user/bin/edit

53 lines
1.1 KiB
Plaintext
Raw Normal View History

2024-10-23 23:09:08 -04:00
#!/bin/sh
if [ "$#" -ne 1 ]; then
2024-12-11 05:17:37 -05:00
echo "usage: ${0##*/} <file>"
2024-10-23 23:09:08 -04:00
exit 1
fi
first_link () {
if [ "${1%/*}" != "$1" ]; then
local x="$(first_link "${1%/*}")"
fi
if [ -z "$x" ] && [ -L "$1" ]; then
echo "$1"
else
echo "$x"
fi
}
NIX="$HOME/.local/nixos/user"
FIRST="$(first_link "$(realpath -s "$1")")"
if ! [ -z "$FIRST" ]; then
LINKED="$(realpath "$FIRST")"
if [ "${LINKED#/nix/store/}" != "$LINKED" ]; then
CONFIG="${FIRST#~/.config/}"
if [ "$CONFIG" != "$FIRST" ] && [ -e "$NIX/config/$CONFIG" ]; then
rm "$FIRST"
ln -s "$NIX/config/$CONFIG" "$FIRST"
$EDITOR "$1"
exit
fi
BIN="${FIRST#~/.local/bin}"
if [ "$BIN" != "$FIRST" ] && [ -e "$NIX/bin/$BIN" ]; then
rm "$FIRST"
ln -s "$NIX/bin/$BIN" "$FIRST"
$EDITOR "$1"
exit
fi
2024-11-12 03:25:31 -05:00
SEARCH="$(fd "$CONFIG" "$NIX" -p1)"
if [ -n "$SEARCH" ]; then
rm "$FIRST"
ln -s "$SEARCH" "$FIRST"
$EDITOR "$1"
exit
fi
2024-10-23 23:09:08 -04:00
echo "not symlinkable"
exit 1
fi
fi
if ! [ -e "$1" ]; then
DIR="${1%/*}"
[ "$DIR" != "$1" ] && mkdir -p "$DIR"
fi
$EDITOR "$1"