query
On this page

normalizeHash

lib.fetchers.normalizeHash

Docs pulled from | This Revision | about 1 hour ago


Converts an attrset containing one of hash, sha256 or sha512, into one containing outputHash{,Algo} as accepted by mkDerivation.

An appropriate “fake hash” is substituted when the hash value is "", as is the convention for fetchers.

All other attributes in the set remain as-is.

Example

normalizeHash { } { hash = ""; foo = "bar"; }
=>
{
  outputHash = lib.fakeHash;
  outputHashAlgo = null;
  foo = "bar";
}
normalizeHash { } { sha256 = lib.fakeSha256; }
=>
{
  outputHash = lib.fakeSha256;
  outputHashAlgo = "sha256";
}
normalizeHash { } { sha512 = lib.fakeSha512; }
=>
{
  outputHash = lib.fakeSha512;
  outputHashAlgo = "sha512";
}

Type

normalizeHash :: { hashTypes :: List String, required :: Bool } -> AttrSet -> AttrSet

Arguments

hashTypes
the set of attribute names accepted as hash inputs, in addition to hash
required
whether to throw if no hash was present in the input; otherwise returns the original input, unmodified

Noogle detected

Implementation

The following is the current implementation of this function.

normalizeHash =
    {
      hashTypes ? [ "sha256" ],
      required ? true,
    }:
    let
      inherit (lib)
        concatMapStringsSep
        head
        tail
        throwIf
        ;
      inherit (lib.attrsets)
        attrsToList
        intersectAttrs
        removeAttrs
        optionalAttrs
        ;

      inherit (commonH hashTypes) hashNames hashSet;
    in
    args:
    if args ? "outputHash" then
      args
    else
      let
        # The argument hash, as a {name, value} pair
        h =
          # All hashes passed in arguments (possibly 0 or >1) as a list of {name, value} pairs
          let
            hashesAsNVPairs = attrsToList (intersectAttrs hashSet args);
          in
          if hashesAsNVPairs == [ ] then
            throwIf required "fetcher called without `hash`" null
          else if tail hashesAsNVPairs != [ ] then
            throw "fetcher called with mutually-incompatible arguments: ${
              concatMapStringsSep ", " (a: a.name) hashesAsNVPairs
            }"
          else
            head hashesAsNVPairs;
      in
      removeAttrs args hashNames
      // (optionalAttrs (h != null) {
        outputHashAlgo = if h.name == "hash" then null else h.name;
        outputHash =
          if h.value == "" then
            fakeH.${h.name} or (throw "no “fake hash” defined for ${h.name}")
          else
            h.value;
      });