Skip to content

Instantly share code, notes, and snippets.

@nrdxp
Created September 27, 2022 21:04
Show Gist options
  • Save nrdxp/264739e30935f8c775972608b1decb98 to your computer and use it in GitHub Desktop.
Save nrdxp/264739e30935f8c775972608b1decb98 to your computer and use it in GitHub Desktop.
extending `std` actions
{
std,
nixpkgs,
...
}: {
nomadJobManifests = name: let
type = std.nomadJobManifests name;
in
type
// {
actions = {
system,
flake,
fragment,
fragmentRelPath,
} @ args: let
inherit (nixpkgs.legacyPackages.${system}) jq gnused wl-clipboard xsel;
jq' = "${jq}/bin/jq";
sed = "${gnused}/bin/sed";
actions = type.actions args;
command = ''
set -e
cell="${baseNameOf (dirOf fragmentRelPath)}"
env="''${cell//Jobs/}"
src=$(nix eval ".#${system}.cloud.constants.envs.$env" --apply 'env: env.src or null' --json)
if [[ "$src" != "null" ]]; then
read -r ref hash < <(${jq'} -r '.ref + " " + .narHash' <<< "$src")
expected=$(nix flake info ".?ref=$ref" --json | ${jq'} -r '.locked.narHash')
if [[ "$hash" != "$expected" ]]; then
update_failed () {
cat <<- EOF >&2
Failed to automatically update hash, please update the hash manually.
For convenience, it will now be copied to the system clipboard.
EOF
if [[ "${system}" =~ "darwin" ]]; then
printf "%s" "$expected" | pbcopy
elif [[ -v WAYLAND_DISPLAY ]]; then
printf "%s" "$expected" | ${wl-clipboard}/bin/wl-copy
else
printf "%s" "$expected" | ${xsel}/bin/xsel -i
fi
}
cat <<- EOF >&2
error: 'narHash' mismatch
in env: $env
for ref: $ref
declared hash: $hash
correct hash: $expected
EOF
>&2 echo "Attempting to update the hash automatically..."
# validate that the hash is valid or fail
# this avoids mangling other text in the file
if nix hash to-base64 "$hash" > /dev/null; then
constants="$PRJ_ROOT/nix/cloud/constants.nix"
if ! ${sed} -i '/'"$hash"'/,''${s//'"$expected"'/;b};$q1' "$constants"; then
update_failed
fi
echo "Automatic update of hash succeeded! Commit it to try again."
else
update_failed
fi
exit 1
fi
fi
'';
in
map (action: action // {command = command + action.command;}) actions;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment