From 822adb11c67901c7b0e5d1885f4f2e1202904cb8 Mon Sep 17 00:00:00 2001 From: Konstantin Nazarov Date: Sun, 14 May 2023 20:24:27 +0100 Subject: [PATCH] Adapt to Nix --- flake.lock | 26 ++++++++++++++ flake.nix | 97 ++++++++++++++++++++++++++++++++++++----------------- q | 21 +++++++----- q-bookmarks | 9 +++-- q-browse | 9 +++-- q-calc | 10 ++++-- q-clipboard | 14 +++++--- q-pass | 9 +++-- q-run | 11 +++--- 9 files changed, 147 insertions(+), 59 deletions(-) create mode 100644 flake.lock diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..4f69992 --- /dev/null +++ b/flake.lock @@ -0,0 +1,26 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1684076341, + "narHash": "sha256-60pNoKP2opEnGoPGrVo43Vt433El4+BcSl0TS36qJgQ=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "1feac2b61756a5a4392a6cc5f5b510325312437d", + "type": "github" + }, + "original": { + "owner": "NixOS", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix index eebb821..594f38c 100644 --- a/flake.nix +++ b/flake.nix @@ -1,36 +1,73 @@ { - description = "Nix q.sh dev environment"; + description = "Nix q.sh dev environment"; - # Flake inputs - inputs = { - nixpkgs.url = "github:NixOS/nixpkgs"; - }; + # Flake inputs + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs"; + }; - # Flake outputs - outputs = { self, nixpkgs }: - let - # Systems supported - allSystems = [ - "x86_64-linux" # 64-bit Intel/AMD Linux - "aarch64-linux" # 64-bit ARM Linux - "x86_64-darwin" # 64-bit Intel macOS - "aarch64-darwin" # 64-bit ARM macOS + # Flake outputs + outputs = { self, nixpkgs }: + let + # Systems supported + allSystems = [ + "x86_64-linux" # 64-bit Intel/AMD Linux + "aarch64-linux" # 64-bit ARM Linux + "x86_64-darwin" # 64-bit Intel macOS + "aarch64-darwin" # 64-bit ARM macOS + ]; + + # Helper to provide system-specific attributes + forAllSystems = f: nixpkgs.lib.genAttrs allSystems (system: f { + pkgs = import nixpkgs { inherit system; }; + }); + + make_package = pkgs: pkgs.stdenv.mkDerivation { + name = "q-sh"; + src = self; + installPhase = '' + mkdir -p $out/bin + cp q $out/bin + cp q-* $out/bin + echo "OUT " $out + ''; + + postFixup = '' + q-fixup() { + TOOL="$1" + PATH="$2" + ${pkgs.findutils}/bin/find $out -type f -name 'q*' -exec ${pkgs.gnused}/bin/sed -i "s#ENV_$TOOL=.*#ENV_$TOOL=$PATH#g" {} + + } + q-fixup "fzf" "${pkgs.fzf}/bin/fzf" + q-fixup "find" "${pkgs.findutils}/bin/find" + q-fixup "sed" "${pkgs.gnused}/bin/sed" + q-fixup "awk" "${pkgs.gawk}/bin/awk" + 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 + { + devShells = forAllSystems ({ pkgs }: { + default = pkgs.mkShell { + packages = with pkgs; [ + fzf + pass + bc + wl-clipboard + xdg-utils ]; - - # Helper to provide system-specific attributes - forAllSystems = f: nixpkgs.lib.genAttrs allSystems (system: f { - pkgs = import nixpkgs { inherit system; }; - }); - in - { - # Development environment output - devShells = forAllSystems ({ pkgs }: { - default = pkgs.mkShell { - # The Nix packages provided in the environment - packages = with pkgs; [ - fzf - ]; - }; - }); }; + }); + packages = forAllSystems({ pkgs }: {"q-sh" = make_package pkgs;}); + }; } diff --git a/q b/q index 95f87f2..4c74fec 100755 --- a/q +++ b/q @@ -28,12 +28,17 @@ set -e -if [[ -z "$Q_SCRIPT_DIR" ]]; then - Q_SCRIPT_DIR=~/.config/q.sh -fi +ENV_fzf=fzf +ENV_find=find +ENV_awk=awk +ENV_q=~/.config/q.sh SELF=$(cd -P -- "$(dirname -- "$0")" && printf '%s\n' "$(pwd -P)/$(basename -- "$0")") +if [[ -z "$Q_SCRIPT_DIR" ]]; then + Q_SCRIPT_DIR="$ENV_q" +fi + INDEX_FILE=~/.q.index HINT_FILE=~/.q.hints @@ -46,7 +51,7 @@ new_scripts_exist() { return 0 fi - NEWER_FILES="$(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 return 1 @@ -61,7 +66,7 @@ rebuild_index() { fi pushd "$Q_SCRIPT_DIR" >/dev/null - find . -type f -name 'q-*' | while read -r FN + $ENV_find . -type f -name 'q-*' | while read -r FN do FILENAME="$(echo "$FN" | sed 's/^\.\///g')" @@ -79,7 +84,7 @@ rebuild_hints() { fi pushd "$Q_SCRIPT_DIR" >/dev/null - find . -type f -name 'q-*' | while read -r FN + $ENV_find . -type f -name 'q-*' | while read -r FN do FILENAME="$(echo "$FN" | sed 's/^\.\///g')" @@ -93,7 +98,7 @@ get_scripts_for_cmd() { return fi - echo "$1" | awk -f "$INDEX_FILE" | while read -r line + echo "$1" | $ENV_awk -f "$INDEX_FILE" | while read -r line do echo "$line" done @@ -197,7 +202,7 @@ fi if [[ -z "$COMMAND" ]]; then PREF="$SELF -c" INITIAL="" - FZF_DEFAULT_COMMAND="$PREF '$INITIAL'" fzf \ + FZF_DEFAULT_COMMAND="$PREF '$INITIAL'" $ENV_fzf \ --bind "change:reload-sync:$PREF {q} || true" \ --ansi --query "$INITIAL" \ --preview "$SELF -p {}" \ diff --git a/q-bookmarks b/q-bookmarks index f68965f..efdc3e9 100755 --- a/q-bookmarks +++ b/q-bookmarks @@ -1,24 +1,27 @@ #!/bin/sh +ENV_cat=cat +ENV_awk=awk +ENV_xdg_open=xdg-open if [[ -z "$@" ]]; then echo "^b .*$" elif [[ "$1" == "--hint" ]]; then shift if [[ -f ~/.bookmarks.txt ]]; then - cat ~/.bookmarks.txt | awk '{print $1 " b " $2}' + $ENV_cat ~/.bookmarks.txt | $ENV_awk '{print $1 " b " $2}' fi elif [[ "$1" == "--complete" ]]; then shift if [[ -f ~/.bookmarks.txt ]]; then - cat ~/.bookmarks.txt | awk '{print $1 " b " $2}' + $ENV_cat ~/.bookmarks.txt | $ENV_awk '{print $1 " b " $2}' fi elif [[ "$1" == "--preview" ]]; then shift echo "$1" elif [[ "$1" == "--run" ]]; then shift - xdg-open "$1" + $ENV_xdg_open "$1" else echo "Unexpected arguments: $@" fi diff --git a/q-browse b/q-browse index 2562253..d7045b1 100755 --- a/q-browse +++ b/q-browse @@ -1,5 +1,8 @@ #!/bin/sh +ENV_xdg_open=xdg-open +ENV_cut=cut + if [[ -z "$@" ]]; then echo "^f .*$" elif [[ "$1" == "--hint" ]]; then @@ -9,11 +12,11 @@ elif [[ "$1" == "--complete" ]]; then echo "0 $@" elif [[ "$1" == "--preview" ]]; then shift - URL="$(echo "$@" | cut -d ' ' -f3-)" + URL="$(echo "$@" | $ENV_cut -d ' ' -f3-)" echo "Will open '$URL' in firefox" elif [[ "$1" == "--run" ]]; then shift - URL="$(echo "$@" | cut -d ' ' -f3-)" - (nohup xdg-open "$URL" 2>/dev/null &) + URL="$(echo "$@" | $ENV_cut -d ' ' -f3-)" + (nohup $ENV_xdg_open "$URL" 2>/dev/null &) sleep 0.01 fi diff --git a/q-calc b/q-calc index 36f3b60..71f6d8c 100755 --- a/q-calc +++ b/q-calc @@ -1,7 +1,11 @@ #!/bin/sh +ENV_bc=bc +ENV_wl_copy=wl-copy +ENV_cut=cut + calc() { - res="$(echo "$1" | bc 2>/dev/null)" + res="$(echo "$1" | $ENV_bc 2>/dev/null)" if [ "$res" == "" ]; then res="-" fi @@ -24,10 +28,10 @@ elif [[ "$1" == "--preview" ]]; then elif [[ "$1" == "--run" ]]; then shift echo "$1" - echo "$1" | (nohup wl-copy -n 2>/dev/null &) + echo "$1" | (nohup $ENV_wl_copy -n 2>/dev/null &) elif [[ "$1" == "--cli" ]]; then shift - calc "$(echo "$@" | cut -d ' ' -f2-)" + calc "$(echo "$@" | $ENV_cut -d ' ' -f2-)" else echo "Unexpected arguments: $@" fi diff --git a/q-clipboard b/q-clipboard index 1dcb76b..fd3ee66 100755 --- a/q-clipboard +++ b/q-clipboard @@ -1,26 +1,30 @@ #!/bin/sh +ENV_cat=cat +ENV_tac=tac +ENV_base64=base64 +ENV_wl_copy=wl-copy if [[ -z "$@" ]]; then echo "^c .*$" elif [[ "$1" == "--hint" ]]; then shift if [[ -f ~/.clipboard.txt ]]; then - cat ~/.clipboard.txt | tac + $ENV_cat ~/.clipboard.txt | $ENV_tac fi elif [[ "$1" == "--complete" ]]; then shift if [[ -f ~/.clipboard.txt ]]; then - cat ~/.clipboard.txt | tac + $ENV_cat ~/.clipboard.txt | $ENV_tac fi elif [[ "$1" == "--preview" ]]; then shift - echo "$1" | base64 -d + echo "$1" | $ENV_base64 -d elif [[ "$1" == "--run" ]]; then shift - TEXT="$(echo "$1" | base64 -d)" + TEXT="$(echo "$1" | $ENV_base64 -d)" echo "$TEXT" - echo -n "$TEXT" | (nohup wl-copy -n 2>/dev/null &) + echo -n "$TEXT" | (nohup $ENV_wl_copy -n 2>/dev/null &) else echo "Unexpected arguments: $@" fi diff --git a/q-pass b/q-pass index dbf9da6..6ffb57e 100755 --- a/q-pass +++ b/q-pass @@ -1,5 +1,10 @@ #!/bin/sh +ENV_pass=pass +ENV_find=find +ENV_sed=sed +ENV_wl_copy=wl-copy + if [[ -z "$@" ]]; then # Return a regular expression that should trigger # this subcommand. q.sh will then pass control to @@ -14,7 +19,7 @@ elif [[ "$1" == "--complete" ]]; then # passwords. This will be used to provide a list of # completions. cd ~/.password-store - find . -name "*.gpg" | sed -E 's/\.\/(.*)\.gpg/\1 p \1/' + $ENV_find . -name "*.gpg" | $ENV_sed -E 's/\.\/(.*)\.gpg/\1 p \1/' elif [[ "$1" == "--preview" ]]; then shift # This information will be displayed in the preview @@ -26,7 +31,7 @@ elif [[ "$1" == "--run" ]]; then # one of the options. We now decrypt the password # and place it to the clipboard. NAME="$1" - pass "$NAME" | (nohup wl-copy -n 2>/dev/null &) + $ENV_pass "$NAME" | (nohup $ENV_wl_copy -n 2>/dev/null &) # A short sleep is needed to wait for child process # to spawn before exiting. diff --git a/q-run b/q-run index cf74fe5..7cd87e2 100755 --- a/q-run +++ b/q-run @@ -1,21 +1,22 @@ #!/bin/sh -export PATH=~/.prefix/bin:~/.local/bin:$PATH +ENV_awk=awk +ENV_sort=sort +ENV_uniq=uniq find_all_executables() { { IFS=:; for d in $PATH; do for f in $d/*; do [ -f $f ] && [ -x $f ] && echo ${f##*/}; done; done; - } | sort - #flatpak --columns=application --app list | tail -n +1 | awk '{print "flatpak run " $0}'; + } | $ENV_sort | $ENV_uniq } if [[ -z "$@" ]]; then echo "^run [a-zA-Z0-9_-]*$" elif [[ "$1" == "--hint" ]]; then - find_all_executables | awk '{print $1 " run " $1}' + find_all_executables | $ENV_awk '{print $1 " run " $1}' elif [[ "$1" == "--complete" ]]; then shift - find_all_executables | awk '{print $1 " run " $1}' + find_all_executables | $ENV_awk '{print $1 " run " $1}' elif [[ "$1" == "--preview" ]]; then shift echo "Will run $1"