From b0d398ca8c27973ea3b303d18deb477fdbfdd05f Mon Sep 17 00:00:00 2001 From: Artemis Tosini Date: Sat, 2 Dec 2023 00:20:35 +0000 Subject: [PATCH] packages/jlink: bring from personal config --- flake.lock | 6 ++-- flake.nix | 5 +++- packages/jlink/default.nix | 57 +++++++++++++++++++++++++++++++++++++ packages/jlink/update.py | 52 +++++++++++++++++++++++++++++++++ packages/jlink/version.json | 1 + 5 files changed, 117 insertions(+), 4 deletions(-) create mode 100644 packages/jlink/default.nix create mode 100755 packages/jlink/update.py create mode 100644 packages/jlink/version.json diff --git a/flake.lock b/flake.lock index bcfa6af..1d0a4a5 100644 --- a/flake.lock +++ b/flake.lock @@ -2,11 +2,11 @@ "nodes": { "nixpkgs": { "locked": { - "lastModified": 1699343069, - "narHash": "sha256-s7BBhyLA6MI6FuJgs4F/SgpntHBzz40/qV0xLPW6A1Q=", + "lastModified": 1701336116, + "narHash": "sha256-kEmpezCR/FpITc6yMbAh4WrOCiT2zg5pSjnKrq51h5Y=", "owner": "nixos", "repo": "nixpkgs", - "rev": "ec750fd01963ab6b20ee1f0cb488754e8036d89d", + "rev": "f5c27c6136db4d76c30e533c20517df6864c46ee", "type": "github" }, "original": { diff --git a/flake.nix b/flake.nix index f7baba9..a964406 100644 --- a/flake.nix +++ b/flake.nix @@ -14,7 +14,10 @@ overlays = [ (final: prev: self.packages."${system}") ]; }; in { - packages = { zephyr = pkgs.callPackage ./packages/zephyr { }; }; + packages = { + jlink = pkgs.callPackage ./packages/jlink { }; + zephyr = pkgs.callPackage ./packages/zephyr { }; + }; devShells = { zephyr = pkgs.callPackage ./shells/zephyr { }; }; diff --git a/packages/jlink/default.nix b/packages/jlink/default.nix new file mode 100644 index 0000000..2c8263c --- /dev/null +++ b/packages/jlink/default.nix @@ -0,0 +1,57 @@ +{ stdenv, lib, fetchurl, autoPatchelfHook, fontconfig, freetype, libusb, libICE +, libSM, ncurses5, udev, libX11, libXext, libXcursor, libXfixes, libXrender +, libXrandr }: +let + conf = (lib.importJSON ./version.json).${stdenv.hostPlatform.system} or (throw + "unsupported system ${stdenv.hostPlatform.system}"); +in stdenv.mkDerivation rec { + pname = "jlink"; + version = conf.version; + + src = fetchurl { inherit (conf) url hash curlOpts; }; + + dontConfigure = true; + dontBuild = true; + dontStrip = true; + preferLocalBuild = true; + + nativeBuildInputs = [ autoPatchelfHook ]; + buildInputs = [ + fontconfig + freetype + libusb + libICE + libSM + ncurses5 + udev + libX11 + libXext + libXcursor + libXfixes + libXrender + libXrandr + ]; + + runtimeDependencies = [ udev ]; + + installPhase = '' + mkdir -p $out/{JLink,bin} + cp -R * $out/JLink + ln -s $out/JLink/J* $out/bin/ + install -D -t $out/lib/udev/rules.d 99-jlink.rules + ''; + + preFixup = '' + patchelf --add-needed libudev.so.1 $out/JLink/libjlinkarm.so + ''; + + passthru.updateScript = ./update.py; + + meta = with lib; { + homepage = "https://www.segger.com/downloads/jlink"; + description = "SEGGER J-Link"; + license = licenses.unfree; + platforms = [ "x86_64-linux" "i686-linux" "armv7l-linux" "aarch64-linux" ]; + maintainers = with maintainers; [ artemist ]; + }; +} diff --git a/packages/jlink/update.py b/packages/jlink/update.py new file mode 100755 index 0000000..725e778 --- /dev/null +++ b/packages/jlink/update.py @@ -0,0 +1,52 @@ +#!/usr/bin/env nix-shell +#! nix-shell -i python3 -p "python3.withPackages (ps: with ps; [ beautifulsoup4 requests lxml ] )" nix-prefetch +import requests +import bs4 +import json +import subprocess +import os + + +BASE_DIR = os.path.dirname(os.path.abspath(__file__)) +URL_FORMAT = "https://www.segger.com/downloads/jlink/JLink_Linux_V{version}_{arch}.tgz" +CURL_OPTS = "-d accept_license_agreement=accepted -d submit=Download+software" + +page = requests.get("https://www.segger.com/downloads/jlink").text +elem = ( + bs4.BeautifulSoup(page, features="lxml") + .find("select", {"class": "version"}) + .find("option") +) +version = elem.children.__next__().lstrip("V") + + +arches = [ + ("x86_64-linux", "x86_64"), + ("i686-linux", "i686"), + ("armv7l-linux", "arm"), + ("aarch64-linux", "arm64"), +] + + +out_obj = {} +for nix_arch, jlink_arch in arches: + url = URL_FORMAT.format(version=version.replace(".", ""), arch=jlink_arch) + out = subprocess.run( + [ + "nix-prefetch", + f'{{fetchurl}}: fetchurl {{ url = "{url}"; curlOpts = "{CURL_OPTS}"; }}', + ], + stdout=subprocess.PIPE, + check=True, + ) + file_hash = out.stdout.decode("utf8").strip() + out_obj[nix_arch] = { + "url": url, + "version": version, + "curlOpts": CURL_OPTS, + "hash": file_hash, + } + +out_file = open(os.path.join(BASE_DIR, "version.json"), "w") +json.dump(out_obj, out_file) +out_file.close() diff --git a/packages/jlink/version.json b/packages/jlink/version.json new file mode 100644 index 0000000..dd5ea3a --- /dev/null +++ b/packages/jlink/version.json @@ -0,0 +1 @@ +{"x86_64-linux": {"url": "https://www.segger.com/downloads/jlink/JLink_Linux_V794_x86_64.tgz", "version": "7.94", "curlOpts": "-d accept_license_agreement=accepted -d submit=Download+software", "hash": "sha256-6Gvmz/EsEBK7VZ+EW4RMiCiA0mauLBgdmKvU1FyfODY="}, "i686-linux": {"url": "https://www.segger.com/downloads/jlink/JLink_Linux_V794_i686.tgz", "version": "7.94", "curlOpts": "-d accept_license_agreement=accepted -d submit=Download+software", "hash": "sha256-lYDxkNIwuCZ9qkeZ9clBQEMx5283DgoU13SLROdr2bs="}, "armv7l-linux": {"url": "https://www.segger.com/downloads/jlink/JLink_Linux_V794_arm.tgz", "version": "7.94", "curlOpts": "-d accept_license_agreement=accepted -d submit=Download+software", "hash": "sha256-9jCd9w7TBasvP+rk6z/3E2tgOvpGOfLiYWiHxAYtAA0="}, "aarch64-linux": {"url": "https://www.segger.com/downloads/jlink/JLink_Linux_V794_arm64.tgz", "version": "7.94", "curlOpts": "-d accept_license_agreement=accepted -d submit=Download+software", "hash": "sha256-VPxD8FlPvlLQM+lFsgK88ksXbrGDkZYlR5FYl8A550s="}} \ No newline at end of file