@@ -14,10 +14,20 @@ fail() {
1414 exit 1
1515}
1616
17+ absolute_path () {
18+ local path=" ${1} "
19+ local bname
20+ local dname
21+ bname=" $( basename " ${path} " ) "
22+ dname=" $( dirname " ${path} " ) "
23+ echo " $( cd " ${dname} " ; pwd ) /${bname} "
24+ }
25+
1726# MARK - Arguments
1827
1928cwd=" ${RNS_CWD:- } "
2029script=" ${RNS_RUN:- } "
30+ derivation_path=" ${RNS_DERIVATION_PATH:- } "
2131
2232pure=" ${RNS_PURE:- true} "
2333verbose=" ${RNS_VERBOSE:- false} "
@@ -70,11 +80,13 @@ if is_verbose; then
7080RNS_CWD: ${RNS_CWD:- }
7181RNS_OPTS: ${RNS_OPTS:- }
7282RNS_RUN: ${RNS_RUN:- }
83+ RNS_DERIVATION_PATH: ${RNS_DERIVATION_PATH:- }
7384RNS_PURE: ${RNS_PURE:- }
7485cwd: ${cwd:- }
7586nix_shell_opts: $( printf " %q " " ${nix_shell_opts[@]} " )
7687pure: ${pure:- }
7788script: ${script:- }
89+ derivation_path: ${derivation_path:- }
7890===
7991EOF
8092) "
8395
8496# MARK - Process Options and Arguments
8597
98+ # Resolve the target directory to an absolute path before processing the
99+ # derivation path. That logic may change directories.
100+ if [[ -n " ${cwd:- } " ]]; then
101+ cwd=" $( absolute_path " ${cwd} " ) "
102+ fi
103+
104+ # The shell.nix or default.nix may contain relative paths to other files. The
105+ # Nix logic does not appear to resolve these relative to the file, but to the
106+ # current directory. So, we will ensure that we are in the derivation's
107+ # directory before executing the command.
108+ if [[ -n " ${derivation_path:- } " ]]; then
109+ derivation_path=" $( absolute_path " ${derivation_path} " ) "
110+ if [[ -d " ${derivation_path} " ]]; then
111+ derivation_dirname=" ${derivation_path} "
112+ else
113+ derivation_dirname=" $( dirname " ${derivation_path} " ) "
114+ derivation_basename=" $( basename " ${derivation_path} " ) "
115+ fi
116+ cd " ${derivation_dirname} "
117+ if [[ -n " ${derivation_basename:- } " ]]; then
118+ nix_shell_opts+=( " ${derivation_basename} " )
119+ fi
120+ fi
121+
86122if [[ " ${pure} " == " true" ]]; then
87123 nix_shell_opts+=( --pure )
88124fi
@@ -101,16 +137,22 @@ if [[ -n "${RNS_OPTS:-}" ]]; then
101137fi
102138
103139if [[ -z " ${script:- } " ]]; then
104- fail " A script for a path to a file must be provided."
140+ fail " A script or a path to a file must be provided."
105141fi
106142
107143# MARK - Execute script
108144
109145# Change to the specified working directory
110146if [[ -n " ${cwd:- } " ]]; then
111- cd " ${cwd} "
147+ cd_cmd= " cd ${cwd} "
112148fi
113149
150+ script=" $( cat << -EOF
151+ ${cd_cmd:- }
152+ ${script}
153+ EOF
154+ ) "
155+
114156cmd=( nix-shell )
115157if [[ ${# nix_shell_opts[@]} -gt 0 ]]; then
116158 cmd+=( " ${nix_shell_opts[@]} " )
@@ -120,6 +162,7 @@ cmd+=( --run "${script}" )
120162if is_verbose; then
121163 verbose_output=" $( cat << -EOF
122164=== run_nix_shell command-line invocation ===
165+ pwd: ${PWD}
123166$( printf " %q " " ${cmd[@]} " )
124167===
125168EOF
0 commit comments