2024-05-28 19:43:04 +03:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
2012-07-09 22:41:43 +03:00
|
|
|
source common.sh
|
|
|
|
|
2024-06-16 13:51:46 +03:00
|
|
|
TODO_NixOS
|
|
|
|
|
2012-07-09 22:41:43 +03:00
|
|
|
clearStore
|
|
|
|
|
2015-10-30 13:33:40 +02:00
|
|
|
# Test fetching a flat file.
|
2015-10-30 12:27:47 +02:00
|
|
|
hash=$(nix-hash --flat --type sha256 ./fetchurl.sh)
|
2012-07-09 22:41:43 +03:00
|
|
|
|
2024-10-30 06:33:28 +02:00
|
|
|
outPath=$(nix-build -vvvvv --expr 'import <nix/fetchurl.nix>' --argstr url "file://$(pwd)/fetchurl.sh" --argstr sha256 "$hash" --no-out-link)
|
2012-07-09 22:41:43 +03:00
|
|
|
|
2024-10-30 06:33:28 +02:00
|
|
|
cmp "$outPath" fetchurl.sh
|
2015-10-30 12:27:47 +02:00
|
|
|
|
2022-02-27 16:25:22 +02:00
|
|
|
# Do not re-fetch paths already present.
|
2024-10-30 06:33:28 +02:00
|
|
|
outPath2=$(nix-build -vvvvv --expr 'import <nix/fetchurl.nix>' --argstr url file:///does-not-exist/must-remain-unused/fetchurl.sh --argstr sha256 "$hash" --no-out-link)
|
2022-02-27 16:25:22 +02:00
|
|
|
test "$outPath" == "$outPath2"
|
|
|
|
|
2017-07-04 15:47:59 +03:00
|
|
|
# Now using a base-64 hash.
|
|
|
|
clearStore
|
|
|
|
|
2020-07-24 19:44:43 +03:00
|
|
|
hash=$(nix hash file --type sha512 --base64 ./fetchurl.sh)
|
2017-07-04 15:47:59 +03:00
|
|
|
|
2024-10-30 06:33:28 +02:00
|
|
|
outPath=$(nix-build -vvvvv --expr 'import <nix/fetchurl.nix>' --argstr url "file://$(pwd)/fetchurl.sh" --argstr sha512 "$hash" --no-out-link)
|
2017-07-04 15:47:59 +03:00
|
|
|
|
2024-10-30 06:33:28 +02:00
|
|
|
cmp "$outPath" fetchurl.sh
|
2017-07-04 15:47:59 +03:00
|
|
|
|
2018-12-13 15:30:52 +02:00
|
|
|
# Now using an SRI hash.
|
|
|
|
clearStore
|
|
|
|
|
2020-07-24 19:44:43 +03:00
|
|
|
hash=$(nix hash file ./fetchurl.sh)
|
2018-12-13 15:30:52 +02:00
|
|
|
|
2018-12-15 06:37:20 +02:00
|
|
|
[[ $hash =~ ^sha256- ]]
|
2018-12-13 15:30:52 +02:00
|
|
|
|
2024-10-30 06:33:28 +02:00
|
|
|
outPath=$(nix-build -vvvvv --expr 'import <nix/fetchurl.nix>' --argstr url "file://$(pwd)/fetchurl.sh" --argstr hash "$hash" --no-out-link)
|
2018-12-13 15:30:52 +02:00
|
|
|
|
2024-10-30 06:33:28 +02:00
|
|
|
cmp "$outPath" fetchurl.sh
|
2018-12-13 15:30:52 +02:00
|
|
|
|
2020-06-12 22:39:44 +03:00
|
|
|
# Test that we can substitute from a different store dir.
|
2017-07-17 14:13:18 +03:00
|
|
|
clearStore
|
|
|
|
|
2024-10-30 06:33:28 +02:00
|
|
|
other_store="file://$TEST_ROOT/other_store?store=/fnord/store"
|
2017-07-17 14:13:18 +03:00
|
|
|
|
2020-07-24 19:44:43 +03:00
|
|
|
hash=$(nix hash file --type sha256 --base16 ./fetchurl.sh)
|
2017-07-17 14:13:18 +03:00
|
|
|
|
2024-10-30 06:33:28 +02:00
|
|
|
nix --store "$other_store" store add-file ./fetchurl.sh
|
2020-06-12 23:32:52 +03:00
|
|
|
|
2024-10-30 06:33:28 +02:00
|
|
|
outPath=$(nix-build -vvvvv --expr 'import <nix/fetchurl.nix>' --argstr url file:///no-such-dir/fetchurl.sh --argstr sha256 "$hash" --no-out-link --substituters "$other_store")
|
2017-07-17 14:13:18 +03:00
|
|
|
|
2019-06-01 16:27:43 +03:00
|
|
|
# Test hashed mirrors with an SRI hash.
|
2024-10-30 06:33:28 +02:00
|
|
|
nix-build -vvvvv --expr 'import <nix/fetchurl.nix>' --argstr url file:///no-such-dir/fetchurl.sh --argstr hash "$(nix hash to-sri --type sha256 "$hash")" \
|
|
|
|
--no-out-link --substituters "$other_store"
|
2019-06-01 16:27:43 +03:00
|
|
|
|
2015-10-30 13:33:40 +02:00
|
|
|
# Test unpacking a NAR.
|
2024-10-30 06:33:28 +02:00
|
|
|
rm -rf "$TEST_ROOT/archive"
|
|
|
|
mkdir -p "$TEST_ROOT/archive"
|
|
|
|
cp ./fetchurl.sh "$TEST_ROOT/archive"
|
|
|
|
chmod +x "$TEST_ROOT/archive/fetchurl.sh"
|
|
|
|
ln -s foo "$TEST_ROOT/archive/symlink"
|
|
|
|
nar="$TEST_ROOT/archive.nar"
|
|
|
|
nix-store --dump "$TEST_ROOT/archive" > "$nar"
|
2015-10-30 12:27:47 +02:00
|
|
|
|
2024-10-30 06:33:28 +02:00
|
|
|
hash=$(nix-hash --flat --type sha256 "$nar")
|
2015-10-30 12:27:47 +02:00
|
|
|
|
2024-10-30 06:33:28 +02:00
|
|
|
outPath=$(nix-build -vvvvv --expr 'import <nix/fetchurl.nix>' --argstr url "file://$nar" --argstr sha256 "$hash" \
|
2015-10-30 12:27:47 +02:00
|
|
|
--arg unpack true --argstr name xyzzy --no-out-link)
|
|
|
|
|
2024-10-30 06:33:28 +02:00
|
|
|
echo "$outPath" | grepQuiet 'xyzzy'
|
2015-10-30 12:27:47 +02:00
|
|
|
|
2024-10-30 06:33:28 +02:00
|
|
|
test -x "$outPath/fetchurl.sh"
|
|
|
|
test -L "$outPath/symlink"
|
2015-10-30 13:33:40 +02:00
|
|
|
|
2024-10-30 06:33:28 +02:00
|
|
|
nix-store --delete "$outPath"
|
2015-10-30 13:33:40 +02:00
|
|
|
|
|
|
|
# Test unpacking a compressed NAR.
|
2024-10-30 06:33:28 +02:00
|
|
|
narxz="$TEST_ROOT/archive.nar.xz"
|
|
|
|
rm -f "$narxz"
|
|
|
|
xz --keep "$nar"
|
|
|
|
outPath=$(nix-build -vvvvv --expr 'import <nix/fetchurl.nix>' --argstr url "file://$narxz" --argstr sha256 "$hash" \
|
2015-10-30 13:33:40 +02:00
|
|
|
--arg unpack true --argstr name xyzzy --no-out-link)
|
|
|
|
|
2024-10-30 06:33:28 +02:00
|
|
|
test -x "$outPath/fetchurl.sh"
|
|
|
|
test -L "$outPath/symlink"
|
2024-02-01 22:46:01 +02:00
|
|
|
|
|
|
|
# Make sure that *not* passing a outputHash fails.
|
2024-02-02 14:22:18 +02:00
|
|
|
requireDaemonNewerThan "2.20"
|
2024-02-02 14:07:08 +02:00
|
|
|
expected=100
|
|
|
|
if [[ -v NIX_DAEMON_PACKAGE ]]; then expected=1; fi # work around the daemon not returning a 100 status correctly
|
2024-10-30 06:33:28 +02:00
|
|
|
expectStderr $expected nix-build --expr '{ url }: builtins.derivation { name = "nix-cache-info"; system = "x86_64-linux"; builder = "builtin:fetchurl"; inherit url; outputHashMode = "flat"; }' --argstr url "file://$narxz" 2>&1 | grep 'must be a fixed-output or impure derivation'
|