【问题标题】:Inherit Attributes from Another Set in Nix Expression Language从 Nix 表达式语言中的另一个集合继承属性
【发布时间】:2017-05-02 21:28:04
【问题描述】:

在 Nix 手册的 Inheriting attributes 部分,我们有

graphviz = (import ../tools/graphics/graphviz) {
  inherit fetchurl stdenv libpng libjpeg expat x11 yacc;
  inherit (xlibs) libXaw;
};

xlibs = {
  libX11 = ...;
  libXaw = ...;
  ...
}

libpng = ...;
libjpg = ...;
...

import ...(即(import ../tools/graphics/graphviz))周围的括号有什么作用?还有,inherit (xlibs) libXaw; 中的括号有什么作用?

【问题讨论】:

    标签: nix nixos


    【解决方案1】:

    xlibs 与 graphviz 在同一范围内,但 libXaw 不在,因为它在 xlibs 集合内。因此,为了能够将其作为参数传递给 graphviz 函数,您需要显式地inherit (xlibs) libXaw。方括号表示的行为是 inherit 关键字所独有的。

    (import ../tools/graphics/graphviz) 中的括号只是表示评估顺序的常用约定。 import 是一个接受单个参数 path 的函数。 ../tools/graphics/graphviz 中的文件包含一个函数,该函数接受一组属性作为其参数。所以括号表示评估顺序应该是(1)在path中导入函数,然后(2)将属性集{...}应用于该函数。


    编辑:@danbst 指出在这种情况下不需要import ../tools/graphics/graphviz 中的括号。这样做的原因是评估import ../tools/graphics/graphviz 返回一个函数,然后使用集合{ ... } 调用该函数。

    括号的必要性可以通过使用与此函数typesOfArgs = one: two: with builtins; "${typeOf one} and ${typeOf two}" 的参数相同的值来证明,该函数返回一个字符串。 typesOfArgs (import ../tools/graphics/graphviz) { } 将评估为 "lambda and set",但如果没有括号,解释器会将 typesOfArgs import ../tools/graphics/graphviz 评估为 "lambda and path",然后尝试将该字符串作为带有参数 { } 的函数调用,这将导致 error: attempt to call something which is not a function but a string

    如果没有括号,解释器会假设您要调用函数import,并带有两个参数path{ ... },这将是一个错误。

    【讨论】:

    • 很好的解释。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2012-03-30
    • 2011-02-24
    • 2014-07-03
    • 1970-01-01
    • 1970-01-01
    • 2020-12-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多