Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eval-machines: support specialArgs #190

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 44 additions & 45 deletions data/eval-machines.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,25 @@
{ networkExpr }:

let
network = import networkExpr;
nwPkgs = network.network.pkgs or {};
lib = network.network.lib or nwPkgs.lib or (import <nixpkgs/lib>);
evalConfig = network.network.evalConfig or ((nwPkgs.path or <nixpkgs>) + "/nixos/lib/eval-config.nix");
runCommand = network.network.runCommand or nwPkgs.runCommand or ((import <nixpkgs> {}).runCommand);
network = import networkExpr;
nw = network.network;
nwPkgs = nw.pkgs or {};
nwLib = nw.lib or nwPkgs.lib or (import <nixpkgs/lib>);
nwEvalConfig = let
nwEvalConfig' =
nw.evalConfig or ((nwPkgs.path or <nixpkgs>) + "/nixos/lib/eval-config.nix");
in
if nwLib.isFunction nwEvalConfig' then nwEvalConfig' else import nwEvalConfig';
nwSpecialArgs = nw.specialArgs or {};
nwRunCommand = nw.runCommand or nwPkgs.runCommand or ((import <nixpkgs> {}).runCommand);
in
with lib;

let
modules = { machineName, nodes, check }: [
modules = { machineName, machineModule ? network.${machineName}, nodes, check }: [
# Get the configuration of this machine from each network
# expression, attaching _file attributes so the NixOS module
# system can give sensible error messages.
{ imports = [ network.${machineName} ]; }
{ imports = [ machineModule ]; }

({ config, lib, options, ... }: {
key = "deploy-stuff";
Expand All @@ -41,46 +46,40 @@ let
inherit check;
};
})
] ++ optional (network ? _file) { inherit (network) _file; };
] ++ nwLib.optional (network ? _file) { inherit (network) _file; };

machineNames = attrNames (removeAttrs network [ "network" "defaults" "resources" "require" "_file" ]);
networkMachines = removeAttrs network [ "network" "defaults" "resources" "require" "_file" ];

in rec {
# Unchecked configuration of all machines.
# Using unchecked config evaluation allows each machine to access other machines
# configuration without recursing as full evaluation is prevented
uncheckedNodes =
listToAttrs (map (machineName:
{ name = machineName;
value = import evalConfig {
modules = modules {
inherit machineName;
check = false;
nodes = uncheckedNodes;
};
};
}
) machineNames);
builtins.mapAttrs (machineName: machineModule: nwEvalConfig {
modules = modules {
inherit machineName machineModule;
check = false;
nodes = uncheckedNodes;
};
specialArgs = nwSpecialArgs;
}) networkMachines;

# Compute the definitions of the machines.
nodes =
listToAttrs (map (machineName:
{ name = machineName;
value = import evalConfig {
modules = modules {
inherit machineName;
check = true;
nodes = uncheckedNodes;
};
};
}
) machineNames);
builtins.mapAttrs (machineName: machineModule: nwEvalConfig {
modules = modules {
inherit machineName;
check = true;
nodes = uncheckedNodes;
};
specialArgs = nwSpecialArgs;
}) networkMachines;

deploymentInfoModule = {
deployment = {
name = deploymentName;
arguments = args;
inherit uuid;
name = nwLib.deploymentName;
arguments = nwLib.args;
inherit (nwLib) uuid;
};
};

Expand All @@ -92,17 +91,17 @@ in rec {
in rec {

machines =
flip mapAttrs nodes (n: v': let v = scrubOptionValue v'; in
builtins.mapAttrs (n: v': let v = nwLib.scrubOptionValue v'; in
{ inherit (v.config.deployment) targetHost targetPort targetUser secrets healthChecks buildOnly substituteOnDestination tags;
name = n;
nixosRelease = v.config.system.nixos.release or (removeSuffix v.config.system.nixos.version.suffix v.config.system.nixos.version);
nixConfig = mapAttrs
nixosRelease = v.config.system.nixos.release or (nwLib.removeSuffix v.config.system.nixos.version.suffix v.config.system.nixos.version);
nixConfig = builtins.mapAttrs
(n: v: if builtins.isString v then v else throw "nix option '${n}' must have a string typed value")
(network'.network.nixConfig or {});
}
);
) nodes;

machineList = (map (key: getAttr key machines) (attrNames machines));
machineList = (map (key: builtins.getAttr key machines) (builtins.attrNames machines));
network = network'.network or {};
deployment = {
hosts = machineList;
Expand All @@ -119,21 +118,21 @@ in rec {
machines = { argsFile, buildTargets ? null }:
let
fileArgs = builtins.fromJSON (builtins.readFile argsFile);
nodes' = filterAttrs (n: v: elem n fileArgs.Names) nodes; in
runCommand "morph"
nodes' = nwLib.filterAttrs (n: v: builtins.elem n fileArgs.Names) nodes; in
nwRunCommand "morph"
{ preferLocalBuild = true; }
(if buildTargets == null
then ''
mkdir -p $out
${toString (mapAttrsToList (nodeName: nodeDef: ''
${toString (nwLib.mapAttrsToList (nodeName: nodeDef: ''
ln -s ${nodeDef.config.system.build.toplevel} $out/${nodeName}
'') nodes')}
''
else ''
mkdir -p $out
${toString (mapAttrsToList (nodeName: nodeDef: ''
${toString (nwLib.mapAttrsToList (nodeName: nodeDef: ''
mkdir -p $out/${nodeName}
${toString (mapAttrsToList (buildName: buildFn: ''
${toString (nwLib.mapAttrsToList (buildName: buildFn: ''
ln -s ${buildFn nodeDef} $out/${nodeName}/${buildName}
'') buildTargets)}
'') nodes')}
Expand Down
14 changes: 7 additions & 7 deletions data/options.nix
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{ config, lib, pkgs, ... }:

with lib;
with lib.types;

let

inherit (lib) mkOption types;
inherit (types) attrsOf bool enum int path listOf nullOr str submodule;

ownerOptionsType = submodule ({ ... }: {
options = {
group = mkOption {
Expand Down Expand Up @@ -91,7 +91,7 @@ healthCheckType = submodule ({ ... }: {
};
});

httpHealthCheckType = types.submodule ({ ... }: {
httpHealthCheckType = submodule ({ ... }: {
options = {
description = mkOption {
type = str;
Expand Down Expand Up @@ -140,7 +140,7 @@ httpHealthCheckType = types.submodule ({ ... }: {
};
});

cmdHealthCheckType = types.submodule ({ ... }: {
cmdHealthCheckType = submodule ({ ... }: {
options = {
description = mkOption {
type = str;
Expand Down Expand Up @@ -255,7 +255,7 @@ in
# all derived dependencies.
config.system.extraDependencies =
let
cmds = concatMap (h: h.cmd) config.deployment.healthChecks.cmd;
cmds = builtins.concatMap (h: h.cmd) config.deployment.healthChecks.cmd;
in
[ (pkgs.writeText "healthcheck-commands.txt" (concatStringsSep "\n" cmds)) ];
[ (pkgs.writeText "healthcheck-commands.txt" (builtins.concatStringsSep "\n" cmds)) ];
}