Nix Koans
摘要
本文通过五个短篇寓言形象地概括了 Nix 的技术特性:包括强制修改不可变存储(/nix/store)的后果、纯净构建与非纯净模式的区别、闭包(closure)的概念、Nix 新旧命令行工具并存的矛盾,以及在处理交叉编译难题时对远程构建器(remote builders)的依赖。
荐读理由
借由禅宗公案形式,揭示了 Nix 在处理不可变存储、纯净构建边界以及跨平台编译(如配置远程构建器 ssh-ng)时的核心逻辑与常见陷阱,为你评估是否采用 Nix 作为 AI 工程的可复现基础设施提供了直观的技术权衡依据。
原文
Nix Koans
Inspired by the legendary Qc Na and Git Koans, which was in turn inspired by Vim Koans.
Immutable
A site reliability engineer rushed into the Nix Store.
"The whole front aisle is rotten!" he cried to the clerk.
They seized the tainted goods, but the goods were fastened to the shelves, and the shelves to the floor.
A passerby said: "Build a new shelf, and move the door."
"There is no time", cried the clerk. Then she shouted: "mount -o remount,rw /nix/store!"
At last, something budged. The building collapsed, its foundations swapped for enlightenment.
Walls of Purity
A novice was running a free advertising campaign for Master Nix.
"What Master Nix builds today, he will build in ten years!" he shouted to the marketplace. "A build within his garden knows neither the day, nor the hour!"
Master Nix, who was passing, handed the novice a derivation. "Print my pamphlets," he said.
The novice took the derivation to the garden, and printed the pamphlets. Each pamphlet bore its day and hour of printing.
"A breach in the walls!" cried the novice.
Master Nix said nothing. He struck the day and hour from the template, and printed again with --impure. The pamphlets agreed. He tore down the garden walls with __noChroot = true, and printed once more, in the open air. The pamphlets agreed.
"Master, with the walls torn, what keeps the world out of the build?"
"What kept the hour out, when they stood?"
The marketplace went quiet.
At that moment, the novice was enlightened.
Derivation
A functional programmer was trying to convince Master Nix that his project was worthy of a derivation.
Master Nix eventually answered the call, evaluated the programmer, and fulfilled his wish.
The programmer copied the bestowed gift onto a portable storage device, and as he turned to depart, Master Nix asked:
"And its closure?"
The programmer rummaged through his code, and only found self-contained functions. "There are no closures in my program" he said.
Master Nix said nothing.
At the demonstration, the programmer was englightened.
The Old Tongue
A new student of Master Nix was learning the basics.
Proud of his first package, he showed the master his work: "nix-build hello.nix"
Master Nix struck him on the head with a metal bat. "We do not speak the old tongue."
The student learned the new tongue, in which a silence stands where the hyphen stood. He spoke it beautifully: nix build, nix shell, nix run. In time he grew zealous, and corrected the older students when they slipped.
One day, wishing he knew how the temple was kept, he opened the scripts of the masters. nix-instantiate, nix-env, nix-prefetch-url, nix-store. The old tongue, from the first scroll to the last.
"Master!" he cried. "Your own scriptures-"
Master Nix threw him into the well.
At the bottom, stood enlightenment.
Cross
A poor devops engineer who was assigned a teletype of unusual thinness was limping along in despair, for his masters demanded x86_64-linux, and the teletype knew it not.
One day, he heard a colleague speak of pkgsCross, by which a machine may build for a land it will never see. That night, he evaluated pkgsCross.gnu64, and the teletype began to build the world.
binutils crossed. glibc crossed. Then a package, wishing to test what it had built, tried to run it; but what it had built was of the far land, and the teletype knew it not. A thousand errors followed. The engineer read the first, and wept.
An old operator looked over his shoulder.
"A fine terminal," he said. "But one needs a computer to build."
The engineer wrote in his configuration: builders = ssh-ng://elsewhere x86_64-linux. The teletype clattered through the night, printing the labors of a distant machine.
At dawn, the engineer was enlightened.
这条对你有帮助吗?