Jump to content

Appimage

From Official NixOS Wiki
Revision as of 16:46, 7 October 2025 by Ardenet (talk | contribs) (Created page with "=== 将 AppImage 文件作为二进制类型注册到 binfmt_misc ===")

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";
  name = "${pname}-${version}";

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.wrapType1 {
  inherit name src;

extraInstallCommands = ''
    mv $out/bin/${name} $out/bin/${pname}
    install -m 444 -D ${appimageContents}/${pname}.desktop -t $out/share/applications
    substituteInPlace $out/share/applications/${pname}.desktop \
      --replace-fail 'Exec=AppRun' 'Exec=${pname}'
    cp -r ${appimageContents}/usr/share/icons $out/share
  '';

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

You can tell the Linux kernel to use an interpreter (e.g. appimage-run) when executing certain binary files through the use of binfmt_misc, either by filename extension or magic number matching. Below NixOS configuration registers AppImage files (ELF files with magic number "AI" + 0x02) to be run with appimage-run as interpreter.

Since NixOS 24.05, there is a binfmt option:

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

This way AppImage files can be invoked directly as if they were normal programs