Skip to content

Commit a6904bf

Browse files
committed
add nixops eval subcommand
1 parent 9962fe4 commit a6904bf

File tree

4 files changed

+61
-19
lines changed

4 files changed

+61
-19
lines changed

nix/eval-machine-info.nix

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
, deploymentName
66
, args
77
, pluginNixExprs
8+
, evalFile ? null
89
}:
910

1011
with import <nixpkgs> { inherit system; };
1112
with lib;
1213

13-
14-
rec {
14+
let
15+
evaluator = if evalFile != null then (import evalFile) else id;
16+
in evaluator (rec {
1517

1618
importedPluginNixExprs = map
1719
(expr: import expr)
@@ -200,4 +202,4 @@ rec {
200202
getNixOpsArgs = fs: lib.zipAttrs (lib.unique (lib.concatMap fileToArgs (getNixOpsExprs fs)));
201203

202204
nixopsArguments = getNixOpsArgs networkExprs;
203-
}
205+
})

nixops/__main__.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,18 @@
527527
help="include the physical specification in the evaluation",
528528
)
529529

530+
subparser = add_subparser(
531+
subparsers, "eval", help="eval the given file is nix code in the network expression"
532+
)
533+
subparser.set_defaults(op=op_eval)
534+
subparser.add_argument("code", metavar="CODE", help="code")
535+
subparser.add_argument(
536+
"--json", action="store_true", help="print the option value in JSON format"
537+
)
538+
subparser.add_argument(
539+
"--strict", action="store_true", help="enable strict evaluation"
540+
)
541+
530542
subparser = add_subparser(
531543
subparsers,
532544
"list-generations",

nixops/deployment.py

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -380,22 +380,12 @@ def _eval_flags(self, exprs: List[str]) -> List[str]:
380380

381381
flags.extend(
382382
[
383-
"--arg",
384-
"networkExprs",
385-
py2nix(exprs_, inline=True),
386-
"--arg",
387-
"args",
388-
py2nix(args, inline=True),
389-
"--argstr",
390-
"uuid",
391-
self.uuid,
392-
"--argstr",
393-
"deploymentName",
394-
self.name if self.name else "",
395-
"--arg",
396-
"pluginNixExprs",
397-
py2nix(extraexprs),
398-
"<nixops/eval-machine-info.nix>",
383+
"--arg", "networkExprs", py2nix(exprs_, inline=True),
384+
"--arg", "args", py2nix(args, inline=True),
385+
"--argstr", "uuid", self.uuid,
386+
"--argstr", "deploymentName", self.name if self.name else "",
387+
"--arg", "pluginNixExprs", py2nix(extraexprs),
388+
self.expr_path + "/eval-machine-info.nix",
399389
]
400390
)
401391
return flags
@@ -503,6 +493,38 @@ def evaluate(self) -> None:
503493
)
504494
self.definitions[name] = defn
505495

496+
def evaluate_code(
497+
self,
498+
code: str,
499+
json: bool = False,
500+
strict: bool = False
501+
) -> str:
502+
"""Evaluate nix code in the deployment specification."""
503+
504+
exprs = self.nix_exprs
505+
phys_expr = self.tempdir + "/physical.nix"
506+
with open(phys_expr, "w") as f:
507+
f.write(self.get_physical_spec())
508+
exprs.append(phys_expr)
509+
510+
try:
511+
return subprocess.check_output(
512+
["nix-instantiate"]
513+
+ self.extra_nix_eval_flags
514+
+ self._eval_flags(exprs)
515+
+ [
516+
"--eval-only",
517+
"--arg", "checkConfigurationOptions", "false",
518+
"--arg", "evalFile", code,
519+
]
520+
+ (["--strict"] if strict else [])
521+
+ (["--json"] if json else []),
522+
stderr=self.logger.log_file,
523+
text=True,
524+
)
525+
except subprocess.CalledProcessError:
526+
raise NixEvalError
527+
506528
def evaluate_option_value(
507529
self,
508530
machine_name: str,

nixops/script_defs.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,12 @@ def op_show_option(args):
892892
)
893893
)
894894

895+
def op_eval(args):
896+
with deployment(args) as depl:
897+
depl.evaluate()
898+
sys.stdout.write(
899+
depl.evaluate_code(args.code, json=args.json, strict=args.strict)
900+
)
895901

896902
@contextlib.contextmanager
897903
def deployment_with_rollback(args):

0 commit comments

Comments
 (0)