Skip to content

Instantly share code, notes, and snippets.

@zimbatm
Created December 31, 2019 12:22
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 zimbatm/64511073de887986754ce613574dc885 to your computer and use it in GitHub Desktop.
Save zimbatm/64511073de887986754ce613574dc885 to your computer and use it in GitHub Desktop.
{ lib
, bashInteractive
, buildEnv
, writeText
}:
{ name ? "profile"
, packages ? {}
, env ? {}
, profile ? ""
, interactive ? ""
}:
let
envPairs = lib.mapAttrsToList
(k: v: "export ${k}=${lib.escapeShellArg (toString v)}")
env;
profileDrv = writeText "bashrc" ''
# Set all the environment variables
${builtins.concatStringsSep "\n" envPairs}
# Restore user variables in --pure more
if [[ -z "''${USER:-}" ]]; then
# Magic! Expand \u like it was a prompt
: \\u
export USER=$(printf '%s\n' "''${_@P}")
fi
if [[ -z "''${HOME:-}" ]]; then
export HOME=$(eval echo ~$USER)
fi
# Load installed profiles
shopt -s nullglob
for profile in $PROFILE_ROOT/etc/profile.d/*.sh; do
source "$profile"
done
# Extra profile
${profile}
# Interactive sessions
if [[ $- == *i* ]]; then
# Set PS1
PS1='\[\033[0;32;40m\][${name}]$\[\033[0m\] '
${interactive}
fi
'';
in
buildEnv {
inherit name;
paths = packages;
postBuild = ''
cat <<PROFILE > $out/bashrc
export PROFILE_ROOT=$out
export PATH=$out/bin\''${PATH+:\$PATH}
PROFILE
cat "${profileDrv}" >> $out/bashrc
cat <<'ALMOST_BASH' > $out/bash.sh
#!${bashInteractive}/bin/bash
#
# Almost bash. If --pure
#
# Usage: bash.sh [--pure] [<args>...]
#
# Options:
# * --pure: re-start the script in a pure environment. Must be
# the first argument.
# * normal bash options
set -euo pipefail
if [[ "''${1:-}" == "--pure" ]]; then
shift
exec -c "$0" "$@"
fi
ALMOST_BASH
# splice in the bashrc location
echo "bashrc_path=$out/bashrc" >> $out/bash.sh
cat <<'ALMOST_BASH' >> $out/bash.sh
if [[ $# = 0 ]]; then
# start an interactive shell
set -- ${bashInteractive}/bin/bash --rcfile "$bashrc_path" --noprofile
else
# start a script
source "$bashrc_path"
set -- ${bashInteractive}/bin/bash "$@"
fi
exec -a "$0" "$@"
ALMOST_BASH
chmod +x $out/bash.sh
'';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment