Skip to content

Instantly share code, notes, and snippets.

@cleverca22
Created January 29, 2020 12:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cleverca22/0b5f850823123d404363ccb681cd7109 to your computer and use it in GitHub Desktop.
Save cleverca22/0b5f850823123d404363ccb681cd7109 to your computer and use it in GitHub Desktop.
{
interactiveShellInit = ''
#####################################################################
# shell independent prompts #########################################
#####################################################################
# Read a single char from /dev/tty, prompting with "$*"
# Note: pressing enter will return a null string. Perhaps a version terminated with X and then remove it in caller?
# See https://unix.stackexchange.com/a/367880/143394 for dealing with multi-byte, etc.
function get_keypress {
local REPLY IFS=
>/dev/tty printf '%s' "$*"
[[ $ZSH_VERSION ]] && read -rk1 # Use -u0 to read from STDIN
# See https://unix.stackexchange.com/q/383197/143394 regarding '\n' -> '''
[[ $BASH_VERSION ]] && </dev/tty read -rn1
printf '%s' "$REPLY"
}
# Get a y/n from the user, return yes=0, no=1 enter=$2
# Prompt using $1.
# If set, return $2 on pressing enter, useful for cancel or defualting
function get_yes_keypress {
local prompt="''${1:-Are you sure [y/n]? }"
local enter_return=$2
local REPLY
# [[ ! $prompt ]] && prompt="[y/n]? "
while REPLY=$(get_keypress "$prompt"); do
[[ $REPLY ]] && printf '\n' # $REPLY blank if user presses enter
case "$REPLY" in
Y|y) return 0;;
N|n) return 1;;
''') [[ $enter_return ]] && return "$enter_return"
esac
done
}
# Credit: http://unix.stackexchange.com/a/14444/143394
# Prompt to confirm, defaulting to NO on <enter>
# Usage: confirm "Dangerous. Are you sure?" && rm *
function confirm {
local prompt=''${*:-Are you sure} [y/N]? "
get_yes_keypress "$prompt" 1
}
# Prompt to confirm, defaulting to YES on <enter>
function confirm_yes {
local prompt="''${*:-Are you sure} [Y/n]? "
get_yes_keypress "$prompt" 0
}
#####################################################################
#####################################################################
#####################################################################
alias up='nixos-rebuild test --upgrade '
function upn {
cd $NIXPKGS &&
if [[ $(git status --porcelain ) == "" ]];
then
echo "checking out commit " $(nixos-version --hash) " under branch name " $(nixos-version | cut -d" " -f1)
git fetch upstream && git checkout $(nixos-version --hash) -b $(nixos-version | cut -d" " -f1)
else
git status
fi
}
alias gcn='cd $NIXPKGS && git checkout $(nixos-version | cut -d" " -f1)'
alias te='nixos-rebuild test -p rt -I nixos-config=/home/bart/nixosConfig/machines/$(hostname | cut -d"-" -f1)/rt.nix && nixos-rebuild test'
alias ten='nixos-rebuild test -p rt -I nixos-config=/home/bart/nixosConfig/machines/$(hostname | cut -d"-" -f1)/rt.nix -I nixpkgs=$NIXPKGS && nixos-rebuild test -I nixpkgs=$NIXPKGS'
alias sw='nixos-rebuild boot -p rt -I nixos-config=/home/bart/nixosConfig/machines/$(hostname | cut -d"-" -f1)/rt.nix && nixos-rebuild switch'
alias swn='nixos-rebuild switch -p rt -I nixos-config=/home/bart/nixosConfig/machines/$(hostname | cut -d"-" -f1)/rt.nix -I nixpkgs=$NIXPKGS && nixos-rebuild switch -I nixpkgs=$NIXPKGS'
nga() {
if confirm "Delete all generations and vacuum the systemd journal?"; then
nix-collect-garbage -d && journalctl --vacuum-time=2d
else
echo "\nOK, we'll keep it all"
fi
}
ngd() {
if [[ -n "$1" ]] && [[ "$1" =~ ^-?[0-9]+$ ]]; then
if confirm "Delete all generations and vacuum the systemd journal except for the last $1 days?"; then
nix-collect-garbage --delete-older-than $1d && journalctl --vacuum-time=$1d
else
echo "\nOK, we'll keep it all."
fi
else
echo "\nYou need to give the number of days you want to keep!"
fi
}
lg() {
echo "System generations\n"
nix-env -p /nix/var/nix/profiles/system --list-generations
echo "\n\nRT generations:\n"
nix-env -p /nix/var/nix/profiles/system-profiles/rt --list-generations
}
dgs() {
if [[ -n "$@" ]] && [[ "$@" =~ ^-?[0-9]+$ ]]; then
nix-env -p /nix/var/nix/profiles/system --delete-generations $@
else
echo "\nYou need to tell me which generations to delete!"
fi
}
dgr() {
if [[ -n "$@" ]] && [[ "$@" =~ ^-?[0-9]+$ ]]; then
nix-env -p /nix/var/nix/profiles/system-profiles/rt --delete-generations $@
else
echo "\nYou need to tell me which generations to delete!"
fi
}
vi() {emacseditor --create-frame --quiet --no-wait "$@"}
# export EDITOR="vi"
'';
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment