Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cyclic dependencies in Nix/NixOS explained on a simple example

Here it is written in point 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.

This seems a little bit difficult to understand.

For example if derivation A depends on derivation B and derivation B depends on derivation A, then how is such a mutually recursive pair of derivations built in Nix/NixOS ?

Could you please give a simple example how and why such mutually recursive derivations do not lead to problems ?

like image 872
jhegedus Avatar asked Nov 14 '25 21:11

jhegedus


2 Answers

If A depends on B and viceversa, it's a cyclic dependency and Nix cannot handle that.

But mutually recursive sets are a different thing. It just means A can depend on B of the same set:

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

As stated by jhegedus, it's equivalent to (because of laziness):

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

But this is a cycle, and doesn't work:

rec {
  a = b;
  b = a;
}
like image 102
lethalman Avatar answered Nov 17 '25 11:11

lethalman


I'll post this anyway because it is more than nothing and it might help someone:

Here in point 1: http://nixos.org/nix/manual/#ex-hello-composition, it is written : "we define a mutually recursive set of attributes", this is a little bit confusing. Does this not lead to chicken-egg problem ?

joco42_ Say, package 1 depends on package 2 but package 2 depends on package 1, isn't that a problem ?

joco42_ Can such cyclic dependencies really exist in nix ?

kmicu No it’s not a proble

kmicu m with Nix http://augustss.blogspot.hu/2011/05/more-points-for-lazy-evaluation-in.html

joco42_ kmicu: many thanks

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

joco42_ kmicu: many thanks, I've just asked this on sof before i saw your comment Cyclic dependencies in Nix/NixOS explained on a simple example

joco42_ kmicu: so basically nix expression are written in a lazy language ?

kmicu Yes, “The Nix expression language is a pure, lazy, functional language.”

(there is also an example at http://lethalman.blogspot.com/2014/11/nix-pill-17-nixpkgs-overriding-packages.html )

Basically, nix language can handle recursion because it is lazy:

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; }
like image 40
jhegedus Avatar answered Nov 17 '25 10:11

jhegedus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!