nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1{ lib
2, stdenv
3, fetchzip
4, testers
5}:
6let
7 inherit (stdenv.hostPlatform) system;
8 throwSystem = throw "Unsupported system: ${system}";
9
10 plat = {
11 x86_64-linux = "linux-x64";
12 x86_64-darwin = "macOS-x64";
13 # Balena only packages for x86 so we rely on Rosetta for Apple Silicon
14 aarch64-darwin = "macOS-x64";
15 }.${system} or throwSystem;
16
17 sha256 = {
18 x86_64-linux = "sha256-5AdMuVHYX41O+iUSUlbHLQkATY2sPb9e8y4pfmvj07U=";
19 x86_64-darwin = "sha256-5GX8dglvEgvWMnIG54G3jPxHUAKlS4E6aDMvvjYGidQ=";
20 aarch64-darwin = "sha256-5GX8dglvEgvWMnIG54G3jPxHUAKlS4E6aDMvvjYGidQ=";
21 }.${system} or throwSystem;
22
23 version = "16.5.0";
24 src = fetchzip {
25 url = "https://github.com/balena-io/balena-cli/releases/download/v${version}/balena-cli-v${version}-${plat}-standalone.zip";
26 inherit sha256;
27 };
28in
29stdenv.mkDerivation (finalAttrs: {
30 pname = "balena-cli";
31 inherit version src;
32
33 installPhase = ''
34 runHook preInstall
35
36 mkdir -p $out/bin
37 cp -r ./* $out/
38 ln -s $out/balena $out/bin/balena
39
40 runHook postInstall
41 '';
42
43 passthru.tests.version = testers.testVersion {
44 package = finalAttrs.finalPackage;
45 command = ''
46 # Override default cache directory so Balena CLI's unavoidable update check does not fail due to write permissions
47 BALENARC_DATA_DIRECTORY=./ balena --version
48 '';
49 inherit version;
50 };
51
52 # https://github.com/NixOS/nixpkgs/pull/48193/files#diff-b65952dbe5271c002fbc941b01c3586bf5050ad0e6aa6b2fcc74357680e103ea
53 preFixup =
54 if stdenv.isLinux then
55 let
56 libPath = lib.makeLibraryPath [ stdenv.cc.cc ];
57 in
58 ''
59 orig_size=$(stat --printf=%s $out/balena)
60 patchelf --set-interpreter "$(cat $NIX_CC/nix-support/dynamic-linker)" $out/balena
61 patchelf --set-rpath ${libPath} $out/balena
62 chmod +x $out/balena
63 new_size=$(stat --printf=%s $out/balena)
64 ###### zeit-pkg fixing starts here.
65 # we're replacing plaintext js code that looks like
66 # PAYLOAD_POSITION = '1234 ' | 0
67 # [...]
68 # PRELUDE_POSITION = '1234 ' | 0
69 # ^-----20-chars-----^^------22-chars------^
70 # ^-- grep points here
71 #
72 # var_* are as described above
73 # shift_by seems to be safe so long as all patchelf adjustments occur
74 # before any locations pointed to by hardcoded offsets
75 var_skip=20
76 var_select=22
77 shift_by=$(expr $new_size - $orig_size)
78 function fix_offset {
79 # $1 = name of variable to adjust
80 location=$(grep -obUam1 "$1" $out/bin/balena | cut -d: -f1)
81 location=$(expr $location + $var_skip)
82 value=$(dd if=$out/balena iflag=count_bytes,skip_bytes skip=$location \
83 bs=1 count=$var_select status=none)
84 value=$(expr $shift_by + $value)
85 echo -n $value | dd of=$out/balena bs=1 seek=$location conv=notrunc
86 }
87 fix_offset PAYLOAD_POSITION
88 fix_offset PRELUDE_POSITION
89 '' else '''';
90 dontStrip = true;
91
92 meta = with lib; {
93 description = "A command line interface for balenaCloud or openBalena";
94 longDescription = ''
95 The balena CLI is a Command Line Interface for balenaCloud or openBalena. It is a software
96 tool available for Windows, macOS and Linux, used through a command prompt / terminal window.
97 It can be used interactively or invoked in scripts. The balena CLI builds on the balena API
98 and the balena SDK, and can also be directly imported in Node.js applications.
99 '';
100 homepage = "https://github.com/balena-io/balena-cli";
101 changelog = "https://github.com/balena-io/balena-cli/blob/v${version}/CHANGELOG.md";
102 license = licenses.asl20;
103 maintainers = [ maintainers.kalebpace ];
104 platforms = platforms.linux ++ platforms.darwin;
105 sourceProvenance = [ sourceTypes.binaryNativeCode ];
106 mainProgram = "balena";
107 };
108})