eval.nix: Support specifying a list of configs

This commit is contained in:
Zhaofeng Li 2022-01-01 16:41:35 -08:00
parent bc3450b3ce
commit cdbb69617f
2 changed files with 32 additions and 4 deletions

View file

@ -302,6 +302,13 @@ let
}; };
in mergedHive // meta; in mergedHive // meta;
configsFor = node: let
nodeConfig = hive.${node};
in
assert lib.assertMsg (!elem node reservedNames) "\"${node}\" is a reserved name and cannot be used as the name of a node";
if typeOf nodeConfig == "list" then nodeConfig
else [ nodeConfig ];
mkNixpkgs = configName: pkgConf: let mkNixpkgs = configName: pkgConf: let
uninitializedError = typ: '' uninitializedError = typ: ''
Passing ${typ} as ${configName} is no longer accepted with Flakes. Passing ${typ} as ${configName} is no longer accepted with Flakes.
@ -348,7 +355,7 @@ let
lib = pkgs.lib; lib = pkgs.lib;
reservedNames = [ "defaults" "network" "meta" ]; reservedNames = [ "defaults" "network" "meta" ];
evalNode = name: config: let evalNode = name: configs: let
npkgs = npkgs =
if hasAttr name hive.meta.nodeNixpkgs if hasAttr name hive.meta.nodeNixpkgs
then mkNixpkgs "meta.nodeNixpkgs.${name}" hive.meta.nodeNixpkgs.${name} then mkNixpkgs "meta.nodeNixpkgs.${name}" hive.meta.nodeNixpkgs.${name}
@ -436,8 +443,7 @@ let
keyChownModule keyChownModule
deploymentOptions deploymentOptions
hive.defaults hive.defaults
config ] ++ configs ++ (import (npkgs.path + "/nixos/modules/module-list.nix"));
] ++ (import (npkgs.path + "/nixos/modules/module-list.nix"));
specialArgs = hive.meta.specialArgs // { specialArgs = hive.meta.specialArgs // {
inherit name nodes; inherit name nodes;
modulesPath = npkgs.path + "/nixos/modules"; modulesPath = npkgs.path + "/nixos/modules";
@ -452,7 +458,7 @@ let
nodes = listToAttrs (map (name: { nodes = listToAttrs (map (name: {
inherit name; inherit name;
value = evalNode name hive.${name}; value = evalNode name (configsFor name);
}) nodeNames); }) nodeNames);
toplevel = lib.mapAttrs (name: eval: eval.config.system.build.toplevel) nodes; toplevel = lib.mapAttrs (name: eval: eval.config.system.build.toplevel) nodes;

View file

@ -200,6 +200,28 @@ fn test_parse_unknown_option() {
"#); "#);
} }
#[test]
fn test_config_list() {
TempHive::valid(r#"
with builtins;
{
host-a = [
{
time.timeZone = "America/Los_Angeles";
}
{
deployment.tags = [ "some-tag" ];
}
];
host-b = { name, nodes, ... }:
assert length (attrNames nodes) == 2;
assert nodes.host-a.config.time.timeZone == "America/Los_Angeles";
assert elem "some-tag" nodes.host-a.config.deployment.tags;
{};
}
"#);
}
#[test] #[test]
fn test_parse_key_text() { fn test_parse_key_text() {
TempHive::valid(r#" TempHive::valid(r#"