git.delta.rocks / jrsonnet / refs/commits / 213ad7de4b85

difftreelog

feat experimental opentofu integration

Yaroslav Bolyukin2024-08-18parent: #505f82e.patch.diff
in: trunk

9 files changed

modifiedCargo.lockdiffbeforeafterboth
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1438,6 +1438,7 @@
  "itertools",
  "nixlike",
  "r2d2",
+ "regex",
  "serde",
  "serde_json",
  "thiserror",
@@ -1866,9 +1867,9 @@
 
 [[package]]
 name = "regex"
-version = "1.10.4"
+version = "1.10.6"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "c117dbdfde9c8308975b6a18d71f3f385c89461f7b3fb054288ecf2a2058ba4c"
+checksum = "4219d74c6b67a3654a9fbebc4b419e22126d13d2f3c4a07ee0cb61ff79a79619"
 dependencies = [
  "aho-corasick",
  "memchr",
modifiedcmds/fleet/src/cmds/mod.rsdiffbeforeafterboth
--- a/cmds/fleet/src/cmds/mod.rs
+++ b/cmds/fleet/src/cmds/mod.rs
@@ -2,3 +2,4 @@
 pub mod complete;
 pub mod info;
 pub mod secrets;
+pub mod tf;
addedcmds/fleet/src/cmds/tf.rsdiffbeforeafterboth
--- /dev/null
+++ b/cmds/fleet/src/cmds/tf.rs
@@ -0,0 +1,23 @@
+use anyhow::Result;
+use clap::Parser;
+use nix_eval::nix_go_json;
+use serde_json::Value;
+use tokio::fs::write;
+use tracing::info;
+
+use crate::host::Config;
+
+#[derive(Parser)]
+pub struct Tf;
+impl Tf {
+	pub async fn run(&self, config: &Config) -> Result<()> {
+		let system = &config.local_system;
+		let config = &config.config_field;
+		let data: Value = nix_go_json!(config.tf({ system }).config);
+		let str = serde_json::to_string_pretty(&data)?;
+
+		write("fleet.tf.json", str.as_bytes()).await?;
+
+		Ok(())
+	}
+}
modifiedcmds/fleet/src/main.rsdiffbeforeafterboth
--- a/cmds/fleet/src/main.rs
+++ b/cmds/fleet/src/main.rs
@@ -19,6 +19,7 @@
 	complete::Complete,
 	info::Info,
 	secrets::Secret,
+	tf::Tf,
 };
 use futures::{future::LocalBoxFuture, stream::FuturesUnordered, TryStreamExt};
 use host::{Config, FleetOpts};
@@ -86,6 +87,8 @@
 	/// Command completions
 	#[clap(hide(true))]
 	Complete(Complete),
+	/// Compile and evaluate terranix configuration
+	Tf(Tf),
 }
 
 #[derive(Parser)]
@@ -104,6 +107,7 @@
 		Opts::Secret(s) => s.run(config).await?,
 		Opts::Info(i) => i.run(config).await?,
 		Opts::Prefetch(p) => p.run(config).await?,
+		Opts::Tf(t) => t.run(config).await?,
 		// TODO: actually parse commands before starting the async runtime
 		Opts::Complete(c) => {
 			tokio::task::spawn_blocking(move || c.run(RootOpts::command())).await?
modifiedcrates/nix-eval/Cargo.tomldiffbeforeafterboth
--- a/crates/nix-eval/Cargo.toml
+++ b/crates/nix-eval/Cargo.toml
@@ -11,6 +11,7 @@
 itertools = "0.13.0"
 nixlike.workspace = true
 r2d2 = "0.8.10"
+regex = "1.10.6"
 serde = { workspace = true, features = ["derive"] }
 serde_json.workspace = true
 thiserror = "1.0.61"
modifiedcrates/nix-eval/src/session.rsdiffbeforeafterboth
--- a/crates/nix-eval/src/session.rs
+++ b/crates/nix-eval/src/session.rs
@@ -12,7 +12,7 @@
 	sync::{mpsc, oneshot, Mutex},
 };
 use tokio_util::codec::{FramedRead, LinesCodec};
-use tracing::{debug, error, warn, Level};
+use tracing::{debug, error, info, warn, Level};
 
 #[derive(Error, Debug)]
 pub enum Error {
@@ -327,8 +327,16 @@
 		n.parse::<u64>().map_err(Error::Int)
 	}
 	async fn execute_expression_string(&mut self, expr: impl AsRef<[u8]>) -> Result<String> {
+		// builtins.toJSON escapes some thing in incorrect way, e.g escaped "$" in "\${" is being outputed as "\$",
+		// while this escape should be removed as it is intended for nix itself, not for json output.
+		//
+		// This regex only allows \$ in the beginning of the string, it is easier to implement correctly.
+		// TODO: Add peg parser for nix-produced JSON?..
+		let regex = regex::Regex::new(r#"(?<prefix>[: {,\[]\\")\\\$"#).expect("fixup json");
+
 		let num = self.string_wrapping.clone();
 		let n = self.execute_expression_wrapping(expr, &num).await?;
+		let n = regex.replace_all(&n, "$prefix$$");
 		let str: String = serde_json::from_str(&n)?;
 		Ok(str)
 	}
@@ -339,6 +347,7 @@
 		let mut fexpr = b"builtins.toJSON (".to_vec();
 		fexpr.extend_from_slice(expr.as_ref());
 		fexpr.push(b')');
+		let s = String::from_utf8_lossy(expr.as_ref());
 		let v = self.execute_expression_string(fexpr).await?;
 		Ok(serde_json::from_str(&v)?)
 	}
modifiedflake.nixdiffbeforeafterboth
--- a/flake.nix
+++ b/flake.nix
@@ -37,6 +37,8 @@
         };
         flakeModule = flakeModules.default;
 
+        fleetModules.tf = ./modules/extras/tf.nix;
+
         # To be used with https://github.com/NixOS/nix/pull/8892
         schemas = let
           inherit (inputs.nixpkgs.lib) mapAttrs;
modifiedlib/flakePart.nixdiffbeforeafterboth
before · lib/flakePart.nix
1{crane}: {2  fleetLib,3  lib,4  config,5  ...6}: let7  inherit (lib.options) mkOption;8  inherit (lib.attrsets) mapAttrs;9  inherit (lib.types) lazyAttrsOf deferredModule unspecified;10  inherit (lib.strings) isPath;11  inherit (fleetLib.options) mkHostsOption;12in {13  options.fleetModules = mkOption {14    type = lazyAttrsOf unspecified;15    default = {};16  };17  options.fleetConfigurations = mkOption {18    type = lazyAttrsOf deferredModule;19    apply = nameToModule:20      mapAttrs (21        name: module: data: let22          # To use user-provided nixpkgs, we first need to extract wanted nixpkgs attribute,23          # to do that, evaluate all the modules with only needed option declared.24          bootstrapEval = lib.evalModules {25            modules = [26              module27              {28                options.nixpkgs.buildUsing = mkOption {29                  description = ''30                    Nixpkgs to use for fleetConfiguration evaluation.31                  '';32                };33                config._module.check = false;34              }35            ];36          };37          bootstrapNixpkgs = bootstrapEval.config.nixpkgs.buildUsing;38          normalEval = bootstrapNixpkgs.lib.evalModules {39            modules =40              (import ../modules/module-list.nix)41              ++ [42                module43                {44                  options.hosts = mkHostsOption {45                    nixos.nixpkgs.overlays = [46                      (final: prev:47                        import ../pkgs {48                          inherit (prev) callPackage;49                          craneLib = crane.mkLib prev;50                        })51                    ];52                  };53                  config = {54                    data =55                      if isPath data56                      then import data57                      else data;58                  };59                }60              ];61            specialArgs.fleetLib = import ../lib {62              inherit (bootstrapNixpkgs) lib;63            };64          };65        in66          normalEval67      )68      nameToModule;69  };70  config = {71    _module.args.fleetLib = import ../lib {inherit lib;};72    flake.fleetConfigurations = config.fleetConfigurations;73    flake.fleetModules = config.fleetModules;74  };7576  _file = ./flakePart.nix;77}
after · lib/flakePart.nix
1{crane}: {2  fleetLib,3  lib,4  config,5  inputs ? {},6  ...7}: let8  inherit (lib.options) mkOption;9  inherit (lib.attrsets) mapAttrs;10  inherit (lib.types) lazyAttrsOf deferredModule unspecified;11  inherit (lib.strings) isPath;12  inherit (fleetLib.options) mkHostsOption;13in {14  options.fleetModules = mkOption {15    type = lazyAttrsOf unspecified;16    default = {};17  };18  options.fleetConfigurations = mkOption {19    type = lazyAttrsOf deferredModule;20    apply = nameToModule:21      mapAttrs (22        name: module: data: let23          # To use user-provided nixpkgs, we first need to extract wanted nixpkgs attribute,24          # to do that, evaluate all the modules with only needed option declared.25          bootstrapEval = lib.evalModules {26            modules = [27              module28              {29                options.nixpkgs.buildUsing = mkOption {30                  description = ''31                    Nixpkgs to use for fleetConfiguration evaluation.32                  '';33                };34                config._module.check = false;35              }36            ];37          };38          bootstrapNixpkgs = bootstrapEval.config.nixpkgs.buildUsing;39          normalEval = bootstrapNixpkgs.lib.evalModules {40            modules =41              (import ../modules/module-list.nix)42              ++ [43                module44                {45                  options.hosts = mkHostsOption {46                    nixos.nixpkgs.overlays = [47                      (final: prev:48                        import ../pkgs {49                          inherit (prev) callPackage;50                          craneLib = crane.mkLib prev;51                        })52                    ];53                  };54                  config = {55                    data =56                      if isPath data57                      then import data58                      else data;59                  };60                }61              ];62            specialArgs = {63              fleetLib = import ../lib {64                inherit (bootstrapNixpkgs) lib;65              };66              inputs = inputs;67            };68          };69        in70          normalEval71      )72      nameToModule;73  };74  config = {75    _module.args.fleetLib = import ../lib {inherit lib;};76    flake.fleetConfigurations = config.fleetConfigurations;77    flake.fleetModules = config.fleetModules;78  };7980  _file = ./flakePart.nix;81}
addedmodules/extras/tf.nixdiffbeforeafterboth
--- /dev/null
+++ b/modules/extras/tf.nix
@@ -0,0 +1,26 @@
+{
+  config,
+  lib,
+  inputs,
+  ...
+}: let
+  inherit (lib) mkOption;
+  inherit (lib.types) deferredModule;
+in {
+  options.tf = mkOption {
+    type = deferredModule;
+    apply = module: system:
+      inputs.terranix.lib.terranixConfigurationAst {
+        inherit system;
+        pkgs = config.nixpkgs.buildUsing.legacyPackages.${system};
+        modules = [module];
+      };
+  };
+  config.tf.output.fleet = {
+    value = {
+      managed = true;
+    };
+    # Just to avoid printing this attribute on every apply.
+    sensitive = true;
+  };
+}