diff --git a/manual/colorized-help.nix b/manual/colorized-help.nix index bb8eb79..c59639b 100644 --- a/manual/colorized-help.nix +++ b/manual/colorized-help.nix @@ -14,6 +14,7 @@ let "eval" "exec" "nix-info" + "repl" ]; renderHelp = subcommand: let fullCommand = if subcommand == null then "colmena" else "colmena ${subcommand}"; diff --git a/manual/src/features/eval.md b/manual/src/features/eval.md index f5ff19d..d940dd3 100644 --- a/manual/src/features/eval.md +++ b/manual/src/features/eval.md @@ -30,3 +30,18 @@ You may directly instantiate an expression that evaluates to a derivation: $ colmena eval --instantiate -E '{ nodes, ... }: nodes.alpha.config.boot.kernelPackages.kernel' /nix/store/7ggmhnwvywrqcd1z2sdpan8afz55sw7z-linux-5.14.14.drv ``` + +## Interactive REPL + +To explore the configurations interactively, start a REPL session with `colmena repl`: + +```console +$ colmena repl +[INFO ] Using flake: git+file:///home/user/cluster +Welcome to Nix 2.10.3. Type :? for help. + +Loading installable ''... +Added 3 variables. +nix-repl> nodes.alpha.config.deployment.targetHost +"fd12:3456::1" +``` diff --git a/manual/src/release-notes.md b/manual/src/release-notes.md index 26ddafc..5e83245 100644 --- a/manual/src/release-notes.md +++ b/manual/src/release-notes.md @@ -11,6 +11,7 @@ - The [`meta.allowApplyAll`](./reference/meta.md#allowapplyall) option has been added. If set to false, deployments without a node filter (`--on`) are disallowed ([#95](https://github.com/zhaofengli/colmena/issues/95)). - The `--no-substitutes` option under the `apply` subcommand has been renamed to `--no-substitute` ([#59](https://github.com/zhaofengli/colmena/issues/59)). - The [`meta.nodeSpecialArgs`](./reference/meta.md#nodespecialargs) option has been added. It allows specifying node-specific `specialArgs` passed to NixOS modules ([#100](https://github.com/zhaofengli/colmena/pull/100)). +- The [`repl`](./reference/cli.html#colmena-repl) subcommand has been added. It allows you to start an [interactive REPL](./features/eval.md#interactive-repl) with access to the complete node configurations. ## [Release 0.3.0](https://github.com/zhaofengli/colmena/releases/tag/v0.3.0) (2022/04/27) diff --git a/src/cli.rs b/src/cli.rs index b97c96e..04b9156 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -171,6 +171,7 @@ It's also possible to specify the preference using environment variables. See ClapCommand<'static> { + ClapCommand::new("repl") + .about("Start an interactive REPL with the complete configuration") + .long_about( + r#"Start an interactive REPL with the complete configuration + +In the REPL, you can inspect the configuration interactively with tab +completion. The node configurations are accessible under the `nodes` +attribute set."#, + ) +} + +pub async fn run(_global_args: &ArgMatches, local_args: &ArgMatches) -> Result<(), ColmenaError> { + let hive = util::hive_from_args(local_args).await?; + + let expr = hive.get_repl_expression(); + + let status = Command::new("nix") + .arg("repl") + // `nix repl` is expected to be marked as experimental: + // + .args(&["--experimental-features", "nix-command flakes"]) + .args(&["--expr", &expr]) + .status() + .await?; + + if !status.success() { + return Err(status.into()); + } + + Ok(()) +} diff --git a/src/nix/hive/mod.rs b/src/nix/hive/mod.rs index 49123cb..9cbccb8 100644 --- a/src/nix/hive/mod.rs +++ b/src/nix/hive/mod.rs @@ -369,6 +369,11 @@ impl Hive { } } + /// Returns the expression for a REPL session. + pub fn get_repl_expression(&self) -> String { + format!("{} hive.introspect (x: x)", self.get_base_expression()) + } + /// Returns the base expression from which the evaluated Hive can be used. fn get_base_expression(&self) -> String { self.assets.get_base_expression()