Appimage/zh: Difference between revisions
Appearance
Created page with "=== 将 AppImage 文件作为二进制类型注册到 binfmt_misc ===" |
Created page with "从 [https://nixos.org/manual/nixos/stable/release-notes#sec-release-24.05-new-services NixOS 24.05] 开始,有一个 binfmt 选项:" |
||
| Line 68: | Line 68: | ||
=== 将 AppImage 文件作为二进制类型注册到 binfmt_misc === | === 将 AppImage 文件作为二进制类型注册到 binfmt_misc === | ||
您可以通过 [https://en.wikipedia.org/wiki/Binfmt_misc#External_links binfmt_misc] 来告诉 Linux 内核在执行某些二进制文件时使用哪个解释器(例如 <code>appimage-run</code>),具体方式可以通过文件扩展名或魔数匹配来实现。下面的 NixOS 配置会注册 AppImage 文件(魔数为“AI”+ 0x02 的 ELF 文件),并使用 <code>appimage-run</code> 作为解释器运行。 | |||
从 [https://nixos.org/manual/nixos/stable/release-notes#sec-release-24.05-new-services NixOS 24.05] 开始,有一个 binfmt 选项: | |||
<syntaxhighlight lang="nixos"> | <syntaxhighlight lang="nixos"> | ||
Revision as of 16:48, 7 October 2025
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
您可以通過 binfmt_misc 來告訴 Linux 內核在執行某些二進制文件時使用哪個解釋器(例如 appimage-run),具體方式可以通過文件擴展名或魔數匹配來實現。下面的 NixOS 配置會註冊 AppImage 文件(魔數為「AI」+ 0x02 的 ELF 文件),並使用 appimage-run 作為解釋器運行。
從 NixOS 24.05 開始,有一個 binfmt 選項:
programs.appimage = {
enable = true;
binfmt = true;
};
This way AppImage files can be invoked directly as if they were normal programs