Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 73957a6

Browse files
committedMay 20, 2021
Make nix-shell support content-addressed derivations
Resolve the derivation before trying to load its environment − essentially reproducing what the build loop does − so that we can effectively access our dependencies (and not just their placeholders). Fix #4821
1 parent af4ff64 commit 73957a6

File tree

5 files changed

+42
-6
lines changed

5 files changed

+42
-6
lines changed
 

‎src/nix-build/nix-build.cc

+7
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,13 @@ static void main_nix_build(int argc, char * * argv)
387387

388388
if (dryRun) return;
389389

390+
if (settings.isExperimentalFeatureEnabled("ca-derivations")) {
391+
auto resolvedDrv = drv.tryResolve(*store);
392+
if (!resolvedDrv)
393+
throw Error("unable to resolve the derivation '%s'. nix-shell can’t continue", drvInfo.queryDrvPath());
394+
drv = *resolvedDrv;
395+
}
396+
390397
// Set the environment.
391398
auto env = getEnv();
392399

‎tests/ca/nix-shell.sh

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env bash
2+
3+
source common.sh
4+
5+
sed -i 's/experimental-features .*/& ca-derivations ca-references nix-command flakes/' "$NIX_CONF_DIR"/nix.conf
6+
7+
CONTENT_ADDRESSED=true
8+
cd ..
9+
source ./nix-shell.sh
10+

‎tests/local.mk

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ nix_tests = \
4646
ca/build.sh \
4747
ca/substitute.sh \
4848
ca/signatures.sh \
49+
ca/nix-shell.sh \
4950
ca/nix-run.sh \
5051
ca/nix-copy.sh
5152
# parallel.sh

‎tests/nix-shell.sh

+10-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ source common.sh
22

33
clearStore
44

5+
if [[ -n ${CONTENT_ADDRESSED:-} ]]; then
6+
nix-shell () {
7+
command nix-shell --arg contentAddressed true "$@"
8+
}
9+
fi
10+
511
# Test nix-shell -A
612
export IMPURE_VAR=foo
713
export SELECTED_IMPURE_VAR=baz
@@ -41,15 +47,15 @@ output=$(NIX_PATH=nixpkgs=shell.nix nix-shell --pure -p foo bar --run 'echo "$(f
4147
[ "$output" = "foo bar" ]
4248

4349
# Test nix-shell shebang mode
44-
sed -e "s|@ENV_PROG@|$(type -p env)|" shell.shebang.sh > $TEST_ROOT/shell.shebang.sh
50+
sed -e "s|@ENV_PROG@|$(type -P env)|" shell.shebang.sh > $TEST_ROOT/shell.shebang.sh
4551
chmod a+rx $TEST_ROOT/shell.shebang.sh
4652

4753
output=$($TEST_ROOT/shell.shebang.sh abc def)
4854
[ "$output" = "foo bar abc def" ]
4955

5056
# Test nix-shell shebang mode again with metacharacters in the filename.
5157
# First word of filename is chosen to not match any file in the test root.
52-
sed -e "s|@ENV_PROG@|$(type -p env)|" shell.shebang.sh > $TEST_ROOT/spaced\ \\\'\"shell.shebang.sh
58+
sed -e "s|@ENV_PROG@|$(type -P env)|" shell.shebang.sh > $TEST_ROOT/spaced\ \\\'\"shell.shebang.sh
5359
chmod a+rx $TEST_ROOT/spaced\ \\\'\"shell.shebang.sh
5460

5561
output=$($TEST_ROOT/spaced\ \\\'\"shell.shebang.sh abc def)
@@ -58,15 +64,15 @@ output=$($TEST_ROOT/spaced\ \\\'\"shell.shebang.sh abc def)
5864
# Test nix-shell shebang mode for ruby
5965
# This uses a fake interpreter that returns the arguments passed
6066
# This, in turn, verifies the `rc` script is valid and the `load()` script (given using `-e`) is as expected.
61-
sed -e "s|@SHELL_PROG@|$(type -p nix-shell)|" shell.shebang.rb > $TEST_ROOT/shell.shebang.rb
67+
sed -e "s|@SHELL_PROG@|$(type -P nix-shell)|" shell.shebang.rb > $TEST_ROOT/shell.shebang.rb
6268
chmod a+rx $TEST_ROOT/shell.shebang.rb
6369

6470
output=$($TEST_ROOT/shell.shebang.rb abc ruby)
6571
[ "$output" = '-e load(ARGV.shift) -- '"$TEST_ROOT"'/shell.shebang.rb abc ruby' ]
6672

6773
# Test nix-shell shebang mode for ruby again with metacharacters in the filename.
6874
# Note: fake interpreter only space-separates args without adding escapes to its output.
69-
sed -e "s|@SHELL_PROG@|$(type -p nix-shell)|" shell.shebang.rb > $TEST_ROOT/spaced\ \\\'\"shell.shebang.rb
75+
sed -e "s|@SHELL_PROG@|$(type -P nix-shell)|" shell.shebang.rb > $TEST_ROOT/spaced\ \\\'\"shell.shebang.rb
7076
chmod a+rx $TEST_ROOT/spaced\ \\\'\"shell.shebang.rb
7177

7278
output=$($TEST_ROOT/spaced\ \\\'\"shell.shebang.rb abc ruby)

‎tests/shell.nix

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
1-
{ inNixShell ? false }:
1+
{ inNixShell ? false, contentAddressed ? false }:
22

3-
with import ./config.nix;
3+
let cfg = import ./config.nix; in
4+
with cfg;
5+
6+
let
7+
mkDerivation =
8+
if contentAddressed then
9+
args: cfg.mkDerivation ({
10+
__contentAddressed = true;
11+
outputHashMode = "recursive";
12+
outputHashAlgo = "sha256";
13+
} // args)
14+
else cfg.mkDerivation;
15+
in
416

517
let pkgs = rec {
618
setupSh = builtins.toFile "setup" ''

0 commit comments

Comments
 (0)
Please sign in to comment.