是否有一种简单的方法可以向stdenv提供的环境中添加bash函数?在使用“ nix-shell”进行开发时,我可以运行“ unpackPhase”或“ buildPhase”之类的命令,因为mkDerivation
将它们置于范围内-超级有用。我的派生属性也添加到环境中。但我也想看到一种在构建/ shell环境中自动添加属性作为函数的方法。
为了解释我要得到的内容,当前可以通过使用eval
语句或toFile
和source
将函数引入环境。例如,使用eval
,类似:
{ stdenv, ... } : stdenv.mkDerivation
{ shellHook = ''eval "$myFunctions"'';
myFunctions = ''
myFunction1(){
echo "doing myFunction1"
}
myFunction2(){
echo "doing myFunction2"
}
''
}
当我输入myFunction1
时将“来源” myFunction2
和nix-shell
。
但是,我期望像mkFunction
实用程序这样的东西能补充mkWrapper
,mkProgram
和其他此类基本实用程序。我希望可以像下面那样使用它,其中使用mkFunction
定义的属性将如上所述自动“来源”。
{ stdenv, mkFunction, ... } : stdenv.mkDerivation
{ myFunction1 = mkFunction '' echo "doing myFunction1" '';
myFunction2 = mkFunction '' echo "doing myFunction2" '';
shellHook = '' echo "No need to source, myFunctions are already in scope." '';
}
我认为这样的实用程序会很有用。我以为setup hooks可能涵盖了这种用法,但我不确定如何使用它们。而且以第一方式做起来还不错(如果我在编写问题的过程中没有弄清楚,我就不会打扰问)。但是nix
似乎已经可以使用这种实用程序,因此无论如何我都在询问。
设置挂钩确实可以做到这一点,这是一个定义函数foo
的简单示例:
with import <nixpkgs> {};
let
fooHook = stdenv.mkDerivation {
name = "foo-hook";
# Setting phases directly is usually discouraged, but in this case we really
# only need fixupPhase because that's what installs setup hooks
phases = [ "fixupPhase" ];
setupHook = writeText "my-setup-hook" ''
foo() { echo "Foo was called!"; }
'';
};
in mkShell {
buildInputs = [ fooHook ];
shellHook = "foo";
}
对此运行nix-shell
会产生所需的结果:
$ nix-shell
Foo was called!
[nix-shell:~]$