nix nixlang:通过 nix-build -A hello 在 default.nix 中未定义的变量 pkgs 但在 nix repl 中有效

nix nixlang: undefined variable pkgs in default.nix via nix-build -A hello but works in nix repl

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

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

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

error: undefined variable 'pkgs' at /home/jane/graphviz/default.nix:3:47

这是源代码:

[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 ;};
    
}

有效。

但我原来的问题仍然存在。

{ } 语法只定义一个值。它不会将其中的属性带入作用域。您可以使用 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.