From c9eec49812c9a3332b623ef44ed02ab76d6c59e1 Mon Sep 17 00:00:00 2001 From: Ross Smyth <18294397+RossSmyth@users.noreply.github.com> Date: Fri, 25 Sep 2026 11:53:47 -0400 Subject: [PATCH] _7zz: speculatively try unpacking any file 7zip supports many different archive formats. Implementing our own extensions-based detection is a bit silly, and FODs often do not have an extension. So just try it, and if it fails it fails. --- pkgs/by-name/_7/_7zz/package.nix | 81 ++++++++++++++++++++++++++++-- pkgs/by-name/_7/_7zz/setup-hook.sh | 30 +++++++++-- 2 files changed, 104 insertions(+), 7 deletions(-) diff --git a/pkgs/by-name/_7/_7zz/package.nix b/pkgs/by-name/_7/_7zz/package.nix index 5c087e4600d4..e18fe0d1929e 100644 --- a/pkgs/by-name/_7/_7zz/package.nix +++ b/pkgs/by-name/_7/_7zz/package.nix @@ -22,6 +22,7 @@ # For tests testers, + fetchurl, }: let @@ -158,9 +159,83 @@ stdenv.mkDerivation (finalAttrs: { extraArgs = [ "--version=skip" ]; }) ]; - tests.version = testers.testVersion { - package = finalAttrs.finalPackage; - command = "7zz --help"; + tests = { + version = testers.testVersion { + package = finalAttrs.finalPackage; + command = "7zz --help"; + }; + # So dmg can be tested on Linux without + # macos toolchains + undmg = stdenv.mkDerivation { + pname = "test-undmg"; + version = "none"; + + src = fetchurl { + url = "https://caido.download/releases/v0.57.0/caido-desktop-v0.57.0-mac-aarch64.dmg"; + hash = "sha256-GmpMnaGR7gYz1RvSO5xj9AA3xU1mn2IBInakmVkuG7A="; + }; + + nativeBuildInputs = [ + finalAttrs.finalPackage + ]; + + buildPhase = '' + if [[ ! -e Contents ]]; then + # Should exist + ls + exit 1 + fi + ''; + installPhase = "touch $out"; + }; + + unzip = stdenv.mkDerivation { + pname = "test-unzip"; + version = "none"; + + src = fetchurl { + url = "mirror://sourceforge/libjson/libjson_7.6.1.zip"; + hash = "sha256-ByZ6OVEDjuLgLSbMQb+OJ1Zow491EkDT543JeRguc3Y="; + }; + + nativeBuildInputs = [ + finalAttrs.finalPackage + ]; + + buildPhase = '' + if [[ ! -e libjson.h ]]; then + # Should exist + ls + exit 1 + fi + ''; + installPhase = "touch $out"; + }; + + # We do not want to decompress tarballs since + # it will just leave an uncompressed tarball in the source dir. + tarballs = stdenv.mkDerivation { + pname = "test-tarball"; + version = "none"; + + src = fetchurl { + url = "https://github.com/ip7z/7zip/archive/refs/tags/26.04.tar.gz"; + hash = "sha256-SP3G3+h57i24Z7sH8ZxKlAa/6gHVH0h4W2x6E3iB4lw="; + }; + + nativeBuildInputs = [ + finalAttrs.finalPackage + ]; + + buildPhase = '' + if [[ ! -e README.md ]]; then + # Should exist + ls + exit 1 + fi + ''; + installPhase = "touch $out"; + }; }; }; diff --git a/pkgs/by-name/_7/_7zz/setup-hook.sh b/pkgs/by-name/_7/_7zz/setup-hook.sh index 1ae5bdcef3da..cdb558c99664 100644 --- a/pkgs/by-name/_7/_7zz/setup-hook.sh +++ b/pkgs/by-name/_7/_7zz/setup-hook.sh @@ -1,5 +1,27 @@ -unpackCmdHooks+=(_tryUnpackDmg) -_tryUnpackDmg() { - if ! [[ "$curSrc" =~ \.dmg$ ]]; then return 1; fi - 7zz x -sns- -snld "$curSrc" +unpackCmdHooks+=(_tryUnpack7zip) + +# 7zip supports unpacking many formats. +# No need to implement our own inaccurate extension-based detection. +_tryUnpack7zip() { + local mime="$(file -zb --mime-type "$curSrc")" + + if [[ "$mime" == "application/x-tar" ]]; then + # Compressed tarballs should use tar instead, and 7zip doesn't + # recursively unpack. So it would just leave a .tar file in + # the build directory + return 1 + fi + + local flags=( + "-bso0" # Disable stdout + "-bsp0" # Disable progress output + "-bse0" # Disable stderr output + "-sns-" # Ignore auxilliary filesystem streams + # Allow 'dangerous' links, which are links that include '..' + # This does not include absolute paths and links that escape the archive, + # those require a higher 'dangerous level'. + "-snld" + "-y" # Accept all prompts + ) + 7zz x "${flags[@]}" "$curSrc" }