Update to work with latest NixOS

This commit is contained in:
Konstantin Nazarov 2024-08-19 23:52:14 +01:00
parent 4db7f63b04
commit e1b69b417b
Signed by: knazarov
GPG key ID: 4CFE0A42FA409C22
3 changed files with 154 additions and 148 deletions

View file

@ -2,11 +2,11 @@
"nodes": { "nodes": {
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1684076341, "lastModified": 1724104367,
"narHash": "sha256-60pNoKP2opEnGoPGrVo43Vt433El4+BcSl0TS36qJgQ=", "narHash": "sha256-ZYT8DwJKW6UTUm1F0kSluOGNrpmCrwEWruhtmWDhiNM=",
"owner": "NixOS", "owner": "NixOS",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "1feac2b61756a5a4392a6cc5f5b510325312437d", "rev": "dfd48cea67e78574522201471bccca03383e9987",
"type": "github" "type": "github"
}, },
"original": { "original": {

View file

@ -22,7 +22,7 @@
pkgs = import nixpkgs { inherit system; }; pkgs = import nixpkgs { inherit system; };
}); });
make_package = pkgs: pkgs.stdenv.mkDerivation { make_package = pkgs: pkgs.stdenv.mkDerivation rec {
name = "q-sh"; name = "q-sh";
src = self; src = self;
installPhase = '' installPhase = ''
@ -32,28 +32,34 @@
echo "OUT " $out echo "OUT " $out
''; '';
dontStrip = true;
nativeBuildInputs = [pkgs.makeWrapper];
wrapperPath = pkgs.lib.makeBinPath ( with pkgs; [
fzf
findutils
gnused
gawk
bc
pass
coreutils
wl-clipboard
xdg-utils
]
);
postFixup = '' postFixup = ''
q-fixup() { substituteInPlace $out/bin/q \
TOOL="$1" --replace 'ENV_q="''${0##*/}"' "ENV_q=$out/bin/q"
PATH="$2"
${pkgs.findutils}/bin/find $out -type f -name 'q*' -exec ${pkgs.gnused}/bin/sed -i "s#ENV_$TOOL=.*#ENV_$TOOL=$PATH#g" {} + # Ensure all dependencies are in PATH
} for fn in $out/bin/q*; do
q-fixup "fzf" "${pkgs.fzf}/bin/fzf" echo "wrapping $fn"
q-fixup "find" "${pkgs.findutils}/bin/find" wrapProgram $fn \
q-fixup "sed" "${pkgs.gnused}/bin/sed" --prefix PATH : "${wrapperPath}"
q-fixup "awk" "${pkgs.gawk}/bin/awk" done
q-fixup "bc" "${pkgs.bc}/bin/bc" '';
q-fixup "pass" "${pkgs.pass}/bin/pass"
q-fixup "cut" "${pkgs.coreutils}/bin/cut"
q-fixup "base64" "${pkgs.coreutils}/bin/base64"
q-fixup "cat" "${pkgs.coreutils}/bin/cat"
q-fixup "tac" "${pkgs.coreutils}/bin/tac"
q-fixup "sort" "${pkgs.coreutils}/bin/sort"
q-fixup "uniq" "${pkgs.coreutils}/bin/uniq"
q-fixup "wl_copy" "${pkgs.wl-clipboard}/bin/wl-copy"
q-fixup "xdg_open" "${pkgs.xdg-utils}/bin/xdg-open"
q-fixup "q" "$out/bin"
'';
}; };
in in
{ {

246
q
View file

@ -36,179 +36,179 @@ ENV_q=~/.config/q.sh
SELF=$(cd -P -- "$(dirname -- "$0")" && printf '%s\n' "$(pwd -P)/$(basename -- "$0")") SELF=$(cd -P -- "$(dirname -- "$0")" && printf '%s\n' "$(pwd -P)/$(basename -- "$0")")
if [[ -z "$Q_SCRIPT_DIR" ]]; then if [[ -z "$Q_SCRIPT_DIR" ]]; then
Q_SCRIPT_DIR="$ENV_q" Q_SCRIPT_DIR="$(dirname -- $SELF)"
fi fi
INDEX_FILE=~/.q.index INDEX_FILE=~/.q.index
HINT_FILE=~/.q.hints HINT_FILE=~/.q.hints
new_scripts_exist() { new_scripts_exist() {
if [[ ! -d "$Q_SCRIPT_DIR" ]]; then if [[ ! -d "$Q_SCRIPT_DIR" ]]; then
return 0 return 0
fi fi
if [[ ! -s "$INDEX_FILE" ]]; then if [[ ! -s "$INDEX_FILE" ]]; then
return 0 return 0
fi fi
NEWER_FILES="$($ENV_find "$Q_SCRIPT_DIR" -name "q-*" -newer "$INDEX_FILE")" NEWER_FILES="$($ENV_find "$Q_SCRIPT_DIR" -name "q-*" -newer "$INDEX_FILE")"
if [[ -z "$NEWER_FILES" ]]; then if [[ -z "$NEWER_FILES" ]]; then
return 1 return 1
fi fi
return 0 return 0
} }
rebuild_index() { rebuild_index() {
if [[ ! -d "$Q_SCRIPT_DIR" ]]; then if [[ ! -d "$Q_SCRIPT_DIR" ]]; then
return return
fi fi
pushd "$Q_SCRIPT_DIR" >/dev/null pushd "$Q_SCRIPT_DIR" >/dev/null
$ENV_find . -type f -name 'q-*' | while read -r FN $ENV_find . -type f -name 'q-*' | while read -r FN
do do
FILENAME="$(echo "$FN" | sed 's/^\.\///g')" FILENAME="$(echo "$FN" | sed 's/^\.\///g')"
"$Q_SCRIPT_DIR/$FILENAME" | while read -r line "$Q_SCRIPT_DIR/$FILENAME" | while read -r line
do do
echo "/$line/ {print \"$FILENAME\"}" echo "/$line/ {print \"$FILENAME\"}"
done done
done done
popd > /dev/null popd > /dev/null
} }
rebuild_hints() { rebuild_hints() {
if [[ ! -d "$Q_SCRIPT_DIR" ]]; then if [[ ! -d "$Q_SCRIPT_DIR" ]]; then
return return
fi fi
pushd "$Q_SCRIPT_DIR" >/dev/null pushd "$Q_SCRIPT_DIR" >/dev/null
$ENV_find . -type f -name 'q-*' | while read -r FN $ENV_find . -type f -name 'q-*' | while read -r FN
do do
FILENAME="$(echo "$FN" | sed 's/^\.\///g')" FILENAME="$(echo "$FN" | sed 's/^\.\///g')"
"$Q_SCRIPT_DIR/$FILENAME" --hint "$Q_SCRIPT_DIR/$FILENAME" --hint
done done
popd > /dev/null popd > /dev/null
} }
get_scripts_for_cmd() { get_scripts_for_cmd() {
if [[ ! -f "$INDEX_FILE" ]]; then if [[ ! -f "$INDEX_FILE" ]]; then
return return
fi fi
echo "$1" | $ENV_awk -f "$INDEX_FILE" | while read -r line echo "$1" | $ENV_awk -f "$INDEX_FILE" | while read -r line
do do
echo "$line" echo "$line"
done done
} }
complete_command() { complete_command() {
COMMAND="$@" COMMAND="$@"
SCRIPTS="$(get_scripts_for_cmd "$COMMAND")" SCRIPTS="$(get_scripts_for_cmd "$COMMAND")"
if [ -z "$SCRIPTS" ] || \ if [ -z "$SCRIPTS" ] || \
[ "$(echo "$SCRIPTS" | wc -w)" -gt "1" ]; then [ "$(echo "$SCRIPTS" | wc -w)" -gt "1" ]; then
cat "$HINT_FILE" cat "$HINT_FILE"
exit 0 exit 0
fi fi
"$Q_SCRIPT_DIR/$SCRIPTS" --complete "$@" "$Q_SCRIPT_DIR/$SCRIPTS" --complete "$@"
} }
run_command() { run_command() {
COMMAND="$@" COMMAND="$@"
FIRST=${COMMAND%%" "*} FIRST=${COMMAND%%" "*}
REST=${COMMAND#*" "} REST=${COMMAND#*" "}
SCRIPTS="$(get_scripts_for_cmd "$REST")" SCRIPTS="$(get_scripts_for_cmd "$REST")"
if [ -z "$SCRIPTS" ] || \ if [ -z "$SCRIPTS" ] || \
[ "$(echo "$SCRIPTS" | wc -w)" -gt "1" ]; then [ "$(echo "$SCRIPTS" | wc -w)" -gt "1" ]; then
echo "Scripts matched: $SCRIPTS" echo "Scripts matched: $SCRIPTS"
exit 0 exit 0
fi fi
"$Q_SCRIPT_DIR/$SCRIPTS" --run "$FIRST" "$REST" "$Q_SCRIPT_DIR/$SCRIPTS" --run "$FIRST" "$REST"
} }
run_command_cli() { run_command_cli() {
COMMAND="$@" COMMAND="$@"
SCRIPTS="$(get_scripts_for_cmd "$COMMAND")" SCRIPTS="$(get_scripts_for_cmd "$COMMAND")"
if [ -z "$SCRIPTS" ] || \ if [ -z "$SCRIPTS" ] || \
[ "$(echo "$SCRIPTS" | wc -w)" -gt "1" ]; then [ "$(echo "$SCRIPTS" | wc -w)" -gt "1" ]; then
echo "Scripts matched: $SCRIPTS" echo "Scripts matched: $SCRIPTS"
exit 0 exit 0
fi fi
"$Q_SCRIPT_DIR/$SCRIPTS" --cli "$COMMAND" "$Q_SCRIPT_DIR/$SCRIPTS" --cli "$COMMAND"
} }
preview_command() { preview_command() {
COMMAND="$@" COMMAND="$@"
FIRST=${COMMAND%%" "*} FIRST=${COMMAND%%" "*}
REST=${COMMAND#*" "} REST=${COMMAND#*" "}
SCRIPTS="$(get_scripts_for_cmd "$REST")" SCRIPTS="$(get_scripts_for_cmd "$REST")"
if [ -z "$SCRIPTS" ] || \ if [ -z "$SCRIPTS" ] || \
[ "$(echo "$SCRIPTS" | wc -w)" -gt "1" ]; then [ "$(echo "$SCRIPTS" | wc -w)" -gt "1" ]; then
echo "Scripts matched: $SCRIPTS" echo "Scripts matched: $SCRIPTS"
exit 0 exit 0
fi fi
"$Q_SCRIPT_DIR/$SCRIPTS" --preview "$FIRST" "$REST" "$Q_SCRIPT_DIR/$SCRIPTS" --preview "$FIRST" "$REST"
} }
if [[ ! -f "$INDEX_FILE" ]] || new_scripts_exist; then if [[ ! -f "$INDEX_FILE" ]] || new_scripts_exist; then
rebuild_index > "$INDEX_FILE" rebuild_index > "$INDEX_FILE"
rebuild_hints > "$HINT_FILE" rebuild_hints > "$HINT_FILE"
fi fi
while (( "$#" )); do while (( "$#" )); do
case "$1" in case "$1" in
-c|--complete) -c|--complete)
shift shift
complete_command "$@" complete_command "$@"
exit 0 exit 0
;; ;;
-r|--run) -r|--run)
shift shift
if [[ -z "$@" ]]; then if [[ -z "$@" ]]; then
exit 0 exit 0
fi fi
run_command "$@" run_command "$@"
exit 0 exit 0
;; ;;
-p|--preview) -p|--preview)
shift shift
if [[ -z "$@" ]]; then if [[ -z "$@" ]]; then
exit 0 exit 0
fi fi
preview_command "$@" preview_command "$@"
exit 0 exit 0
;; ;;
-u|--update) -u|--update)
shift shift
rebuild_index > "$INDEX_FILE" rebuild_index > "$INDEX_FILE"
rebuild_hints > "$HINT_FILE" rebuild_hints > "$HINT_FILE"
exit 0 exit 0
;; ;;
*) *)
break break
;; ;;
esac esac
done done
COMMAND="$@" COMMAND="$@"
if [[ -z "$COMMAND" ]] && [[ ! -t 0 ]]; then if [[ -z "$COMMAND" ]] && [[ ! -t 0 ]]; then
COMMAND="$(cat)" COMMAND="$(cat)"
fi fi
if [[ -z "$COMMAND" ]]; then if [[ -z "$COMMAND" ]]; then
PREF="$SELF -c" PREF="$SELF -c"
INITIAL="" INITIAL=""
FZF_DEFAULT_COMMAND="$PREF '$INITIAL'" $ENV_fzf \ FZF_DEFAULT_COMMAND="$PREF '$INITIAL'" $ENV_fzf \
--bind "change:reload-sync:$PREF {q} || true" \ --bind "change:reload-sync:$PREF {q} || true" \
--ansi --query "$INITIAL" \ --ansi --query "$INITIAL" \
--preview "$SELF -p {}" \ --preview "$SELF -p {}" \
--with-nth="2..-1" \ --with-nth="2..-1" \
--preview-window wrap \ --preview-window wrap \
--tiebreak=index | xargs -o "$SELF" -r --tiebreak=index | xargs -o "$SELF" -r
else else
run_command_cli "$COMMAND" run_command_cli "$COMMAND"
fi fi