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 1 commit
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;
}
})
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"
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
subparsers, "eval", help="eval the given file is nix code in the network expression"
subparsers, "eval", help="evaluate a Nix expression with the NixOps network as arguments"

my proposed wording isn't quite right, but maybe something like this?

)
subparser.set_defaults(op=op_eval)
subparser.add_argument("code", metavar="CODE", help="code")
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
subparser.add_argument("code", metavar="CODE", help="code")
subparser.add_argument("file", metavar="FILE", help="File containing a Nix expression")

subparser.add_argument(
"--json", action="store_true", help="print the option value in JSON format"
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"--json", action="store_true", help="print the option value in JSON format"
"--json", action="store_true", help="print the resulting value as an JSON representation of the abstract syntax tree"

what does it print without --json?

)
subparser.add_argument("--strict", action="store_true", help="enable strict evaluation")
Copy link
Member

Choose a reason for hiding this comment

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

I don't have a specific suggestion, but we might want to crib what the nix-instantiate manual says:

recursively evaluate list elements and attributes. Normally, such sub-expressions are left unevaluated
(since the Nix expression language is lazy).
Warning
This option can cause non-termination, because lazy data structures can be infinitely large.

I wonder why someone wouldn't use --strict --json? Should --json imply --strict?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, --json will only work with --strict if it's more than one level deep.

The use-case here was if you wanted to evaluate something but finely control the level of strictness and output Nix instead of JSON. For that you'd want lazy evaluation of functions, which is impossible if --strict is hardcoded.

Given that this is supposed to be used by other tooling (that's why I don't pass a plain string but a file instead), it shouldn't be too confusing or hard to configure it for the specific usage.


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()
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.code, json=args.json, strict=args.strict)
)


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