diff --git a/dotfiles/lib/functions/add_to_path b/dotfiles/lib/functions/add_to_path new file mode 100755 index 00000000..7053bb82 --- /dev/null +++ b/dotfiles/lib/functions/add_to_path @@ -0,0 +1,7 @@ +#!/usr/bin/env sh + +function add_to_path { + path_lines "$2" | { cat -; echo "$1" } | path_from_lines +} + +add_to_path "$@" diff --git a/dotfiles/lib/functions/join_by_char b/dotfiles/lib/functions/join_by_char new file mode 100755 index 00000000..5490f0d8 --- /dev/null +++ b/dotfiles/lib/functions/join_by_char @@ -0,0 +1,22 @@ +#!/usr/bin/env sh + +function join_by_char { + delimiter="$1" + if [ -z "$delimiter" ]; then + echo "Please specify a delimiter." + return 1 + fi + + while IFS= read -r line; do + # Append the line to the result with the delimiter, but not for the first line + if [ -z "$result" ]; then + result="$line" + else + result="$result$delimiter$line" + fi + done + + echo "$result" +} + +join_by_char "$@" diff --git a/dotfiles/lib/functions/nixpkgs_source b/dotfiles/lib/functions/nixpkgs_source new file mode 100755 index 00000000..02b5e4d0 --- /dev/null +++ b/dotfiles/lib/functions/nixpkgs_source @@ -0,0 +1,7 @@ +#!/usr/bin/env sh + +function nixpkgs_source { + nix eval --expr '' --impure +} + +nixpkgs_source diff --git a/dotfiles/lib/functions/path_from_lines b/dotfiles/lib/functions/path_from_lines new file mode 100755 index 00000000..776a055c --- /dev/null +++ b/dotfiles/lib/functions/path_from_lines @@ -0,0 +1,7 @@ +#!/usr/bin/env sh + +function path_from_lines { + join_by_char ":" +} + +path_from_lines "$@" diff --git a/dotfiles/lib/functions/path_lines b/dotfiles/lib/functions/path_lines index 03321e1f..bca39da6 100755 --- a/dotfiles/lib/functions/path_lines +++ b/dotfiles/lib/functions/path_lines @@ -1,7 +1,8 @@ #!/usr/bin/env sh function path_lines { - split_by_char ":" "$PATH" + var="${1:-PATH}" + split_by_char ":" "${!var}" } -path_lines +path_lines "$@"