Jump to content

Appimage

From NixOS Wiki
This page is a translated version of the page Appimage and the translation is 100% complete.

AppImage 是一種 Linux 應用程式的單體打包格式。它將所有依賴項都包含在一個文件中,該文件由一個可執行文件和一個附加的文件系統組成。

用法

運行

在大多數發行版中,只需下載 .AppImage 文件,使用 chmod +x $AppImage 使其可執行,然後執行即可。但這在 NixOS 中無法直接使用,因為 AppImage 文件通常(即使並非總是)依賴於硬編碼路徑中的某些系統庫。

$ nix-shell -p appimage-run
$ appimage-run $AppImageFile

打包

請參閱 nixpkgs 手冊中包裝 AppImage 軟件的部分。簡而言之,提取 AppImage,並將所有依賴項添加為 Nix 構建依賴項。 以下示例是 Quba 程序的 Derivation,它以 AppImage 的形式分發。

{
  lib,
  appimageTools,
  fetchurl,
}:

let
  version = "1.4.0";
  pname = "quba";

  src = fetchurl {
    url = "https://github.com/ZUGFeRD/quba-viewer/releases/download/v${version}/Quba-${version}.AppImage";
    hash = "sha256-EsTF7W1np5qbQQh3pdqsFe32olvGK3AowGWjqHPEfoM=";
  };

  appimageContents = appimageTools.extractType1 { inherit name src; };
in
appimageTools.wrapType2 rec {
  inherit pname version src;

  extraInstallCommands = ''
    substituteInPlace $out/share/applications/${pname}.desktop \
      --replace-fail 'Exec=AppRun' 'Exec=${meta.mainProgram}'
  '';

  meta = {
    description = "Viewer for electronic invoices";
    homepage = "https://github.com/ZUGFeRD/quba-viewer";
    downloadPage = "https://github.com/ZUGFeRD/quba-viewer/releases";
    license = lib.licenses.asl20;
    sourceProvenance = with lib.sourceTypes; [ binaryNativeCode ];
    maintainers = with lib.maintainers; [ onny ];
    platforms = [ "x86_64-linux" ];
  };
}


配置

將 AppImage 文件作為二進制類型註冊到 binfmt_misc

您可以通過 binfmt_misc 來告訴 Linux 內核在執行某些二進制文件時使用哪個解釋器(例如 appimage-run),具體方式可以通過文件擴展名或魔數匹配來實現。下面的 NixOS 配置會註冊 AppImage 文件(魔數為「AI」+ 0x02 的 ELF 文件),並使用 appimage-run 作為解釋器運行:

programs.appimage = {
  enable = true;
  binfmt = true;
};

這樣 AppImage 文件就可以像普通程序一樣直接調用