作者近期关于dynamic-derivations的帖子让其更深入思考直接使用Nix。认为尝试手动编写推导并添加到/nix/store
中构建会很“有趣”。
- 所有
/nix/store
中的推导都以简单但古老的ATerm格式编写,工具支持不佳,所以决定纯用 JSON 工作。 - Nix 的新
nix derivation
命令可接受 JSON 而非 ATerm 格式。 - 推导需要 3 个必需参数:
name
、system
、builder
,如{"name":"simple","system":"x86_64-linux","builder":"/bin/sh"}
。 - 添加输出
outputs
,如{"name":"simple","system":"x86_64-linux","builder":"/bin/sh","outputs":{"out":{"path":"/nix/store/7s0z3d6p9y2v5x8b1c4g1w5r2q9n0f8a-simple"}}}
。 - 留空
inputSrcs
(输入源列表)。 - 添加
inputDrvs
(依赖的推导)和env
(环境变量)、args
(命令参数),如{"name":"simple","system":"x86_64-linux","builder":"/bin/sh","outputs":{"out":{"path":"/nix/store/5bkcqwq3qb6dxshcj44hr1jrf8k7qhxb-simple"},"inputSrcs":[],"inputDrvs":{},"env":{"out":"/nix/store/5bkcqwq3qb6dxshcj44hr1jrf8k7qhxb-simple"},"args":["-c","echo 'hello world' > $out"]}
。 - 经过多次修正,最终 Nix 接受了推导,
nix-store --realize
后得到预期输出hello world
。作者选择/bin/sh
是为了保持推导极其简单,且/bin/sh
在 Nix 沙盒中默认存在以满足 POSIX 合规性。
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。