Skip to content

Commit

Permalink
add nixops eval subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
manveru committed Apr 24, 2020
1 parent 9962fe4 commit ffd40d6
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 4 deletions.
8 changes: 5 additions & 3 deletions nix/eval-machine-info.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
, deploymentName
, args
, pluginNixExprs
, evalFile ? null
}:

with import <nixpkgs> { inherit system; };
with lib;


rec {
let
evaluator = if evalFile != null then (import evalFile) else id;
in evaluator (rec {

importedPluginNixExprs = map
(expr: import expr)
Expand Down Expand Up @@ -200,4 +202,4 @@ rec {
getNixOpsArgs = fs: lib.zipAttrs (lib.unique (lib.concatMap fileToArgs (getNixOpsExprs fs)));

nixopsArguments = getNixOpsArgs networkExprs;
}
})
10 changes: 10 additions & 0 deletions nixops/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,16 @@
help="include the physical specification in the evaluation",
)

subparser = add_subparser(
subparsers, "eval", help="eval the given file is nix code in the network expression"
)
subparser.set_defaults(op=op_eval)
subparser.add_argument("code", metavar="CODE", help="code")
subparser.add_argument(
"--json", action="store_true", help="print the option value in JSON format"
)
subparser.add_argument("--strict", action="store_true", help="enable strict evaluation")

subparser = add_subparser(
subparsers,
"list-generations",
Expand Down
33 changes: 32 additions & 1 deletion nixops/deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ def _eval_flags(self, exprs: List[str]) -> List[str]:
"--arg",
"pluginNixExprs",
py2nix(extraexprs),
"<nixops/eval-machine-info.nix>",
self.expr_path + "/eval-machine-info.nix",
]
)
return flags
Expand Down Expand Up @@ -503,6 +503,37 @@ def evaluate(self) -> None:
)
self.definitions[name] = defn

def evaluate_code(self, code: str, json: bool = False, strict: bool = False) -> str:
"""Evaluate nix code in the deployment specification."""

exprs = self.nix_exprs
phys_expr = self.tempdir + "/physical.nix"
with open(phys_expr, "w") as f:
f.write(self.get_physical_spec())
exprs.append(phys_expr)

try:
return subprocess.check_output(
["nix-instantiate"]
+ self.extra_nix_eval_flags
+ self._eval_flags(exprs)
+ [
"--eval-only",
"--arg",
"checkConfigurationOptions",
"false",
"--arg",
"evalFile",
code,
]
+ (["--strict"] if strict else [])
+ (["--json"] if json else []),
stderr=self.logger.log_file,
text=True,
)
except subprocess.CalledProcessError:
raise NixEvalError

def evaluate_option_value(
self,
machine_name: str,
Expand Down
8 changes: 8 additions & 0 deletions nixops/script_defs.py
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,14 @@ def op_show_option(args):
)


def op_eval(args):
with deployment(args) as depl:
depl.evaluate()
sys.stdout.write(
depl.evaluate_code(args.code, json=args.json, strict=args.strict)
)


@contextlib.contextmanager
def deployment_with_rollback(args):
with deployment(args) as depl:
Expand Down

0 comments on commit ffd40d6

Please sign in to comment.