Skip to content
Snippets Groups Projects
update-hashes.nix 1.54 KiB
{
  pkgs,
  buildRoot,
  projectDefinition,
  workingDirectory,
  ...
}: let
  serviceSourcePath = "${buildRoot}/";
  common = import ./common.nix {inherit pkgs;};
in ''
  #!${pkgs.bash}/bin/bash

  ${common}
  
  defaultHash="${pkgs.lib.fakeHash}"

  echo_step "Reset vendorHash to default value: $defaultHash"
  ${pkgs.gnused}/bin/sed -i "s|vendorHash\s*=\s*null|vendorHash = \"$defaultHash\"|g" ${workingDirectory}/project.nix
  ${pkgs.gnused}/bin/sed -i "s|vendorHash\s*=\s*\"[^\"]\"|vendorHash = \"$defaultHash\"|g" ${workingDirectory}/project.nix
  
  RESULT=$(${pkgs.nix}/bin/nix build .#goPkg 2>&1)
  if [[ $? -ne 0 ]]; then

    ## check if  RESULT contain vendorHash = null; then set to null and rebuild
    if [[ "$RESULT" == *"vendorHash = null"* ]]; then
        ${pkgs.gnused}/bin/sed -i "s|vendorHash\s*=\s*\".*\"|vendorHash = null|g" ${workingDirectory}/project.nix
        RESULT=$(${pkgs.nix}/bin/nix build .#goPkg 2>&1)

        if [[ $? -ne 0 ]]; then
            echo_fail "Could not get the hash"
            exit 1
        fi

        echo_ok "No dependencies found and vendorHash is set to null."
        exit 0

    fi

  fi

  HASH=$(echo "$RESULT" | grep 'got:'  | awk '{print $2}')
  if [[ -z "$HASH" ]]; then
    echo_fail "Could not get the hash"
    ${pkgs.gnused}/bin/sed -i 's|vendorHash\s*=\s*".*"|vendorHash = null|g' ${workingDirectory}/project.nix
    exit 1
  fi

  ${pkgs.gnused}/bin/sed -i "s|vendorHash\s*=\s*\".*\"|vendorHash = \"''${HASH}\"|g" ${workingDirectory}/project.nix
  echo_ok "Hash updated, new hash is ''${HASH}'"


''