[Nix-dev] Attribute selection with default

Marc Weber marco-oweber at gmx.de
Thu Sep 24 13:35:49 CEST 2009


Excerpts from Eelco Dolstra's message of Thu Sep 24 12:51:34 +0200 2009:
> Hi all,
> 
> Currently there are many places in the Nixpkgs and NixOS expressions where we
> have to select a possibly missing attribute from a set, e.g.
> 
>   attrByPath ["extraModules"] [] mainCfg
> 
> or
> 
>   getConfig ["python" "db4Support"] true
> 
> Writing `attrByPath ["a" "b" "c"] default as' is rather verbose and ugly
> compared to selecting an attribute without a default, i.e. `as.a.b.c'.
> 
> Since this is fairly common, how about some special syntax for supplying a
> default value to the "." selection operator?  I propose the optional "or"
> keyword following an attribute selection "x.<attrpath>".  E.g.
> 
>   mainCfg.extraModules or []
> 
> or
> 
>   config.python.db4Support or true
> 
> Comments?
> 

How will this be implemented in detail?

How generic should it be? Does it allow kind of getAttr this way?

abc."${attrName}".foo or value ?


I'd like it to behave this way:

let
  { a.b = "foo"; }

in a.c # << this must be an error

in a.c or "value" # << this must yield "value"

Do we want to distinguish

attrByPath ["abc" "def" "ghi"] true attrs

from

attrByPath ["def" "ghi"] true attrs.abc

?

Eg this could be implemented this way:

attr ',' attr_name : take attr.attr_name if it exists else yield null

null or x = x
y    or x = y (unless y is null)

Then you could write

attr.abc,def or value  (this means attr.abc must exist)

and

attr,abc,def or value  (abc may be missing)


Maybe ',' is a bad operator name. But I hope you get the difference?
On the other hand you can still check for attr.abc by using assertions.

Marc Weber



More information about the nix-dev mailing list