Preface:
I know this is totally unecessary and probably entirely useless for most people, but...
I've recently installed NetBSD-Current on a spare disk on my Desktop machine and am slowly getting used to building everything from source. I am quite fond or the fuzzypkg script on Void linux, a wrapper for use with xbps
. So I wondered if something similar could work for pkgsrc
.
Disclaimer:
This script is currently untested and was written by ChatGPT in collaboration with Google's Gemini AI. I basically played pong with ChatGPT and Gemini until I was happy most things were covered. I have no idea if this particular script will work, but I like the idea behind it.
#!/bin/sh
# Check if fzf is installed
if ! command -v fzf >/dev/null 2>&1; then
echo "Error: fzf is required. Please install it first." >&2
exit 1
fi
# Check if bat is installed (optional, for package description previews)
if ! command -v bat >/dev/null 2>&1; then
echo "Warning: bat is not installed. Package description previews won't be available." >&2
fi
# Path to the user's pkgsrc directory (replace with your actual path)
PKGSRCDIR="$HOME/pkgsrc"
# Check if the pkgsrc directory exists
if [ ! -d "$PKGSRCDIR" ]; then
echo "Error: $PKGSRCDIR directory not found. Please create it and place your pkgsrc directory inside." >&2
exit 1
fi
# Find available packages and store them in a variable
pkgs=$(find "$PKGSRCDIR" -mindepth 2 -maxdepth 2 -type f -name 'Makefile' -exec dirname {} \; | sort)
# Use fzf for user selection with bat for previews
selected_pkg=$(fzf --preview 'cat $PKGSRCDIR/{}/DESCR' <<< "$pkgs")
# Check if a package was selected
if [ -z "$selected_pkg" ]; then
echo "No package selected"
exit 1
fi
# Change directory to the selected package
cd "$PKGSRCDIR/$selected_pkg" || exit 1
# Build and install the package
echo "Building and installing $selected_pkg..."
make install clean clean-depends distclean
Explanation:
1. Dependency Checks: The script starts by verifying if fzf
and bat
are installed. If not, it displays an error message and exits.
2. pkgsrc Directory: It defines the path to the user's pkgsrc directory. Replace $HOME/pkgsrc
with the actual location on your system.
3. Directory Existence Check: It checks if the specified pkgsrc directory exists. If not, it displays an error message and exits.
4. Finding Packages: The script uses find to search within the pkgsrc directory for directories containing a file named Makefile. It then extracts directory names and sorts them for presentation.
5. Interactive Selection: This is where the magic happens:
- The script stores the list of packages (pkgs) in a variable.
- It uses fzf
to create an interactive menu displaying the available packages.
- Instead of cat
, it uses bat
in the preview command --preview 'bat $PKGSRCDIR/{}/DESCR'
. This assumes you have bat
installed, a syntax highlighter for better code and text rendering. It displays a preview of the package DESCR
file when hovering over a package in the fzf
menu. Of course you could use cat
if you prefer.
6. Selection Validation: After user interaction, it checks if a package was actually selected. If not, it displays a message and exits.
7. Directory Change: It navigates to the directory of the chosen package within the pkgsrc directory structure.
8. Build and Install: Finally, the script uses make install clean clean-depends distclean
to build and install the selected package.
- make install
: Performs the installation process.
- clean
: Removes temporary build files after installation.
- clean-depends
: Cleans up downloaded dependency files.
- distclean
: Removes everything related to the build process, including downloaded archives.
Summary:
This script empowers you to easily search, select, build, and install packages from your pkgsrc source directory on NetBSD Current. It offers user-friendly features like fzf
for selection and leverages bat for enhanced previews (assuming it's installed) to help you choose the right package. Remember to adjust the PKGSRCDIR
path if your pkgsrc directory is located elsewhere.