Nix/NixOS 中的循环依赖用一个简单的例子解释

Cyclic dependencies in Nix/NixOS explained on a simple example

Here在第1点写的:

This file defines a set of attributes, all of which are concrete derivations (i.e., not functions). In fact, we define a mutually recursive set of attributes. That is, the attributes can refer to each other. This is precisely what we want since we want to “plug” the various packages into each other.

这似乎有点难以理解。

比如derivation A依赖于derivation Bderivation B依赖于derivation A,那么[=27=中如何构建这样一对相互递归的推导呢? ] ?

能否请您举一个简单的例子这样的相互递归推导如何以及为什么不会导致问题?

我会 post 无论如何,因为它比什么都重要,它可能对某人有帮助:

这里第1点:http://nixos.org/nix/manual/#ex-hello-composition,写成:"we define a mutually recursive set of attributes",这有点让人迷惑。这不会导致先有鸡还是先有蛋的问题吗?

joco42_ 比如说,包 1 依赖于包 2 但包 2 依赖于包 1,这不是问题吗?

joco42_ nix中真的可以存在这样的循环依赖吗?

kmicu 不,这不是问题

kmicu 米与尼克斯 http://augustss.blogspot.hu/2011/05/more-points-for-lazy-evaluation-in.html

joco42_ kmicu: 非常感谢

kmicu http://nixos.org/nix/manual/#sec-constructs

joco42_ kmicu:非常感谢,在看到您的评论之前,我刚刚在 sof 上问过这个

joco42_ kmicu:所以基本上 nix 表达式是用惰性语言编写的?

kmicu 是的,“Nix 表达式语言是一种纯粹的、惰性的、函数式语言。”

http://lethalman.blogspot.com/2014/11/nix-pill-17-nixpkgs-overriding-packages.html 处也有例子)

基本上,nix语言可以处理递归,因为它是惰性的:

nix-repl> fix=f : let r= f r ; in r

nix-repl> p= s: { a=3;b=4; c=s.a+s.b;}

nix-repl> fix p
{ a = 3; b = 4; c = 7; }

如果 A 依赖于 B,反之亦然,这是一个循环依赖,Nix 无法处理。

但是相互递归集合是另一回事。这只是意味着A可以依赖同一个集合的B:

rec {
  a = 1;
  b = 2;
  c = a+b;
}

正如 jhegedus 所说,它等同于(因为懒惰):

let s = with s; {
  a = 1;
  b = 2;
  c = a+b;
};
in s

但这是一个循环,不起作用:

rec {
  a = b;
  b = a;
}