mirror of
https://github.com/ryan4yin/nixos-and-flakes-book
synced 2024-11-26 22:10:26 +00:00
feat: update 'callPackage'
This commit is contained in:
parent
9bd961a186
commit
8cb6a01539
2 changed files with 104 additions and 3 deletions
|
@ -13,5 +13,58 @@ When the `xxx.nix` used in `pkgs.callPackge xxx.nix {...}` is a function (most N
|
|||
3. Then `pkgs.callPackge` will merge its second parameter `{...}` with the attribute set obtained in the previous step, and then pass it to the function imported from `xxx.nix` and execute it.
|
||||
4. Finally we get a Derivation as the result of the function execution.
|
||||
|
||||
So the common usage of `pkgs.callPackage` is to import customized Nix packages and used them in Nix Module.
|
||||
For example, we wrote a `hello.nix` ourselves, and then we can use `pkgs.callPackage ./hello.nix {}` in any Nix Module to import and use it.
|
||||
The common usage of `pkgs.callPackage` is to import customized Nix packages and used them in Nix Module.
|
||||
|
||||
|
||||
For example, we have customized a NixOS kernel configuration `kernel.nix`, and used the SBC's name and kernel source as its variable parameters:
|
||||
|
||||
```nix
|
||||
{
|
||||
lib,
|
||||
stdenv,
|
||||
linuxManualConfig,
|
||||
|
||||
src,
|
||||
boardName,
|
||||
...
|
||||
}:
|
||||
(linuxManualConfig {
|
||||
version = "5.10.113-thead-1520";
|
||||
modDirVersion = "5.10.113";
|
||||
|
||||
inherit src lib stdenv;
|
||||
|
||||
# file path to the generated kernel config file(the `.config` generated by make menuconfig)
|
||||
#
|
||||
# here is a special usage to generate a file path from a string
|
||||
configfile = ./. + "${boardName}_config";
|
||||
|
||||
allowImportFromDerivation = true;
|
||||
})
|
||||
```
|
||||
|
||||
Then we can use `pkgs.callPackage ./kernel.nix {}` in any Nix Module to import and use it, and replace any of its parameters:
|
||||
|
||||
```nix
|
||||
{ lib, pkgs, pkgsKernel, kernel-src, ... }:
|
||||
|
||||
{
|
||||
# ......
|
||||
|
||||
boot = {
|
||||
# ......
|
||||
kernelPackages = pkgs.linuxPackagesFor (pkgs.callPackage ./pkgs/kernel {
|
||||
src = kernel-src; # kernel source is passed as a `specialArgs` and injected into this module.
|
||||
boardName = "licheepi4a"; # the board name, used to generate the kernel config file path.
|
||||
});
|
||||
|
||||
# ......
|
||||
}
|
||||
```
|
||||
|
||||
Just like the example above, we can use `pkgs.callPackage` to pass different `src` and `boardName` to the function defined in `kernel.nix` through `pkgs.callPackage`, so that different kernel packages can be generated.
|
||||
The `kernel.nix` can be used to adapt to different kernel sources and different development boards by changing the parameters passed to it.
|
||||
|
||||
## References
|
||||
|
||||
- [Chapter 13. Callpackage Design Pattern - Nix Pills](https://nixos.org/guides/nix-pills/callpackage-design-pattern.html)
|
|
@ -15,7 +15,55 @@
|
|||
3. 接着 `pkgs.callPackge` 会将其第二个参数 `{...}` 与前一步得到的参数集(attribute set)进行合并,得到一个新的参数列表,然后将其传递给该函数并执行。
|
||||
4. 函数执行结果是一个 Derivation,也就是一个软件包。
|
||||
|
||||
这个函数比较常见的用途是用来导入一些自定义的 Nix 包,比如说我们自己写了一个 `hello.nix`,然后就可以在任意 Nix Module 中使用 `pkgs.callPackage ./hello.nix {}` 来导入并使用它。
|
||||
这个函数比较常见的用途是用来导入一些自定义的 Nix 包。
|
||||
|
||||
比如说我们自定义了一个 NixOS 内核配置 `kernel.nix`,并且将开发版名称与内核源码作为了可变更参数:
|
||||
|
||||
```nix
|
||||
{
|
||||
lib,
|
||||
stdenv,
|
||||
linuxManualConfig,
|
||||
|
||||
src,
|
||||
boardName,
|
||||
...
|
||||
}:
|
||||
(linuxManualConfig {
|
||||
version = "5.10.113-thead-1520";
|
||||
modDirVersion = "5.10.113";
|
||||
|
||||
inherit src lib stdenv;
|
||||
|
||||
# file path to the generated kernel config file(the `.config` generated by make menuconfig)
|
||||
#
|
||||
# here is a special usage to generate a file path from a string
|
||||
configfile = ./. + "${boardName}_config";
|
||||
|
||||
allowImportFromDerivation = true;
|
||||
})
|
||||
```
|
||||
|
||||
那么就可以在任意 Nix Module 中使用 `pkgs.callPackage ./hello.nix {}` 来导入并使用它,并且替换它的任意参数。
|
||||
|
||||
```nix
|
||||
{ lib, pkgs, pkgsKernel, kernel-src, ... }:
|
||||
|
||||
{
|
||||
# ......
|
||||
|
||||
boot = {
|
||||
# ......
|
||||
kernelPackages = pkgs.linuxPackagesFor (pkgs.callPackage ./pkgs/kernel {
|
||||
src = kernel-src; # kernel source is passed as a `specialArgs` and injected into this module.
|
||||
boardName = "licheepi4a"; # the board name, used to generate the kernel config file path.
|
||||
});
|
||||
|
||||
# ......
|
||||
}
|
||||
```
|
||||
|
||||
就如上面所展示的,通过 `pkgs.callPackage` 我们可以给 `kernel.nix` 定义的函数传入不同的 `src` 与 `boardName`,来生成不同的内核包,这样就可以使用同一份 `kernel.nix` 来适配不同的内核源码与不同的开发板了。
|
||||
|
||||
|
||||
## 参考
|
||||
|
|
Loading…
Reference in a new issue