nix - 使用 `imports` 和 lib.mkOption 给出的参数

标签 nix nixos

我有什么

在 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/

相关文章:

linux - NixOps:如何部署到现有的 NixOS VM?

rust - 我如何在 nixpkgs 派生中使用特定的 Rust 构建?

vim - 使用 Nix 中的预定义插件列表构建 vim

xmonad - NixOS 初学者 : xmonad and haskellmode in NixOS 14. 04

home-directory - 关于在主目录中安装的 Nix 教程

nix - 如何在 Nix 推导中使用 chown?

haskell - NixOS、Haskell、opengl : problems with building and running openGL programs

haskell - 在NixOS中构建Haskell Stack示例时出现“ghc-cabal:无此文件或目录”错误

purescript - 在NixOS上开始使用purescript时需要帮助

NixOS:使用不同的 channel 安装 unfree 包