core/pkgs/build-support/fetchs3/default.nix

59 lines
1.1 KiB
Nix
Raw Normal View History

2024-06-30 08:16:52 +00:00
{
lib,
runCommand,
awscli,
}:
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
{
s3url,
name ? builtins.baseNameOf s3url,
sha256,
region ? "us-east-1",
credentials ? null, # Default to looking at local EC2 metadata service
recursiveHash ? false,
postFetch ? null,
2024-05-02 00:46:19 +00:00
}:
let
2024-06-30 08:16:52 +00:00
mkCredentials =
{
access_key_id,
secret_access_key,
session_token ? null,
}:
{
AWS_ACCESS_KEY_ID = access_key_id;
AWS_SECRET_ACCESS_KEY = secret_access_key;
AWS_SESSION_TOKEN = session_token;
};
2024-05-02 00:46:19 +00:00
credentialAttrs = lib.optionalAttrs (credentials != null) (mkCredentials credentials);
2024-06-30 08:16:52 +00:00
in
runCommand name
(
{
nativeBuildInputs = [ awscli ];
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
outputHashAlgo = "sha256";
outputHash = sha256;
outputHashMode = if recursiveHash then "recursive" else "flat";
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
preferLocalBuild = true;
2024-05-02 00:46:19 +00:00
2024-06-30 08:16:52 +00:00
AWS_DEFAULT_REGION = region;
}
// credentialAttrs
)
(
if postFetch != null then
''
downloadedFile="$(mktemp)"
aws s3 cp ${s3url} $downloadedFile
${postFetch}
''
else
''
aws s3 cp ${s3url} $out
''
)