From b78002fae71292bca026456c9791908473417693 Mon Sep 17 00:00:00 2001 From: nostoromo root Date: Fri, 15 Jan 2021 10:26:39 -0800 Subject: [PATCH 1/2] Much improved bash experience --- bash.nix | 44 ++++++++++++++++++ defaults.nix | 1 + static/bash/bash.colors | 72 +++++++++++++++++++++++++++++ static/bash/bash.env | 22 +++++++++ static/bash/bash.helpers | 99 ++++++++++++++++++++++++++++++++++++++++ users/niten.nix | 23 +--------- 6 files changed, 239 insertions(+), 22 deletions(-) create mode 100644 bash.nix create mode 100644 static/bash/bash.colors create mode 100644 static/bash/bash.env create mode 100644 static/bash/bash.helpers diff --git a/bash.nix b/bash.nix new file mode 100644 index 0000000..cd7b65d --- /dev/null +++ b/bash.nix @@ -0,0 +1,44 @@ +{ config, lib, pkgs, ... }: + +let + helpers = pkgs.writeText "bash.helpers" + (builtins.readFile ./static/bash/bash.helpers); + + colors = + pkgs.writeText "bash.colors" (builtins.readFile ./static/bash/bash.colors); + + env = pkgs.writeText "bash.env" (builtins.readFile ./static/bash/bash.env); + +in { + config.programs.bash = { + shellAliases = { + ll = "ls $LS_OPTIONS -alF"; + l = "ls $LS_OPTIONS -CF"; + la = "ls $LS_OPTIONS -A"; + ".." = "cd .."; + "..." = "cd ../.."; + rm = "rm --one-file-system --preserve-root"; + }; + + shellInit = '' + if [ -d $HOME/bin ]; then + PATH="$HOME/bin:$PATH" + fi + ''; + + interactiveShellInit = '' + case $TERM in + screen|xterm*|rxvt*) + export LS_OPTIONS="--color=auto" + ;; + *) + export LS_OPTIONS="" + ;; + esac + + . ${colors} + . ${helpers} + . ${env} + ''; + }; +} diff --git a/defaults.nix b/defaults.nix index 548b676..541ae86 100644 --- a/defaults.nix +++ b/defaults.nix @@ -9,6 +9,7 @@ in { ./hardware-configuration.nix ./packages/local.nix ./config/local.nix + ./bash.nix ]; diff --git a/static/bash/bash.colors b/static/bash/bash.colors new file mode 100644 index 0000000..bb5a8b0 --- /dev/null +++ b/static/bash/bash.colors @@ -0,0 +1,72 @@ +# Reset +Color_Off='\e[0m' # Text Reset + +# Regular Colors +Black='\e[0;30m' # Black +Red='\e[0;31m' # Red +Green='\e[0;32m' # Green +Yellow='\e[0;33m' # Yellow +Blue='\e[0;34m' # Blue +Purple='\e[0;35m' # Purple +Cyan='\e[0;36m' # Cyan +White='\e[0;37m' # White + +# Bold +BBlack='\e[1;30m' # Black +BRed='\e[1;31m' # Red +BGreen='\e[1;32m' # Green +BYellow='\e[1;33m' # Yellow +BBlue='\e[1;34m' # Blue +BPurple='\e[1;35m' # Purple +BCyan='\e[1;36m' # Cyan +BWhite='\e[1;37m' # White + +# Underline +UBlack='\e[4;30m' # Black +URed='\e[4;31m' # Red +UGreen='\e[4;32m' # Green +UYellow='\e[4;33m' # Yellow +UBlue='\e[4;34m' # Blue +UPurple='\e[4;35m' # Purple +UCyan='\e[4;36m' # Cyan +UWhite='\e[4;37m' # White + +# Background +On_Black='\e[40m' # Black +On_Red='\e[41m' # Red +On_Green='\e[42m' # Green +On_Yellow='\e[43m' # Yellow +On_Blue='\e[44m' # Blue +On_Purple='\e[45m' # Purple +On_Cyan='\e[46m' # Cyan +On_White='\e[47m' # White + +# High Intensity +IBlack='\e[0;90m' # Black +IRed='\e[0;91m' # Red +IGreen='\e[0;92m' # Green +IYellow='\e[0;93m' # Yellow +IBlue='\e[0;94m' # Blue +IPurple='\e[0;95m' # Purple +ICyan='\e[0;96m' # Cyan +IWhite='\e[0;97m' # White + +# Bold High Intensity +BIBlack='\e[1;90m' # Black +BIRed='\e[1;91m' # Red +BIGreen='\e[1;92m' # Green +BIYellow='\e[1;93m' # Yellow +BIBlue='\e[1;94m' # Blue +BIPurple='\e[1;95m' # Purple +BICyan='\e[1;96m' # Cyan +BIWhite='\e[1;97m' # White + +# High Intensity backgrounds +On_IBlack='\e[0;100m' # Black +On_IRed='\e[0;101m' # Red +On_IGreen='\e[0;102m' # Green +On_IYellow='\e[0;103m' # Yellow +On_IBlue='\e[0;104m' # Blue +On_IPurple='\e[0;105m' # Purple +On_ICyan='\e[0;106m' # Cyan +On_IWhite='\e[0;107m' # White diff --git a/static/bash/bash.env b/static/bash/bash.env new file mode 100644 index 0000000..effd95b --- /dev/null +++ b/static/bash/bash.env @@ -0,0 +1,22 @@ + +if [ -d /fudo/etc ]; then + FUDO_CONFIG=/fudo/etc +fi + +if [ -f $HOME/.bash_local ]; then + . $HOME/.bash_local +fi + +if [ -f $HOME/.bash_env ]; then + . $HOME/.bash_env +fi + +if [ -f $HOME/.bash_prompt ]; then + . $HOME/.bash_prompt +elif [ "${TERM}x" == "dumbx" ]; then + export PS1="\u@\h:\w > " +elif [ $( type -t __makePS1 ) ]; then + export PROMPT_COMMAND=__makePS1 +else + export PS1="\u@\h:\w > " +fi diff --git a/static/bash/bash.helpers b/static/bash/bash.helpers new file mode 100644 index 0000000..4f5ab06 --- /dev/null +++ b/static/bash/bash.helpers @@ -0,0 +1,99 @@ +#!/usr/bin/env bash + +function __makeTerminalTitle() { + local title='' + + local CURRENT_DIR="${PWD/#$HOME/\~}" + + if [ -n "${SSH_CONNECTION}" ] || [ $UID -eq 0 ]; then + title+="`hostname`:${CURRENT_DIR} [`whoami`@`hostname -f`]" + else + title+="${CURRENT_DIR} [`whoami`]" + fi + + echo -en '\033]2;'${title}'\007' +} + +function __getMachineId() { + if [ -f /etc/machine-id ]; then + echo $((0x$(cat /etc/machine-id | head -c 15))) + elif [ -x "$(which hostid 2>&1)" ]; then + echo $(( (${#HOSTNAME}+0x$(hostid)))) + else + echo "00000000" + fi +} + +function __makePS1() { + local EXIT="$?" + + if [ ! -n "${HOST_COLOR}" ]; then + local H=$(__getMachineId) + HOST_COLOR=$(tput setaf $((H%5 + 2))) # foreground + fi + + PS1='' + + if [ ${USER} == root ]; then + PS1+="\[${Red}\]" # root + elif [ ${USER} != ${LOGNAME} ]; then + PS1+="\[${Blue}\]" # normal user + else + PS1+="\[${Green}\]" # normal user + fi + PS1+="\u\[${Color_Off}\]" + + if [ -n "${SSH_CONNECTION}" ] || [ $UID -eq 0 ]; then + PS1+="\[${Purple}\]@\[${Color_Off}\]" + PS1+="\[${UWhite}${HOST_COLOR}\]\h\[${Color_Off}\]" # host displayed only if ssh connection + fi + + PS1+="\[${Purple}\]:\w" # working directory + + # screen sessions + local SCREEN_PATHS="/var/run/screens/S-`whoami` /var/run/screen/S-`whoami` /var/run/uscreens/S-`whoami`" + + for screen_path in ${SCREEN_PATHS}; do + if [ -d ${screen_path} ]; then + SCREEN_JOBS=`ls ${screen_path} | wc -w` + if [ ${SCREEN_JOBS} != 0 ]; then + local current_screen="$(echo ${STY} | cut -d '.' -f 1)" + if [ -n "${current_screen}" ]; then + current_screen=":${current_screen}" + fi + PS1+=" \[${BGreen}\][s${SCREEN_JOBS}${current_screen}]\[${Color_Off}\]" + fi + break + fi + done + + # git branch + if [ -x "`which git 2>&1`" ]; then + local branch="$(git name-rev --name-only HEAD 2>/dev/null)" + + if [ -n "${branch}" ]; then + local git_status="$(git status --porcelain -b 2>/dev/null)" + local letters="$( echo "${git_status}" | grep --regexp=' \w ' | sed -e 's/^\s\?\(\w\)\s.*$/\1/' )" + local untracked="$( echo "${git_status}" | grep -F '?? ' | sed -e 's/^\?\(\?\)\s.*$/\1/' )" + local status_line="$( echo -e "${letters}\n${untracked}" | sort | uniq | tr -d '[:space:]' )" + PS1+=" \[${Blue}\](git: ${branch}" + if [ -n "${status_line}" ]; then + PS1+=" ${status_line}" + fi + PS1+=")\[${Color_Off}\]" + fi + fi + + PS1+="\n" + + # exit code + if [ ${EXIT} != 0 ]; then + PS1+="\[${BRed}\][!${EXIT}]\[${Color_Off}\]" + else + PS1+="\[${Green}\][${EXIT}]\[${Color_Off}\]" + fi + + PS1+="\[${Purple}\]->\[${Color_Off}\] " # prompt + + __makeTerminalTitle +} diff --git a/users/niten.nix b/users/niten.nix index 707d88e..43e8a38 100644 --- a/users/niten.nix +++ b/users/niten.nix @@ -6,28 +6,7 @@ let in { programs = { - bash = { - enable = true; - shellAliases = { - ".." = "cd .."; - "..." = "cd ../.."; - la = "ls -a"; - ll = "ls -l"; - lla = "ls -la"; - rm = "rm --one-file-system --preserve-root"; - }; - - extraInit = '' - case $TERM in - screen|xterm*|rxvt*) - shopt -s checkwinsize - ;; - *) - export LS_OPTIONS="" - ;; - esac - ''; - }; + bash = { enable = true; }; git = { enable = true; From 9228bb41ab9224f732e2b9874c62d5a8016fbb2f Mon Sep 17 00:00:00 2001 From: nostoromo root Date: Fri, 15 Jan 2021 11:06:17 -0800 Subject: [PATCH 2/2] Use new emacs config --- packages/local.nix | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/local.nix b/packages/local.nix index ebe2e9c..43ce5de 100644 --- a/packages/local.nix +++ b/packages/local.nix @@ -158,8 +158,8 @@ in { doom-emacs-config = pkgs.fetchgit { url = "https://git.fudo.org/niten/doom-emacs.git"; - rev = "467c45705c73ee39acbfabc04c5aaa4099408dc4"; - sha256 = "172ah7ymlwymb4rx719nhsfvsxwmq14nlisba84kw34cmhdcsjh7"; + rev = "f89cc7e24e09f0ea24ce7eef8aaf4f061744094c"; + sha256 = "1cwf1dfknnpkg1gpj2c0nihxw00n790xsfli4fk3iqm62m66cw1s"; }; # vanilla-forum = import vanilla-forum.nix { inherit pkgs lib; };