Files
nix-stuff/common/shell/not-aliases.nix
Shelvacu 91a27769ba stuff
2025-05-23 17:56:46 -07:00

209 lines
5.6 KiB
Nix

# These are the things that might in a simpler time go in ~/.bashrc as aliases. But they're not aliases, cuz aliases are bad
{
pkgs,
lib,
config,
...
}:
let
script =
name: content:
pkgs.writers.makeScriptWriter
{
interpreter = lib.getExe pkgs.bashInteractive;
check = lib.escapeShellArgs [
(lib.getExe pkgs.shellcheck)
"--norc"
"--severity=info"
pkgs.shellvaculib.file
];
}
"/bin/${name}"
''
set -euo pipefail
source ${lib.escapeShellArg pkgs.shellvaculib.file}
${content}
'';
simple =
name: args:
let
binContents = ''
#!${lib.getExe pkgs.bash}
exec ${lib.escapeShellArgs args} "$@"'';
funcContents = ''
local aliasName=${lib.escapeShellArg name}
local replacementWords=(${lib.escapeShellArgs args})
local replacementStr="''${replacementWords[*]}"
COMP_LINE="''${COMP_LINE/#$aliasName/$replacementStr}"
COMP_POINT=$(( COMP_POINT + ''${#replacementStr} - ''${#aliasName} ))
COMP_CWORD=$(( COMP_CWORD + ''${#replacementWords[@]} - 1 ))
COMP_WORDS=("''${replacementWords[@]}" "''${COMP_WORDS[@]:1}")
_comp_command_offset 0
'';
in
pkgs.runCommandLocal name { meta.mainProgram = name; } ''
mkdir -p $out/bin
echo ${lib.escapeShellArg binContents} > $out/bin/${name}
out_base="$(dirname "$out")"
LC_ALL=C
completion_function_name="_completion_''${out_base//[^a-zA-Z0-9_]/_}"
completion_file=$out/share/bash-completion/completions/${name}
mkdir -p "$(dirname "$completion_file")"
echo "''${completion_function_name}() {" >> "$completion_file"
echo ${lib.escapeShellArg funcContents} >> "$completion_file"
echo "}" >> "$completion_file"
echo "complete -F $completion_function_name ${name}" >> $completion_file
'';
ms_text = with_sudo: ''
svl_minmax_args $# 1 3
host="$1"
session_name="''${2:-main}"
set -x
mosh -- "$host" ${lib.optionalString with_sudo "sudo"} screen -RdS "$session_name"
'';
systemctl = "${pkgs.systemd}/bin/systemctl";
journalctl = "${pkgs.systemd}/bin/journalctl";
in
{
vacu.packages = [
(script "ms" (ms_text false))
(script "mss" (ms_text true))
(script "msl" ''
svl_exact_args $# 1
host="$1"
echo 'echo "user:"; screen -ls; echo; echo "root:"; sudo screen -ls' | ssh -T "$host"
'')
(script "rmln" ''
if [[ $# == 0 ]]; then
echo "warn: no arguments passed to rmln" >&2
fi
for arg in "$@"; do
if [[ "$arg" != "-*" ]] && [[ ! -L "$arg" ]]; then
die "$arg is not a symlink"
fi
done
rm "$@"
'')
(script "nr" ''
# nix run nixpkgs#<thing> -- <args>
svl_min_args $# 1
installable="$1"
shift
if [[ "$installable" != *'#'* ]]; then
installable="nixpkgs#$installable"
fi
nix run "$installable" -- "$@"
'')
(script "nb" ''
# nix build nixpkgs#<thing> <args>
svl_min_args $# 1
installable="$1"
shift
if [[ "$installable" != *'#'* ]]; then
installable="nixpkgs#$installable"
fi
nix build "$installable" "$@"
'')
(script "ns" ''
# nix shell nixpkgs#<thing>
svl_min_args $# 1
new_args=( )
for arg in "$@"; do
if [[ "$arg" != *'#'* ]] && [[ "$arg" != -* ]]; then
arg="nixpkgs#$arg"
fi
new_args+=("$arg")
done
nix shell "''${new_args[@]}"
'')
(simple "sc" [ systemctl ])
(simple "scs" [
systemctl
"status"
"--lines=20"
"--full"
])
(simple "scc" [
systemctl
"cat"
])
(simple "scr" [
systemctl
"restart"
])
(simple "jc" [
journalctl
"--pager-end"
])
(simple "jcu" [
journalctl
"--pager-end"
"-u"
])
(script "list-auto-roots" ''
auto_roots="/nix/var/nix/gcroots/auto"
svl_exact_args $# 0
echo "List of auto-added nix gcroots, excluding system profiles:"
echo
for fn in "$auto_roots/"*; do
if ! [[ -L "$fn" ]]; then
die "fn is not a symlink!?: $fn"
fi
pointed="$(readlink -v "$fn")"
if ! [[ -e "$pointed" ]]; then
continue
fi
if [[ "$pointed" == /nix/var/nix/profiles/system-* ]]; then
continue
fi
echo "$pointed"
done
'')
];
vacu.shell.functions = {
nd = ''
svl_min_args $# 1
declare -a args=("$@")
lastarg="''${args[-1]}"
if [[ "$lastarg" == "-"* ]]; then
echo "nd: last argument must be the directory" 1>&2
return 1
fi
for arg in "''${args[@]::''${#args[@]}-1}"; do
if [[ "$arg" != "-"* ]]; then
echo "nd: last argument must be the directory" 1>&2
return 1
fi
done
mkdir "''${args[@]}" && cd "''${args[-1]}"
'';
nt = ''pushd "$(mktemp -d "$@")"'';
};
vacu.textChecks."vacu-shell-functions-nd" = ''
source ${lib.escapeShellArg pkgs.shellvaculib.file}
function nd() {
${config.vacu.shell.functions.nd}
}
start=/tmp/test-place
mkdir -p $start
cd $start
nd a
[[ "$PWD" == "$start/a" ]]
cd $start
nd -p b/c
[[ "$PWD" == "$start/b/c" ]]
'';
vacu.textChecks."vacu-shell-functions-nt" = ''
source ${lib.escapeShellArg pkgs.shellvaculib.file}
function nt() {
${config.vacu.shell.functions.nt}
}
start=$PWD
nt
[[ "$PWD" != "$start" ]]
popd
[[ "$PWD" == "$start" ]]
'';
}