Skip to content

Commit b357837

Browse files
authored
feat: specify the location of the derivation with derivation-path (#5)
- Add `derivation-path` input to specify the location of the `shell.nix` or `default.nix`. - Add unit tests and integration tests for this functionality. Related to #4.
1 parent a1bdfab commit b357837

File tree

5 files changed

+89
-5
lines changed

5 files changed

+89
-5
lines changed

.github/workflows/ci.yaml

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,12 @@ jobs:
4343
- uses: ./.github/actions/set_up_runner
4444
with:
4545
github_token: ${{ secrets.GITHUB_TOKEN }}
46-
- name: Execute simple
46+
- name: Execute in directory with shell.nix
4747
uses: ./
4848
with:
4949
verbose: true
50+
# Demonstrate passing a directory that contains a shell.nix.
51+
derivation-path: ./tests/integration_tests
5052
working-directory: ./tests/integration_tests
5153
options: |
5254
--arg customVarBool true
@@ -55,7 +57,7 @@ jobs:
5557
echo "FIRST" > integration_test.out
5658
echo "${CUSTOM_VAR_BOOL}" >> integration_test.out
5759
echo "${CUSTOM_VAR_STR}" >> integration_test.out
58-
- name: Confirm output
60+
- name: Confirm output for Execute in directory with shell.nix
5961
shell: bash
6062
run: |
6163
output="$(<./tests/integration_tests/integration_test.out)"
@@ -67,6 +69,26 @@ jobs:
6769
)"
6870
[[ "${output}" == "${expected}" ]] || \
6971
(echo >&2 "Ouptput from integration test does not match:" "${output}"; exit 1)
72+
- name: Execute in directory without shell.nix
73+
uses: ./
74+
with:
75+
verbose: true
76+
# Demonstrate passing a path to a shell.nix.
77+
derivation-path: ./tests/integration_tests/shell.nix
78+
options: |
79+
--argstr customVarStr "Hello, World!"
80+
run: |
81+
echo "${CUSTOM_VAR_STR}" >> integration_test2.out
82+
- name: Confirm output for Execute in directory without shell.nix
83+
shell: bash
84+
run: |
85+
output="$(<./integration_test2.out)"
86+
expected="$(cat <<-EOF
87+
Hello, World!
88+
EOF
89+
)"
90+
[[ "${output}" == "${expected}" ]] || \
91+
(echo >&2 "Ouptput from integration test does not match:" "${output}"; exit 1)
7092
7193
all_ci_tests:
7294
runs-on: ubuntu-latest

action.yaml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,14 @@ inputs:
1414
description: Any parameters that are to be passed to nix-shell are specified here.
1515
working-directory:
1616
type: string
17-
description: The path where the nix-shell execution should take place.
17+
description: |
18+
The path where the script will be executed. This is not the path where nix-shell is executed.
1819
default: .
20+
derivation-path:
21+
type: string
22+
description: |
23+
The path to directory or the shell.nix or default.nix to use to set up the environment. This
24+
is the directory where nix-shell is executed.
1925
verbose:
2026
type: boolean
2127
description: Enable debug output written to stderr.
@@ -30,5 +36,6 @@ runs:
3036
RNS_OPTS: ${{ inputs.options }}
3137
RNS_RUN: ${{ inputs.run }}
3238
RNS_PURE: ${{ inputs.pure }}
39+
RNS_DERIVATION_PATH: ${{ inputs.derivation-path }}
3340
RNS_VERBOSE: ${{ inputs.verbose }}
3441
run: ${GITHUB_ACTION_PATH}/tools/run_nix_shell.sh

tests/tools_tests/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ run_nix_shell_test(name = "custom_var_bool_test")
44

55
run_nix_shell_test(name = "custom_var_str_test")
66

7+
run_nix_shell_test(name = "derivation_path_test")
8+
79
run_nix_shell_test(name = "multi_line_script_test")
810

911
run_nix_shell_test(name = "pure_test")
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
assert_msg="custom derivation path via RNS_DERIVATION_PATH"
2+
derivation_path="${PWD}/shell.nix"
3+
another_dir="${PWD}/somewhere_else"
4+
mkdir -p "${another_dir}"
5+
output="$(
6+
RNS_DERIVATION_PATH="${derivation_path}" \
7+
RNS_CWD="${another_dir}" \
8+
"${run_nix_shell_sh}" 'echo "${CUSTOM_VAR_STR}"'
9+
)"
10+
assert_match "default" "${output}" "${assert_msg}"

tools/run_nix_shell.sh

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

1928
cwd="${RNS_CWD:-}"
2029
script="${RNS_RUN:-}"
30+
derivation_path="${RNS_DERIVATION_PATH:-}"
2131

2232
pure="${RNS_PURE:-true}"
2333
verbose="${RNS_VERBOSE:-false}"
@@ -70,11 +80,13 @@ if is_verbose; then
7080
RNS_CWD: ${RNS_CWD:-}
7181
RNS_OPTS: ${RNS_OPTS:-}
7282
RNS_RUN: ${RNS_RUN:-}
83+
RNS_DERIVATION_PATH: ${RNS_DERIVATION_PATH:-}
7384
RNS_PURE: ${RNS_PURE:-}
7485
cwd: ${cwd:-}
7586
nix_shell_opts: $( printf "%q " "${nix_shell_opts[@]}" )
7687
pure: ${pure:-}
7788
script: ${script:-}
89+
derivation_path: ${derivation_path:-}
7890
===
7991
EOF
8092
)"
@@ -83,6 +95,30 @@ fi
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+
86122
if [[ "${pure}" == "true" ]]; then
87123
nix_shell_opts+=( --pure )
88124
fi
@@ -101,16 +137,22 @@ if [[ -n "${RNS_OPTS:-}" ]]; then
101137
fi
102138

103139
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."
105141
fi
106142

107143
# MARK - Execute script
108144

109145
# Change to the specified working directory
110146
if [[ -n "${cwd:-}" ]]; then
111-
cd "${cwd}"
147+
cd_cmd="cd ${cwd}"
112148
fi
113149

150+
script="$(cat <<-EOF
151+
${cd_cmd:-}
152+
${script}
153+
EOF
154+
)"
155+
114156
cmd=( nix-shell )
115157
if [[ ${#nix_shell_opts[@]} -gt 0 ]]; then
116158
cmd+=( "${nix_shell_opts[@]}" )
@@ -120,6 +162,7 @@ cmd+=( --run "${script}" )
120162
if is_verbose; then
121163
verbose_output="$(cat <<-EOF
122164
=== run_nix_shell command-line invocation ===
165+
pwd: ${PWD}
123166
$( printf "%q " "${cmd[@]}" )
124167
===
125168
EOF

0 commit comments

Comments
 (0)