Skip to content

Commit

Permalink
chore(hasura-cli): move to tools (#683)
Browse files Browse the repository at this point in the history
  • Loading branch information
KaiserKarel authored Sep 12, 2023
1 parent 5c7df60 commit d3f7ed2
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 77 deletions.
1 change: 1 addition & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
./tools/rust/crane.nix
./tools/tera/tera.nix
./tools/docgen/docgen.nix
./tools/hasura-cli/hasura-cli.nix
./tools/todo-comment.nix
./networks/e2e-setup.nix
./networks/devnet.nix
Expand Down
77 changes: 0 additions & 77 deletions hubble/hubble.nix
Original file line number Diff line number Diff line change
Expand Up @@ -23,83 +23,6 @@
Env = [ "SSL_CERT_FILE=${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt" ];
};
};

hasura-cli =
let
cli-ext-hashes = {
linux-amd64 = "sha256-4XDLKIO/nT6LVaM6aSBpvKOMGhW5ca04iA1PMGBTWI8=";
linux-arm64 = "sha256-GJ9Xtx1g0nIbirS9SOb/B8AeNhAMq6PDwx2HMiqXW9Q=";
darwin-amd64 = "sha256-9qoW9SlGVafNP/t8xh3JMkpwWZ4BDR0QuEJO8g2sLHg=";
darwin-arm64 = "sha256-wwWk/mOdnCeIan18Mj6qmwbmBUmtA8eqtR4g0UHrNBo=";
};
cli-ext = pkgs.stdenv.mkDerivation rec {
pname = "hasura-cli-ext";
version = "2.4.0-beta.3";
src = pkgs.fetchurl {
url = "https://graphql-engine-cdn.hasura.io/cli-ext/releases/versioned/v${version}/cli-ext-${pkgs.go.GOOS}-${pkgs.go.GOARCH}";
sha256 = cli-ext-hashes."${pkgs.go.GOOS}-${pkgs.go.GOARCH}";
};
dontUnpack = true;
phases = [ "installPhase" "fixupPhase" ];
installPhase = ''
mkdir -p $out/bin
cp $src $out/bin/cli-ext
chmod +x $out/bin/cli-ext
'';
preFixup =
let
libPath = pkgs.lib.makeLibraryPath [ pkgs.stdenv.cc.cc ];
in
''
orig_size=$(stat --printf=%s $out/bin/cli-ext)
patchelf --set-interpreter "$(cat $NIX_CC/nix-support/dynamic-linker)" $out/bin/cli-ext
patchelf --set-rpath ${libPath} $out/bin/cli-ext
new_size=$(stat --printf=%s $out/bin/cli-ext)
###### zeit-pkg fixing starts here.
# we're replacing plaintext js code that looks like
# PAYLOAD_POSITION = '1234 ' | 0
# [...]
# PRELUDE_POSITION = '1234 ' | 0
# ^-----20-chars-----^^------22-chars------^
# ^-- grep points here
#
# var_* are as described above
# shift_by seems to be safe so long as all patchelf adjustments occur
# before any locations pointed to by hardcoded offsets
var_skip=20
var_select=22
shift_by=$(expr $new_size - $orig_size)
function fix_offset {
# $1 = name of variable to adjust
location=$(grep -obUam1 "$1" $out/bin/cli-ext | cut -d: -f1)
location=$(expr $location + $var_skip)
value=$(dd if=$out/bin/cli-ext iflag=count_bytes,skip_bytes skip=$location \
bs=1 count=$var_select status=none)
value=$(expr $shift_by + $value)
echo -n $value | dd of=$out/bin/cli-ext bs=1 seek=$location conv=notrunc
}
fix_offset PAYLOAD_POSITION
fix_offset PRELUDE_POSITION
'';
dontStrip = true;
};
in
pkgs.symlinkJoin {
name = "hasura";
paths = [ pkgs.hasura-cli ];
buildInputs = [ pkgs.makeWrapper ];
postBuild = ''
wrapProgram $out/bin/hasura \
--add-flags "--cli-ext-path ${cli-ext}/bin/cli-ext"
'';
};
};
};
}
7 changes: 7 additions & 0 deletions tools/hasura-cli/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Hasura-CLI

The hasura CLI can be used for local development. This version is created to avoid dynamically installing plugins, and instead builds and patches them directly.

## Usage

After running `nix develop`, call the cli with `hasura`.
83 changes: 83 additions & 0 deletions tools/hasura-cli/hasura-cli.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
{ ... }: {
perSystem = { pkgs, ... }:
{
packages = {
hasura-cli =
let
cli-ext-hashes = {
linux-amd64 = "sha256-4XDLKIO/nT6LVaM6aSBpvKOMGhW5ca04iA1PMGBTWI8=";
linux-arm64 = "sha256-GJ9Xtx1g0nIbirS9SOb/B8AeNhAMq6PDwx2HMiqXW9Q=";
darwin-amd64 = "sha256-9qoW9SlGVafNP/t8xh3JMkpwWZ4BDR0QuEJO8g2sLHg=";
darwin-arm64 = "sha256-wwWk/mOdnCeIan18Mj6qmwbmBUmtA8eqtR4g0UHrNBo=";
};
cli-ext = pkgs.stdenv.mkDerivation rec {
pname = "hasura-cli-ext";
version = "2.4.0-beta.3";
src = pkgs.fetchurl {
url = "https://graphql-engine-cdn.hasura.io/cli-ext/releases/versioned/v${version}/cli-ext-${pkgs.go.GOOS}-${pkgs.go.GOARCH}";
sha256 = cli-ext-hashes."${pkgs.go.GOOS}-${pkgs.go.GOARCH}";
};
dontUnpack = true;
phases = [ "installPhase" "fixupPhase" ];
installPhase = ''
mkdir -p $out/bin
cp $src $out/bin/cli-ext
chmod +x $out/bin/cli-ext
'';
preFixup =
let
libPath = pkgs.lib.makeLibraryPath [ pkgs.stdenv.cc.cc ];
in
''
orig_size=$(stat --printf=%s $out/bin/cli-ext)
patchelf --set-interpreter "$(cat $NIX_CC/nix-support/dynamic-linker)" $out/bin/cli-ext
patchelf --set-rpath ${libPath} $out/bin/cli-ext
new_size=$(stat --printf=%s $out/bin/cli-ext)
###### zeit-pkg fixing starts here.
# we're replacing plaintext js code that looks like
# PAYLOAD_POSITION = '1234 ' | 0
# [...]
# PRELUDE_POSITION = '1234 ' | 0
# ^-----20-chars-----^^------22-chars------^
# ^-- grep points here
#
# var_* are as described above
# shift_by seems to be safe so long as all patchelf adjustments occur
# before any locations pointed to by hardcoded offsets
var_skip=20
var_select=22
shift_by=$(expr $new_size - $orig_size)
function fix_offset {
# $1 = name of variable to adjust
location=$(grep -obUam1 "$1" $out/bin/cli-ext | cut -d: -f1)
location=$(expr $location + $var_skip)
value=$(dd if=$out/bin/cli-ext iflag=count_bytes,skip_bytes skip=$location \
bs=1 count=$var_select status=none)
value=$(expr $shift_by + $value)
echo -n $value | dd of=$out/bin/cli-ext bs=1 seek=$location conv=notrunc
}
fix_offset PAYLOAD_POSITION
fix_offset PRELUDE_POSITION
'';
dontStrip = true;
};
in
pkgs.symlinkJoin {
name = "hasura";
paths = [ pkgs.hasura-cli ];
buildInputs = [ pkgs.makeWrapper ];
postBuild = ''
wrapProgram $out/bin/hasura \
--add-flags "--cli-ext-path ${cli-ext}/bin/cli-ext"
'';
};
};
};
}

0 comments on commit d3f7ed2

Please sign in to comment.