about summary refs log tree commit diff
diff options
context:
space:
mode:
authorsuperherointj <5861043+superherointj@users.noreply.github.com>2023-03-20 17:05:47 -0300
committerGitHub <noreply@github.com>2023-03-20 17:05:47 -0300
commitabb06971b07080cff331cb14439af357b146f34d (patch)
tree0b7ce4c2b555a6a7e9a3cec54e32723e35fbd592
parent283a0ab1cbe274476630e3d43c9e0928dd6a59db (diff)
parent72f784dbbe5511cb2433a369212212e56a7d9c72 (diff)
Merge pull request #222139 from kalebpace/kpace/patch-balena-cli
balena-cli: fix linux binary with patchelf
-rw-r--r--pkgs/tools/admin/balena-cli/default.nix63
1 files changed, 56 insertions, 7 deletions
diff --git a/pkgs/tools/admin/balena-cli/default.nix b/pkgs/tools/admin/balena-cli/default.nix
index be84937ce038e..2f09fbf1afad4 100644
--- a/pkgs/tools/admin/balena-cli/default.nix
+++ b/pkgs/tools/admin/balena-cli/default.nix
@@ -1,8 +1,8 @@
 { lib
 , stdenv
 , fetchzip
+, testers
 }:
-
 let
   inherit (stdenv.hostPlatform) system;
   throwSystem = throw "Unsupported system: ${system}";
@@ -21,27 +21,76 @@ let
     aarch64-darwin = "1ihxyf35px3s6q2yk4p3dy03rcj93hy96bj3pxqlv0rp05gnsf02";
     x86_64-windows = "104hc3qvs04l2hmjmp0bcjr5g5scp4frhprk1fpszziqhdmhwa40";
   }.${system} or throwSystem;
-in
-stdenv.mkDerivation rec {
-  pname = "balena-cli";
-  version = "15.1.1";
 
+  version = "15.1.1";
   src = fetchzip {
     url = "https://github.com/balena-io/balena-cli/releases/download/v${version}/balena-cli-v${version}-${plat}-standalone.zip";
     inherit sha256;
   };
+in
+stdenv.mkDerivation (finalAttrs: {
+  pname = "balena-cli";
+  inherit version src;
 
   installPhase = ''
     runHook preInstall
 
     mkdir -p $out/bin
     cp -r ./* $out/
-
     ln -s $out/balena $out/bin/balena
 
     runHook postInstall
   '';
 
+  passthru.tests.version = testers.testVersion {
+    package = finalAttrs.finalPackage;
+    command = ''
+      # Override default cache directory so Balena CLI's unavoidable update check does not fail due to write permissions
+      BALENARC_DATA_DIRECTORY=./ balena --version
+    '';
+    inherit version;
+  };
+
+  # https://github.com/NixOS/nixpkgs/pull/48193/files#diff-b65952dbe5271c002fbc941b01c3586bf5050ad0e6aa6b2fcc74357680e103ea
+  preFixup =
+    if stdenv.isLinux then
+      let
+        libPath = lib.makeLibraryPath [ stdenv.cc.cc ];
+      in
+      ''
+        orig_size=$(stat --printf=%s $out/balena)
+        patchelf --set-interpreter "$(cat $NIX_CC/nix-support/dynamic-linker)" $out/balena
+        patchelf --set-rpath ${libPath} $out/balena
+        chmod +x $out/balena
+        new_size=$(stat --printf=%s $out/balena)
+        ###### 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/balena | cut -d: -f1)
+          location=$(expr $location + $var_skip)
+          value=$(dd if=$out/balena 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/balena bs=1 seek=$location conv=notrunc
+        }
+        fix_offset PAYLOAD_POSITION
+        fix_offset PRELUDE_POSITION
+      '' else '''';
+  dontStrip = true;
+
   meta = with lib; {
     description = "A command line interface for balenaCloud or openBalena";
     longDescription = ''
@@ -58,4 +107,4 @@ stdenv.mkDerivation rec {
     sourceProvenance = [ sourceTypes.binaryNativeCode ];
     mainProgram = "balena";
   };
-}
+})