Skip to content

Commit ffd40d6

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

File tree

4 files changed

+55
-4
lines changed

4 files changed

+55
-4
lines changed

nix/eval-machine-info.nix

+5-3
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

+10
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,16 @@
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("--strict", action="store_true", help="enable strict evaluation")
539+
530540
subparser = add_subparser(
531541
subparsers,
532542
"list-generations",

nixops/deployment.py

+32-1
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ def _eval_flags(self, exprs: List[str]) -> List[str]:
395395
"--arg",
396396
"pluginNixExprs",
397397
py2nix(extraexprs),
398-
"<nixops/eval-machine-info.nix>",
398+
self.expr_path + "/eval-machine-info.nix",
399399
]
400400
)
401401
return flags
@@ -503,6 +503,37 @@ def evaluate(self) -> None:
503503
)
504504
self.definitions[name] = defn
505505

506+
def evaluate_code(self, code: str, json: bool = False, strict: bool = False) -> str:
507+
"""Evaluate nix code in the deployment specification."""
508+
509+
exprs = self.nix_exprs
510+
phys_expr = self.tempdir + "/physical.nix"
511+
with open(phys_expr, "w") as f:
512+
f.write(self.get_physical_spec())
513+
exprs.append(phys_expr)
514+
515+
try:
516+
return subprocess.check_output(
517+
["nix-instantiate"]
518+
+ self.extra_nix_eval_flags
519+
+ self._eval_flags(exprs)
520+
+ [
521+
"--eval-only",
522+
"--arg",
523+
"checkConfigurationOptions",
524+
"false",
525+
"--arg",
526+
"evalFile",
527+
code,
528+
]
529+
+ (["--strict"] if strict else [])
530+
+ (["--json"] if json else []),
531+
stderr=self.logger.log_file,
532+
text=True,
533+
)
534+
except subprocess.CalledProcessError:
535+
raise NixEvalError
536+
506537
def evaluate_option_value(
507538
self,
508539
machine_name: str,

nixops/script_defs.py

+8
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,14 @@ def op_show_option(args):
893893
)
894894

895895

896+
def op_eval(args):
897+
with deployment(args) as depl:
898+
depl.evaluate()
899+
sys.stdout.write(
900+
depl.evaluate_code(args.code, json=args.json, strict=args.strict)
901+
)
902+
903+
896904
@contextlib.contextmanager
897905
def deployment_with_rollback(args):
898906
with deployment(args) as depl:

0 commit comments

Comments
 (0)