xin/bins/rage.nix

102 lines
2.5 KiB
Nix
Raw Permalink Normal View History

2023-09-12 08:44:05 -06:00
{ pkgs }:
let
2022-08-28 16:17:50 -06:00
oathPkg = pkgs.oath-toolkit or pkgs.oathToolkit;
2023-07-11 09:12:50 -06:00
wlclip =
if pkgs.system == "aarch64-darwin"
then ""
else "${pkgs.wl-clipboard}/bin/wl-copy";
xclip =
if pkgs.system == "aarch64-darwin"
then "pbcopy"
else "${pkgs.xclip}/bin/xclip";
2023-09-12 08:44:05 -06:00
in
''
2022-09-23 09:35:21 -06:00
#!${pkgs.yash}/bin/yash
2022-08-25 12:21:35 -06:00
set -e
rage_dir=~/.rage
. ''${rage_dir}/config
cmd=$1
list() {
find $rage_dir -type f -name \*.age
}
if [ -z $cmd ]; then
list
exit
fi
case $cmd in
ls)
list
;;
re)
F=""
if [ -f $2 ]; then
F=$2
else
F=$(list | grep $2)
fi
echo "Re-encrypting: '$F'"
pass="$(${pkgs.age}/bin/age -i $identity -d "$F")"
echo "$pass" | ${pkgs.age}/bin/age -a -R "$recipients" > "$F"
;;
en)
printf 'Password: '
stty -echo
read pass
stty echo
echo ""
printf 'Location: '
read loc
echo ""
mkdir -p "$(dirname ~/.rage/$loc)"
echo "$pass" | ${pkgs.age}/bin/age -a -R "$recipients" > ~/.rage/''${loc}.age
;;
de)
if [ -f $2 ]; then
${pkgs.age}/bin/age -i $identity -d $2
else
F=$(list | grep $2)
${pkgs.age}/bin/age -i $identity -d "$F"
fi
;;
cp)
CLIP=${xclip}
if [ ! -z $WAYLAND_DISPLAY ]; then
CLIP=${wlclip}
fi
2022-08-25 12:21:35 -06:00
if [ -f $2 ]; then
${pkgs.age}/bin/age -i $identity -d $2 | $CLIP
2022-08-25 12:21:35 -06:00
else
F=$(list | grep $2)
${pkgs.age}/bin/age -i $identity -d "$F" | $CLIP
2022-08-25 12:21:35 -06:00
fi
;;
otp)
if [ -f $2 ]; then
${pkgs.age}/bin/age -i $identity -d $2 | ${oathPkg}/bin/oathtool -b --totp -
else
F=$(list | grep $2)
${pkgs.age}/bin/age -i $identity -d "$F" | ${oathPkg}/bin/oathtool -b --totp -
fi
;;
push)
cd $rage_dir
2022-08-28 16:17:50 -06:00
${pkgs.git}/bin/git push
2022-08-25 12:21:35 -06:00
;;
sync)
cd $rage_dir
2022-08-28 16:17:50 -06:00
${pkgs.git-sync}/bin/git-sync
2022-08-25 12:21:35 -06:00
;;
default)
list
esac
''