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

add nixops eval subcommand #1319

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
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;
}
})
16 changes: 16 additions & 0 deletions nixops/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,22 @@
help="include the physical specification in the evaluation",
)

subparser = add_subparser(
subparsers,
"eval",
help="evaluate a Nix expression with the NixOps network as arguments",
)
subparser.set_defaults(op=op_eval)
subparser.add_argument("file", metavar="FILE", help="file containing a Nix expression")
subparser.add_argument(
"--json", action="store_true", help="convert and print the return value as JSON"
)
subparser.add_argument(
"--strict",
action="store_true",
help="enable strict evaluation, (use with --json if value is more than a level deep)",
)

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, file: 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",
file,
]
+ (["--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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessarily to be fixed here, but it would be nice to think of a way for the deployment API to not require being careful about calling evaluate.

sys.stdout.write(
depl.evaluate_code(args.file, json=args.json, strict=args.strict)
)


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