requireFile: use lib.extendMkDerivation (#487780)

This commit is contained in:
Jo 2026-03-06 09:49:52 +00:00 committed by GitHub
commit 1648979af2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -935,67 +935,93 @@ rec {
# Docs in doc/build-helpers/fetchers.chapter.md
# See https://nixos.org/manual/nixpkgs/unstable/#requirefile
requireFile =
{
name ? null,
sha256 ? null,
sha1 ? null,
hash ? null,
url ? null,
message ? null,
hashMode ? "flat",
}:
assert (message != null) || (url != null);
assert (sha256 != null) || (sha1 != null) || (hash != null);
assert (name != null) || (url != null);
let
msg =
if message != null then
message
else
''
Unfortunately, we cannot download file ${name_} automatically.
Please go to ${url} to download it yourself, and add it to the Nix store
using either
nix-store --add-fixed ${hashAlgo} ${name_}
or
nix-prefetch-url --type ${hashAlgo} file:///path/to/${name_}
'';
hashAlgo =
if hash != null then
(builtins.head (lib.strings.splitString "-" hash))
else if sha256 != null then
"sha256"
else
"sha1";
hashAlgo_ = if hash != null then "" else hashAlgo;
hash_ =
if hash != null then
hash
else if sha256 != null then
sha256
else
sha1;
name_ = if name == null then baseNameOf (toString url) else name;
in
stdenvNoCC.mkDerivation {
name = name_;
outputHashMode = hashMode;
outputHashAlgo = hashAlgo_;
outputHash = hash_;
preferLocalBuild = true;
builder = writeScript "restrict-message" ''
source ${stdenvNoCC}/setup
cat <<_EOF_
requireFile = lib.extendMkDerivation {
constructDrv = stdenv.mkDerivation;
***
${msg}
***
excludeDrvArgNames = [
"hash"
"hashMode"
"message"
"sha1"
"sha256"
"url"
];
_EOF_
exit 1
'';
};
extendDrvArgs =
finalAttrs:
{
name ? null,
sha256 ? null,
sha1 ? null,
hash ? null,
url ? null,
message ? null,
hashMode ? "flat",
}@args:
assert (message != null) || (url != null);
assert (sha256 != null) || (sha1 != null) || (hash != null);
assert (name != null) || (url != null);
let
msg =
if message != null then
message
else
''
Unfortunately, we cannot download file ${name_} automatically.
Please go to ${url} to download it yourself, and add it to the Nix store
using either
nix-store --add-fixed ${hashAlgo} ${name_}
or
nix-prefetch-url --type ${hashAlgo} file:///path/to/${name_}
'';
hashAlgo =
if hash != null then
(builtins.head (lib.strings.splitString "-" hash))
else if sha256 != null then
"sha256"
else
"sha1";
hashAlgo_ = if hash != null then "" else hashAlgo;
hash_ =
if hash != null then
hash
else if sha256 != null then
sha256
else
sha1;
name_ = if name == null then baseNameOf (toString url) else name;
in
{
outputHashMode = hashMode;
outputHashAlgo = hashAlgo_;
outputHash = hash_;
preferLocalBuild = true;
builder = writeScript "restrict-message" ''
source ${stdenvNoCC}/setup
cat <<_EOF_
***
${msg}
***
_EOF_
exit 1
'';
}
// (lib.optionalAttrs (name == null) {
# The case of providing `url`, but not `name`. This has
# weird interactions with the positioning system
# When we set `name` explicitly here, we override where the
# position is read from. So we must fix it here.
pos = lib.unsafeGetAttrPos "url" args;
# If a name is not provided, use the basename of the url
name = builtins.warn "providing a URL without a name is deprecated" baseNameOf (toString url);
});
inheritFunctionArgs = false;
};
# TODO: move copyPathToStore docs to the Nixpkgs manual
/*