diff --git a/nix/better-prompt.nix b/nix/better-prompt.nix new file mode 100644 --- /dev/null +++ b/nix/better-prompt.nix @@ -0,0 +1,61 @@ +{ lib +, stdenv +, git +, shellcheck +, writeTextFile +, runtimeShell +}: + +let + prompt-path = "share/bash-completion/completions/git-prompt.sh"; + +# Normally we would want to use writeShellApplications for scripts, however, +# since file will be sourced, we do not want to inherit 'set -euo pipefail' +# as this will cause the development shell to exit for any failed command +in writeTextFile rec { + name = "better-prompt"; + destination = "/bin/${name}"; + executable = true; + text = '' + #!${runtimeShell} + + comm_set_better_prompt() { + local RED="\033[0;31m" + local GREEN="\033[0;32m" + local NO_COLOR="\033[m" + local BLUE="\033[0;34m" + + # Allow for '__git_ps1' command to be available + git_prompt_path=${git}/${prompt-path} + if [ -f "$git_prompt_path" ] && ! command -v __git_ps1 > /dev/null; then + # shellcheck source=${git}/${prompt-path} + source "$git_prompt_path" + fi + + # If root user, make prompt symbol a # + prompt_symbol=$(test "$UID" == "0" && \ + echo "''${RED}#''${NO_COLOR}" || \ + echo "$") + PS1="''${RED}[\t] ''${GREEN}\u@\h ''${NO_COLOR}\w''${BLUE}" + PS1="$PS1\`__git_ps1\`''${NO_COLOR}\n''${prompt_symbol} " + + if [[ -n "$IN_NIX_SHELL" ]]; then + PS1="(nix-shell) $PS1" + fi + } + + # Check if PS1 is unset or the default bash prompt for mac or GNU + # If it is, make it more usable + if [[ -z "''${PS1:-}" ]] || \ + [[ "''${PS1}" == '\s-\v$ ' ]] || \ + [[ "''${PS1}" == '%n@%m %1~ %# ' ]]; then + comm_set_better_prompt + fi + ''; + + checkPhase = '' + ${stdenv.shellDryRun} $target + # Need to pass -x so that shellcheck will source external files + ${shellcheck}/bin/shellcheck -x $target + ''; +} diff --git a/nix/dev-shell.nix b/nix/dev-shell.nix --- a/nix/dev-shell.nix +++ b/nix/dev-shell.nix @@ -4,6 +4,7 @@ , amqp-cpp , arcanist , aws-sdk-cpp +, better-prompt , boost , cargo , cmake @@ -105,6 +106,9 @@ export ANDROID_SDK_ROOT=''${ANDROID_SDK_ROOT:-$HOME/Library/Android/sdk} fi + # Provide decent bash prompt + source ${better-prompt}/bin/better-prompt + echo "Welcome to Comm dev environment! :)" ''; } diff --git a/nix/overlay.nix b/nix/overlay.nix --- a/nix/overlay.nix +++ b/nix/overlay.nix @@ -41,6 +41,8 @@ arcanist = prev.callPackage ./arcanist.nix { }; + better-prompt = prev.callPackage ./better-prompt.nix { }; + protobuf_3_15_cmake = prev.callPackage ./protobuf_3_15.nix { }; devShell = final.callPackage ./dev-shell.nix { };