@@ -14,10 +14,20 @@ fail() {
14
14
exit 1
15
15
}
16
16
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
+
17
26
# MARK - Arguments
18
27
19
28
cwd=" ${RNS_CWD:- } "
20
29
script=" ${RNS_RUN:- } "
30
+ derivation_path=" ${RNS_DERIVATION_PATH:- } "
21
31
22
32
pure=" ${RNS_PURE:- true} "
23
33
verbose=" ${RNS_VERBOSE:- false} "
@@ -70,11 +80,13 @@ if is_verbose; then
70
80
RNS_CWD: ${RNS_CWD:- }
71
81
RNS_OPTS: ${RNS_OPTS:- }
72
82
RNS_RUN: ${RNS_RUN:- }
83
+ RNS_DERIVATION_PATH: ${RNS_DERIVATION_PATH:- }
73
84
RNS_PURE: ${RNS_PURE:- }
74
85
cwd: ${cwd:- }
75
86
nix_shell_opts: $( printf " %q " " ${nix_shell_opts[@]} " )
76
87
pure: ${pure:- }
77
88
script: ${script:- }
89
+ derivation_path: ${derivation_path:- }
78
90
===
79
91
EOF
80
92
) "
83
95
84
96
# MARK - Process Options and Arguments
85
97
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
+
86
122
if [[ " ${pure} " == " true" ]]; then
87
123
nix_shell_opts+=( --pure )
88
124
fi
@@ -101,16 +137,22 @@ if [[ -n "${RNS_OPTS:-}" ]]; then
101
137
fi
102
138
103
139
if [[ -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."
105
141
fi
106
142
107
143
# MARK - Execute script
108
144
109
145
# Change to the specified working directory
110
146
if [[ -n " ${cwd:- } " ]]; then
111
- cd " ${cwd} "
147
+ cd_cmd= " cd ${cwd} "
112
148
fi
113
149
150
+ script=" $( cat << -EOF
151
+ ${cd_cmd:- }
152
+ ${script}
153
+ EOF
154
+ ) "
155
+
114
156
cmd=( nix-shell )
115
157
if [[ ${# nix_shell_opts[@]} -gt 0 ]]; then
116
158
cmd+=( " ${nix_shell_opts[@]} " )
@@ -120,6 +162,7 @@ cmd+=( --run "${script}" )
120
162
if is_verbose; then
121
163
verbose_output=" $( cat << -EOF
122
164
=== run_nix_shell command-line invocation ===
165
+ pwd: ${PWD}
123
166
$( printf " %q " " ${cmd[@]} " )
124
167
===
125
168
EOF
0 commit comments