如何查看远程Flake仓库树形结构及解决Nix相关报错
解答
一、报错原因解析
import一个目录时,Nix默认会查找目录下的default.nix作为入口文件。你用fetchGit拉取的cosmos.nix仓库采用flake结构,没有default.nix文件,因此直接导入仓库目录会触发找不到文件的错误——这里的路径是Nix Store中缓存的远程仓库路径,并非本地文件路径,只是Nix的报错信息显示了Store的存储位置。
二、解决远程仓库导入问题
针对flake结构的仓库,需要明确指定导入flake.nix文件,而非整个目录:
# 在nix repl中执行 let repo = fetchGit { url = "https://github.com/informalsystems/cosmos.nix"; }; let cosmos = import "${repo}/flake.nix"; cosmos.outputs
这样就能正确获取到flake的outputs,也就是你看到的lambda函数(因为outputs本身是一个接受system参数的函数)。
三、让builtins.toJSON支持flake输出
builtins.toJSON仅能序列化纯数据类型(字符串、数字、列表、属性集等),而flake的outputs是函数类型,无法直接序列化为JSON。解决方法是先调用这个函数,传入具体的系统参数(如x86_64-linux),得到具体的属性集后再转换为JSON:
在nix repl中执行:
let repo = fetchGit { url = "https://github.com/informalsystems/cosmos.nix"; }; let cosmos = import "${repo}/flake.nix"; # 调用outputs函数,传入指定系统参数 builtins.toJSON (cosmos.outputs "x86_64-linux")
在bash中用nix-instantiate执行:
nix-instantiate --eval --expr ' let repo = fetchGit { url = "https://github.com/informalsystems/cosmos.nix"; }; let cosmos = import "${repo}/flake.nix"; builtins.toJSON (cosmos.outputs "x86_64-linux") ' --json
添加--json参数可以让输出格式更规整。
如果不想手动指定系统参数,也可以用builtins.currentSystem自动获取当前运行的系统:
builtins.toJSON (cosmos.outputs builtins.currentSystem)
内容的提问来源于stack exchange,提问作者Pierre-olivier Gendraud
相关产品推荐
相关产品推荐

