[Nix-dev] Nix expression writing conventions

Sander van der Burg - EWI S.vanderBurg at tudelft.nl
Thu Jul 29 16:52:29 CEST 2010


I noticed that we have several conventions to deal with package names and version numbers inside Nix expressions for various reasons. For instance, some expressions explicitly write the name and version number everywhere, without referring to attributes, e.g.:

{stdenv, fetchurl, gettext}:

stdenv.mkDerivation {
  name = "hello-1.0";
  src = fetchurl {
    url = mirror://gnu/hello/hello-1.0.tar.gz;
    sha256 = "123456789abcdefgh";
  };
  buildInputs = [ gettext ];
}

In the expression above we mention hello-1.0 twice, which requires a developer to modify the version number twice in case of a upgrade and the developer could also identically forget to update one of those version numbers.

I also encountered expressions, in which the URL refers to the name attribute. The advantage of this is that we don't have to update the name + version number twice, but the disadvantage is that the URL is a string which is not checked statically:

{stdenv, fetchurl, gettext}:

stdenv.mkDerivation rec {
  name = "hello-1.0";
  src = fetchurl {
    url = "mirror://gnu/hello/${name}.tar.gz";
    sha256 = "123456789abcdefgh";
  };
  buildInputs = [ gettext ];
}

Moreover, I encountered several situations in which the version is specified in an attribute, such as:

{stdenv, fetchurl, gettext}:

let version = "1.0"; in
stdenv.mkDerivation rec {
  name = "hello-"+version;
  src = fetchurl {
    url = "mirror://gnu/hello/${name}.tar.gz";
    sha256 = "123456789abcdefgh";
  };
  buildInputs = [ gettext ];
}

or:

{stdenv, fetchurl, gettext}:

stdenv.mkDerivation rec {
  name = "hello-"+version;
  version = "1.0";
  src = fetchurl {
    url = "mirror://gnu/hello/${name}.tar.gz";
    sha256 = "123456789abcdefgh";
  };
  buildInputs = [ gettext ];
}

So what I noticed from all these expressions is that it is probably better/nicer to specify the name and the version number separately and use those attributes to form a name for in the Nix store and a download URL.
Wouldn't it be better to specify expression by default, like this:

{stdenv, fetchurl, gettext}:

stdenv.mkDerivation rec {
  name = "hello";
  version = "1.0";
  src = fetchurl {
    url = "mirror://gnu/hello/${name}-${version}.tar.gz";
    sha256 = "123456789abcdefgh";
  };
  buildInputs = [ gettext ];
}

We always specify the name and version number separately (which produces /nix/store/hash-${name}-${version} and we refer to those attributes in the URL.

What do you guys think? (I hope this is not considered a bike shedding issue :-) )
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.science.uu.nl/pipermail/nix-dev/attachments/20100729/7b3080fd/attachment.html 


More information about the nix-dev mailing list