Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)
1#!@runtimeShell@
2
3# baked variables, not exported
4bashrc='@bashrc@'
5bash='@bashInteractive@/bin/bash'
6
7# detected variables and option defaults
8shell="$(basename $SHELL)"
9mode=interactive
10
11# remaining args
12args=()
13
14# parse arguments
15while [[ $# -gt 0 ]]; do
16 case "$1" in
17 -c)
18 mode=commands
19 shift
20 break
21 ;;
22 *)
23 mode=exec
24 break
25 ;;
26 esac
27done
28
29while [[ $# -gt 0 ]]; do
30 args+=("$1")
31 shift
32done
33
34case "$mode" in
35 interactive)
36 ;;
37 commands)
38 if ${#args[@]} -eq 0; then
39 echo "nixpkgs: You've requested a command shell, but didn't provide a command."
40 echo " Please provide a command to run."
41 exit 1
42 fi
43 ;;
44 exec)
45 ;;
46esac
47
48invokeWithArgs() {
49 bash -c 'source "'"$bashrc"'"; _script="$(shift)"; _args=("$@"); eval "$_script"' -- "$@"
50}
51
52# For bash, we'll run the interactive variant of the version Nixpkgs uses.
53# For other shells, version correctness can't be a goal, so it's best
54# to launch the user's shell from $SHELL. This also avoids bringing in
55# dependencies of which N-1 aren't needed. Keeps it quick.
56launchBash() {
57 case "$mode" in
58 interactive)
59 exec "$bash" --rcfile "$bashrc" "${args[@]}"
60 ;;
61 commands)
62 exec "$bash" -c 'source "'"$bashrc"'"; _script="$1"; shift; eval "$_script"' -- "${args[@]}"
63 ;;
64 exec)
65 exec "$bash" -c 'source "'"$bashrc"'"; "$@"' -- "${args[@]}"
66 ;;
67 esac
68}
69
70launchShell() {
71 case "$shell" in
72 bash|"")
73 launchBash
74 ;;
75 *)
76 (
77 echo "nixpkgs: I see that you weren't running bash. That's cool, but"
78 echo " stdenv derivations are built with bash, so that's what"
79 echo " I'll run for you by default. We'd love to have support"
80 echo " for many shells, so PRs are welcome!"
81 ) >&2
82 launchBash
83 ;;
84 esac
85}
86
87launchShell