Last active
January 29, 2020 12:40
-
-
Save magnetophon/55cc428e2a4b95de71f6c38a3d4ba86e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
##################################################################### | |
# 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" | |
# This function is called whenever a command is not found. | |
command_not_found_handler() { | |
local p=/nix/store/ldz5024nnwj5q05iccp6wkfaiwbdccq3-command-not-found/bin/command-not-found | |
if [ -x $p -a -f /nix/var/nix/profiles/per-user/root/channels/nixos/programs.sqlite ]; then | |
# Run the helper program. | |
$p "$@" | |
# Retry the command if we just installed it. | |
if [ $? = 126 ]; then | |
"$@" | |
fi | |
else | |
# Indicate than there was an error so ZSH falls back to its default handler | |
echo "$1: command not found" >&2 | |
return 127 | |
fi | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment