【问题标题】:nix nixlang: undefined variable pkgs in default.nix via nix-build -A hello but works in nix replnix nixlang:通过 nix-build -A hello 在 default.nix 中未定义的变量 pkgs,但在 nix repl 中有效
【发布时间】:2020-02-08 21:37:57
【问题描述】:

我写了一个非常简单的 default.nix 文件,我应该可以用它来构建 gnu hello 包(类似于 nix-pills)。

但现在我遇到了一个错误:

[jane@nixos:~/graphviz]$ nix-build -A hello

错误:/home/jane/graphviz/default.nix:3:47 中未定义的变量“pkgs”

这是源代码:

[jane@nixos:~/graphviz]$ cat default.nix 
{
    pkgs = import <nixpkgs> {}; 
    mkDerivation = import ./autotools.nix pkgs;
    hello = import ./hello.nix { inherit mkDerivation ;};
    
}

这对我来说绝对没有意义,因为我在上面定义了 pkgs。

因为我看不出有什么问题,所以我打开了 nix repl 并输入了行。

nix-repl> pkgs = import <nixpkgs> {} 

nix-repl> mkDerivation = import ./autotools.nix pkgs

nix-repl> hello = import ./hello.nix { inherit mkDerivation ;}  

nix-repl> hello
«derivation /nix/store/g2y6sf5q236icvv2gwyg0lnij3mkr36j-hellooo.drv»

瞧,它在那里工作。所以我不明白为什么 default.nix 会失败。我只能想象 default.nix 有点特别,但在语法方面它必须没问题,否则 nix repl 就不能正常工作了。

谁能解释为什么我得到这个未定义的变量错误?

编辑:就在提出问题后,我找到了一种解决未定义变量错误的方法,如果我这样说的话:

let pkgs = import <nixpkgs> {}; mkDerivation = import ./autotools.nix pkgs;
in
{
    hello = import ./hello.nix { inherit mkDerivation ;};
    
}

它有效。

但我最初的问题仍然存在。

【问题讨论】:

    标签: nix


    【解决方案1】:

    { } 语法只定义一个值。它不会将其中的属性纳入范围。你可以使用rec { } 语法,这两者兼而有之。

    rec {
      pkgs = import <nixpkgs> {}; 
      mkDerivation = import ./autotools.nix pkgs;
      hello = import ./hello.nix { inherit mkDerivation ;};
    }
    

    nix repl 所做的基本上是在每次命名时创建一个let 绑定。您的 repl 会话可以被认为是交互式构建的这个表达式:

    let pkgs = import <nixpkgs> {};
    in /* nixlang scope on 2nd prompt */
      let mkDerivation = import ./autotools.nix pkgs
      in /* nixlang scope on 3rd prompt */
        let hello = import ./hello.nix { inherit mkDerivation ;}
          /* nixlang scope on 4th prompt */
        in hello
    

    为了说明属性集创建和名称绑定之间的区别,您可以为属性集命名并在其自己的定义中使用它

    let
      myScope = {
        # myScope is like any other attribute set
        pkgs = import <nixpkgs> {};
    
        # laziness permits the attributes inside to reference myScope
        mkDerivation = import ./autotools.nix myScope.pkgs;
        hello = import ./hello.nix { inherit (myScope) mkDerivation ;};
      };
    in
      # the value of this file is the myScope attribute set
      myScope
    

    这等效于 rec 示例,但只将一个名称带入范围:myScope

    【讨论】:

    • 谢谢你的回答,所以如果我理解正确,默认变量不会被纳入范围以避免评估它们? (因此节省计算)
    • 这可能有点帮助。另一个原因的例子是这个{ key, value }: rec { key = key; value = value + 1; } 产生了一个属性集,其中包含产生无限递归的字段。因此,如果 rec 是唯一的行为,则需要进行更多重命名,并且必须小心避免意外递归。
    猜你喜欢
    • 1970-01-01
    • 2018-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多