Adapt to Nix

This commit is contained in:
Konstantin Nazarov 2023-05-14 20:24:27 +01:00
parent fe69664195
commit 822adb11c6
Signed by: knazarov
GPG key ID: 4CFE0A42FA409C22
9 changed files with 147 additions and 59 deletions

26
flake.lock Normal file
View file

@ -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
}

View file

@ -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;});
};
}

21
q
View file

@ -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 {}" \

View file

@ -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

View file

@ -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

10
q-calc
View file

@ -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

View file

@ -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

9
q-pass
View file

@ -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.

11
q-run
View file

@ -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"