2024-05-28 19:43:04 +03:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
2023-11-13 23:04:34 +02:00
|
|
|
source ../common.sh
|
|
|
|
|
2024-06-16 13:51:46 +03:00
|
|
|
TODO_NixOS
|
|
|
|
|
2023-11-13 23:04:34 +02:00
|
|
|
clearStore
|
2024-09-19 19:43:22 +03:00
|
|
|
rm -rf "$TEST_HOME/.cache" "$TEST_HOME/.config" "$TEST_HOME/.local"
|
2023-11-13 23:04:34 +02:00
|
|
|
|
|
|
|
# Create flake under test.
|
2024-09-19 19:43:22 +03:00
|
|
|
cp ../shell-hello.nix "$config_nix" "$TEST_HOME/"
|
|
|
|
cat <<EOF >"$TEST_HOME/flake.nix"
|
2023-11-13 23:04:34 +02:00
|
|
|
{
|
|
|
|
inputs.nixpkgs.url = "$TEST_HOME/nixpkgs";
|
|
|
|
outputs = {self, nixpkgs}: {
|
|
|
|
packages.$system.hello = (import ./config.nix).mkDerivation {
|
|
|
|
name = "hello";
|
|
|
|
outputs = [ "out" "dev" ];
|
|
|
|
meta.outputsToInstall = [ "out" ];
|
|
|
|
buildCommand = "";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
EOF
|
|
|
|
|
|
|
|
# Create fake nixpkgs flake.
|
2024-09-19 19:43:22 +03:00
|
|
|
mkdir -p "$TEST_HOME/nixpkgs"
|
|
|
|
cp "${config_nix}" ../shell.nix "$TEST_HOME/nixpkgs"
|
2024-11-01 15:56:50 +02:00
|
|
|
|
2024-11-07 14:46:37 +02:00
|
|
|
# `config.nix` cannot be gotten via build dir / env var (runs afoul pure eval). Instead get from flake.
|
|
|
|
removeBuildDirRef "$TEST_HOME/nixpkgs"/*.nix
|
|
|
|
|
2024-09-19 19:43:22 +03:00
|
|
|
cat <<EOF >"$TEST_HOME/nixpkgs/flake.nix"
|
2023-11-13 23:04:34 +02:00
|
|
|
{
|
|
|
|
outputs = {self}: {
|
|
|
|
legacyPackages.$system.bashInteractive = (import ./shell.nix {}).bashInteractive;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
EOF
|
|
|
|
|
2024-09-19 19:43:22 +03:00
|
|
|
cd "$TEST_HOME"
|
2023-11-13 23:04:34 +02:00
|
|
|
|
|
|
|
# Test whether `nix develop` passes through environment variables.
|
|
|
|
[[ "$(
|
|
|
|
ENVVAR=a nix develop --no-write-lock-file .#hello <<EOF
|
|
|
|
echo "\$ENVVAR"
|
|
|
|
EOF
|
|
|
|
)" = "a" ]]
|
|
|
|
|
2024-09-12 04:04:52 +03:00
|
|
|
# Test whether `nix develop --ignore-env` does _not_ pass through environment variables.
|
2023-11-13 23:04:34 +02:00
|
|
|
[[ -z "$(
|
2024-09-12 04:04:52 +03:00
|
|
|
ENVVAR=a nix develop --ignore-env --no-write-lock-file .#hello <<EOF
|
2023-11-13 23:04:34 +02:00
|
|
|
echo "\$ENVVAR"
|
|
|
|
EOF
|
|
|
|
)" ]]
|
|
|
|
|
2024-09-19 19:43:22 +03:00
|
|
|
# Test wether `--keep-env-var` keeps the environment variable.
|
|
|
|
(
|
|
|
|
expect='BAR'
|
|
|
|
got="$(FOO='BAR' nix develop --ignore-env --keep-env-var FOO --no-write-lock-file .#hello <<EOF
|
|
|
|
echo "\$FOO"
|
|
|
|
EOF
|
|
|
|
)"
|
|
|
|
[[ "$got" == "$expect" ]]
|
|
|
|
)
|
|
|
|
|
|
|
|
# Test wether duplicate `--keep-env-var` keeps the environment variable.
|
|
|
|
(
|
|
|
|
expect='BAR'
|
|
|
|
got="$(FOO='BAR' nix develop --ignore-env --keep-env-var FOO --keep-env-var FOO --no-write-lock-file .#hello <<EOF
|
|
|
|
echo "\$FOO"
|
|
|
|
EOF
|
|
|
|
)"
|
|
|
|
[[ "$got" == "$expect" ]]
|
|
|
|
)
|
|
|
|
|
|
|
|
# Test wether `--set-env-var` sets the environment variable.
|
|
|
|
(
|
|
|
|
expect='BAR'
|
|
|
|
got="$(nix develop --ignore-env --set-env-var FOO 'BAR' --no-write-lock-file .#hello <<EOF
|
|
|
|
echo "\$FOO"
|
|
|
|
EOF
|
|
|
|
)"
|
|
|
|
[[ "$got" == "$expect" ]]
|
|
|
|
)
|
|
|
|
|
|
|
|
# Test that `--set-env-var` overwrites previously set variables.
|
|
|
|
(
|
|
|
|
expect='BLA'
|
|
|
|
got="$(FOO='BAR' nix develop --set-env-var FOO 'BLA' --no-write-lock-file .#hello <<EOF
|
|
|
|
echo "\$FOO"
|
|
|
|
EOF
|
|
|
|
)"
|
|
|
|
[[ "$got" == "$expect" ]]
|
|
|
|
)
|
|
|
|
|
|
|
|
# Test that multiple `--set-env-var` work.
|
|
|
|
(
|
|
|
|
expect='BARFOO'
|
|
|
|
got="$(nix develop --set-env-var FOO 'BAR' --set-env-var BAR 'FOO' --no-write-lock-file .#hello <<EOF | tr -d '\n'
|
|
|
|
echo "\$FOO"
|
|
|
|
echo "\$BAR"
|
|
|
|
EOF
|
|
|
|
)"
|
|
|
|
[[ "$got" == "$expect" ]]
|
|
|
|
)
|
|
|
|
|
|
|
|
# Check that we throw an error when `--keep-env-var` is used without `--ignore-env`.
|
|
|
|
expectStderr 1 nix develop --keep-env-var FOO .#hello |
|
|
|
|
grepQuiet "error: --keep-env-var does not make sense without --ignore-env"
|
|
|
|
|
|
|
|
# Check that we throw an error when `--unset-env-var` is used with `--ignore-env`.
|
|
|
|
expectStderr 1 nix develop --ignore-env --unset-env-var FOO .#hello |
|
|
|
|
grepQuiet "error: --unset-env-var does not make sense with --ignore-env"
|
|
|
|
|
|
|
|
# Test wether multiple occurances of `--set-env-var` throws.
|
|
|
|
expectStderr 1 nix develop --set-env-var FOO 'BAR' --set-env-var FOO 'BLA' --no-write-lock-file .#hello |
|
|
|
|
grepQuiet "error: Duplicate definition of environment variable 'FOO' with '--set-env-var' is ambiguous"
|
|
|
|
|
|
|
|
# Test wether similar `--unset-env-var` and `--set-env-var` throws.
|
|
|
|
expectStderr 1 nix develop --set-env-var FOO 'BAR' --unset-env-var FOO --no-write-lock-file .#hello |
|
|
|
|
grepQuiet "error: Cannot unset environment variable 'FOO' that is set with '--set-env-var'"
|
|
|
|
|
|
|
|
expectStderr 1 nix develop --unset-env-var FOO --set-env-var FOO 'BAR' --no-write-lock-file .#hello |
|
|
|
|
grepQuiet "error: Cannot set environment variable 'FOO' that is unset with '--unset-env-var'"
|
|
|
|
|
|
|
|
# Check that multiple `--ignore-env`'s are okay.
|
|
|
|
expectStderr 0 nix develop --ignore-env --set-env-var FOO 'BAR' --ignore-env .#hello
|
|
|
|
|
2023-11-13 23:04:34 +02:00
|
|
|
# Determine the bashInteractive executable.
|
|
|
|
nix build --no-write-lock-file './nixpkgs#bashInteractive' --out-link ./bash-interactive
|
|
|
|
BASH_INTERACTIVE_EXECUTABLE="$PWD/bash-interactive/bin/bash"
|
|
|
|
|
|
|
|
# Test whether `nix develop` sets `SHELL` to nixpkgs#bashInteractive shell.
|
|
|
|
[[ "$(
|
|
|
|
SHELL=custom nix develop --no-write-lock-file .#hello <<EOF
|
|
|
|
echo "\$SHELL"
|
|
|
|
EOF
|
|
|
|
)" -ef "$BASH_INTERACTIVE_EXECUTABLE" ]]
|
|
|
|
|
|
|
|
# Test whether `nix develop` with ignore environment sets `SHELL` to nixpkgs#bashInteractive shell.
|
|
|
|
[[ "$(
|
2024-09-12 04:04:52 +03:00
|
|
|
SHELL=custom nix develop --ignore-env --no-write-lock-file .#hello <<EOF
|
2023-11-13 23:04:34 +02:00
|
|
|
echo "\$SHELL"
|
|
|
|
EOF
|
|
|
|
)" -ef "$BASH_INTERACTIVE_EXECUTABLE" ]]
|
|
|
|
|
|
|
|
clearStore
|