我有什么
在 nixos 模块中使用 mmm 和硬编码路径效果很好,这里有一个例子:
nixcloud-reverse-proxy.nix
{ config, pkgs, lib, ... } @ args:
{
config = { ... };
options = {
services.nixcloud-reverse-proxy = {
configDir = mkOption {
type = types.path;
default = ./. + "/reverse-proxy-config-tests/";
description = ''An absolute path to reverse proxy configurations. This is used for nixcloud.io deployment mainly, where we rebuild the reverse proxy configuration based on many individual configurations.'';
};
};
imports =
let
# walk through all configs in the mmm and merge them
mmm = ./. + "/reverse-proxy-config-tests/";
filesToLoad = attrNames (filterAttrs (k: v: v == "regular") (builtins.readDir mmm));
configsFromPath = map (el: (mmm + ("/" + el) )) filesToLoad;
in configsFromPath;
}
问题
我很乐意将 mmm
替换为 config.services.nixcloud-reverse-proxy.configDir
但这会导致:
nixos-rebuild build
building Nix...
error: infinite recursion encountered, at /nix/var/nix/profiles/per-user/root/channels/nixos/nixpkgs/lib/modules.nix:60:71
(use ‘--show-trace’ to show detailed location information)
building the system configuration...
error: infinite recursion encountered, at /nix/var/nix/profiles/per-user/root/channels/nixos/nixpkgs/lib/modules.nix:60:71
(use ‘--show-trace’ to show detailed location information)
最佳答案
总之,没办法。
事实上,您无法在 config
中定义 imports
子句:
{ config, ... }: {
options = { ... };
config = {
imports = [ ... ]; # <--- error !!!
...
};
}
是关于 NixOS 模块系统如何工作的注释。它的假设之一是 - 您不能根据其他配置选项动态分支或以其他方式控制包含的模块的数量。这就是为什么许多模块定义了 module.enable
选项,它允许这样做。
所以,回到你的问题,imports
子句不能依赖于config.services.nixcloud-reverse-proxy.configDir
,因为config
变量要求所有导入
已合并。当前的模块系统很难解决这个问题。也许可以通过使用另一个变量(不是 config
)来克服这个问题,但这就是您想要的吗?
关于nix - 使用 `imports` 和 lib.mkOption 给出的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45144662/