Discourse 包在 nix-shell 中构建失败

尝试下载并构建 Discourse 的 Nix 包,但因疑似 pnpm 依赖错误而失败:

 ERR_PNPM_INVALID_DEPENDENCY_NAME  Refusing to place a dependency under /build/source/frontend/discourse-types/node_modules with the invalid alias "@types/@glimmer__component"

ERROR: pnpm failed to install dependencies

If you see ERR_PNPM_NO_OFFLINE_TARBALL above this, follow these to fix the issue:
1. Set pnpmDeps.hash to "" (empty string)
2. Build the derivation and wait for it to fail with a hash mismatch
3. Copy the 'got: sha256-' value back into the pnpmDeps.hash field

If you see ERR_PNPM_LOCKFILE_CONFIG_MISMATCH above this, try changing the pnpm version
Found 'pnpm' with version '10.34.4'

error: Cannot build '/nix/store/k90f0nxqhlyb1bym9lg5xl6pfjz4vamf-discourse-assets-2026.1.4.drv'.
       Reason: builder failed with exit code 1.
       Output paths:
         /nix/store/biwwabb1xl58v4ywqmmc7d4vm0sl78q3-discourse-assets-2026.1.4-node_modules
         /nix/store/jsbd94c63h63yq5xvrp27m5l1sg6667c-discourse-assets-2026.1.4-generated
         /nix/store/n278iw1v8kzswvwm8f6n6g15gkw6wb0q-discourse-assets-2026.1.4-frontend
         /nix/store/qnxglj6z2n8fn46af9rp7161634brcgk-discourse-assets-2026.1.4
       Last 25 log lines:
       > Progress: resolved 1909, reused 1263, downloaded 0, added 0
       > Progress: resolved 1909, reused 1907, downloaded 0, added 65
       > Progress: resolved 1909, reused 1909, downloaded 0, added 435
       > Progress: resolved 1909, reused 1909, downloaded 0, added 530
       > Progress: resolved 1909, reused 1909, downloaded 0, added 672
       > Progress: resolved 1909, reused 1909, downloaded 0, added 838
       > Progress: resolved 1909, reused 1909, downloaded 0, added 1036
       > Progress: resolved 1909, reused 1909, downloaded 0, added 1175
       > Progress: resolved 1909, reused 1909, downloaded 0, added 1464
       > Progress: resolved 1909, reused 1909, downloaded 0, added 1767
       > Progress: resolved 1909, reused 1909, downloaded 0, added 1907
       > Progress: resolved 1909, reused 1909, downloaded 0, added 1908
       > Progress: resolved 1909, reused 1909, downloaded 0, added 1909, done
       >  ERR_PNPM_INVALID_DEPENDENCY_NAME  Refusing to place a dependency under /build/source/frontend/discourse-types/node_modules with the invalid alias "@types/@glimmer__component"
       >
       > ERROR: pnpm failed to install dependencies
       >
       > If you see ERR_PNPM_NO_OFFLINE_TARBALL above this, follow these to fix the issue:
       > 1. Set pnpmDeps.hash to "" (empty string)
       > 2. Build the derivation and wait for it to fail with a hash mismatch
       > 3. Copy the 'got: sha256-' value back into the pnpmDeps.hash field
       >
       > If you see ERR_PNPM_LOCKFILE_CONFIG_MISMATCH above this, try changing the pnpm version
       > Found 'pnpm' with version '10.34.4'
       >
       For full logs, run:
         nix log /nix/store/k90f0nxqhlyb1bym9lg5xl6pfjz4vamf-discourse-assets-2026.1.4.drv
error: Cannot build '/nix/store/9n8grwvci4gd2c64i9wm5ggw5v4r63xf-discourse-2026.1.4.drv'.
       Reason: 1 dependency failed.
       Output paths:
         /nix/store/l9xhbd5scihfln5jr9vh1c6l4xznaall-discourse-2026.1.4
error: Build failed due to failed dependency

我好像没在别处看到过这个问题,觉得你们应该知道一下。

问题在于,仅支持 Debian/Ubuntu,不支持 Nix。这是用于开发环境还是生产环境?

这仅仅是使用 Nix 包管理器从 nixpkgs 下载 Discourse 包。派生(Derivations)的工作原理本质上是从源代码直接克隆并构建包。

我只是像安装其他任何 NixOS 包一样临时安装了一个包。这并不是专门为了开发或生产环境,实际上我只是想看看是否能在命令行界面(CLI)中使用 Discourse。我的 TUI 浏览器无法显示部分用户界面,因此我想试用一下这个 Nix 包,看看用户体验是否更友好。

尝试通过 Nix 进行简单的下载/构建时,出现了上述错误。

有意思。我认为这个包并非由 Discourse 官方发布。快速浏览一下维护者列表,并没有列出我所知道的任何 Discourse 团队成员。此外,据我所知,也没有关于此的 #documentation。

供参考:我想是 这个插件

啊,是的,那是 nixpkg 的维护者

实际的源代码位于 let in 块中

第 55 行:

let
  version = "2026.1.4";

  src = fetchFromGitHub {
    owner = "discourse";
    repo = "discourse";
    rev = "v${version}";
    sha256 = "sha256-kQYDKZIMsWByuCZQfUlwhoIew5QykVylRMh6xvrHIBY=";
  };
in
# ...

所以它从 discourse/discourse 获取源码,其余的派生过程基本上只是构建步骤,包括文件放置位置、如何处理某些构建错误等

这个 src 参数有点像变量,因为它会向下传递到派生过程的其余部分

所以,你不需要完整安装 NixOS 来测试这个。

Nix 包管理器几乎适用于任何机器。

`nix-shell -p discourse`

其中,`nix-shell` 会启动一个 shell,使得通过 `-p` 指定的包在该 shell 环境中可用。一旦关闭该 shell,这些包就不再安装了。

我找不到一个 apt 的类比来解释 nix 作为一个概念,但如果水会适应容器的形状,那么 nix 就是一个会适应其内容形状的容器。`nix` 试图解决的问题是“在我的机器上能运行”,也就是说,如果软件能够编译和运行,那么通过 nix 它也能编译和运行。

测试一下这个帖子是否已被锁定,因为我看不到我最近的一次编辑。

也许编辑需要审核通过后才会显示在帖子中

该帖子并未锁定,我们正在享受这个话题并从中学习。感谢您的贡献,欢迎来到 Meta :+1:

别担心!谢谢你的澄清。

我之前怀疑有审批流程,是因为我做了这个编辑,但在我的这边并没有看到它显示出来:

我这边左侧仍然只显示了文本

编辑:它刚刚出现了

感谢反馈!我查看了一下,这似乎是 Nix 包中的 pnpm 版本不匹配问题,而不是依赖项哈希(dependency hash)的问题。

Discourse v2026.1.4 声明使用 pnpm@10.28.0,而 Nix 表达式 使用的是动态变化的 pnpm_10 包,该包目前解析为 10.34.4。这个较新的 pnpm 版本拒绝了该 Discourse 版本所使用的某些依赖项别名(dependency aliases)。

Nixpkgs 中可能的修复方案是将该派生包(derivation)固定到一个较旧的兼容 pnpm 版本,例如现有的 pnpm_10_29_2 包,或者精确指定为 10.28.0,并在必要时更新 pnpmDeps.hash

由于 Nix 包是由社区维护的,因此需要在 Nixpkgs 中进行此修复,但希望这能为维护者指明正确的方向。

我可能操之过急,直接把这个标记为已解决了,因为我其实还没真正测试过,哈哈。

Nixpkgs 允许在本地对其包进行覆盖(overlays)和重写(overrides)。等我有空的时候,我可以试着自己将这些更改应用到派生(derivation)中,然后再反馈我的发现。

我目前进展到这里:

let
  nixpkgs = fetchTarball "https://github.com/NixOS/nixpkgs/tarball/nixos-26.05";
  pkgs = import nixpkgs {
    config = { };
    overlays = with pkgs; [
      (final: prev: {
        discourse = prev.discourse.override (oa: {
          pnpm_10 = oa.pnpm_10.overrideAttrs (_: {
            src = fetchFromGitHub {
              owner = "pnpm";
              repo = "pnpm";
              rev = "ea870c786fc74c99bf9de644c78453b4d25a7d59";
              hash = "sha256-g+TjAW1SGuEXh04RnGZOQTLkJTHs/3zyCp/P+MzBn4c=";
            };
          });
        });

      })
    ];
  };
in

pkgs.mkShellNoCC {
  packages = with pkgs; [
    # for debug
    discourse
  ];
}

错误信息:

Running phase: unpackPhase
unpacking source archive /nix/store/ci631zzm890b2dzsrxxxrg15qpbac6jy-source
source root is source
rm: cannot remove 'package/dist/vendor': No such file or directory
error: Cannot build '/nix/store/lq23pl8ycyf8myl9mynwzx9a2f2miywv-pnpm-10.34.0.drv'.
       Reason: builder failed with exit code 1.
       Output paths:
         /nix/store/kf62vww5jdfpr32qgxhz4qv9dkfdb0pf-pnpm-10.34.0
       Last 5 log lines:
       > structuredAttrs is enabled
       > Running phase: unpackPhase
       > unpacking source archive /nix/store/ci631zzm890b2dzsrxxxrg15qpbac6jy-source
       > source root is source
       > rm: cannot remove 'package/dist/vendor': No such file or directory
       For full logs, run:
         nix log /nix/store/lq23pl8ycyf8myl9mynwzx9a2f2miywv-pnpm-10.34.0.drv
error: Cannot build '/nix/store/1hi18g4r0gmmlnj059irx91grry7ymv7-discourse-assets-2026.1.4.drv'.
       Reason: 1 dependency failed.
       Output paths:
         /nix/store/bw2pzh74fbp0wkn9f4b2bf2p6lwcva3d-discourse-assets-2026.1.4
         /nix/store/kqnrdh18f4pn87rpzk2af0d18xcvbj7k-discourse-assets-2026.1.4-generated
         /nix/store/mcbvqw0y8r3as9zrjmrq7pc32n64bj2r-discourse-assets-2026.1.4-frontend
         /nix/store/my9x48zhzzqi2w8rriww6qm4v9iaxxhx-discourse-assets-2026.1.4-node_modules
error: Cannot build '/nix/store/05ffysx25hfzw34km225d2krpnifwj75-discourse-2026.1.4.drv'.
       Reason: 1 dependency failed.
       Output paths:
         /nix/store/2b1pvnvln95dx7rxr88kc5b9g24x45dr-discourse-2026.1.4

显然,日志只会显示错误的构建过程,就好像我根本没有覆盖它一样,这真是帮了大忙。它抱怨某个 package/dist/vendor 文件没有被创建。

是的,这大概最好留给维护者去处理。

nixpkgs 中的 Discourse 版本比较旧,还停留在 1 月份,所以我提交了一个 PR 来更新它。我从来没有给 nixpkgs 贡献过代码,所以还不知道会怎么样。

@fuguesoft 看起来他们已经合并了更新后的 Discourse nixpkg,如果你想试试的话