如何在Nix REPL中对flake-utils eachSystem结果执行eagerly print?
在Nix REPL中完整打印flake-utils eachSystem的输出结果
问题背景
flake-utils的文档里给出了eachSystem函数的示例,显示的是完整的嵌套输出结果:
eachSystem [ system.x86_64-linux ] (system: { hello = 42; }) # => { hello = { x86_64-linux = 42; }; } eachSystem allSystems (system: { hello = 42; }) # => { hello.aarch64-darwin = 42, hello.aarch64-genode = 42, hello.aarch64-linux = 42, ... hello.x86_64-redox = 42, hello.x86_64-solaris = 42, hello.x86_64-windows = 42 }
但实际在Nix REPL里执行以下操作时:
> nix repl nix-repl> e = builtins.getFlake("github:numtide/flake-utils") nix-repl> with e.outputs.lib; eachSystem [ system.x86_64-linux ] (system: { hello = 42; })
(也可以用:a e.outputs.lib把库的属性导入作用域,不用写with语句)
输出只会显示{ hello = { ... }; },看不到完整的嵌套内容。怎么才能实现完整打印(eagerly print)呢?
解决方法
有几种方法可以在Nix REPL里强制打印完整的嵌套值:
1. 用:p命令强制完整打印
这是最直接的方式,在REPL里使用:p前缀执行命令,会输出值的完整结构:
nix-repl> with e.outputs.lib; :p eachSystem [ system.x86_64-linux ] (system: { hello = 42; }) # 输出完整结果: { hello = { x86_64-linux = 42; }; }
2. 直接访问嵌套属性
如果清楚属性的层级路径,可以直接打印目标属性:
nix-repl> with e.outputs.lib; (eachSystem [ system.x86_64-linux ] (system: { hello = 42; })).hello # 输出结果: { x86_64-linux = 42; }
3. 调整REPL的输出深度限制
Nix REPL默认会截断嵌套较深的结构显示,可以通过设置增大输出深度:
nix-repl> :set max-depth 100
调整后再执行原命令,就能看到完整的嵌套内容。
4. 使用builtins.trace打印
builtins.trace会先打印传入的值,再返回该值,适合临时查看完整结构:
nix-repl> with e.outputs.lib; builtins.trace (eachSystem [ system.x86_64-linux ] (system: { hello = 42; })) {} # 先输出完整结果,再返回空对象{}
内容的提问来源于stack exchange,提问作者nicolas
相关产品推荐
相关产品推荐

