git.delta.rocks / jrsonnet / refs/commits / 1aab6a2e63b6

difftreelog

refactor provide cross system using argument

Yaroslav Bolyukin2023-12-24parent: #89d3567.patch.diff
in: trunk

5 files changed

modifiedcmds/fleet/src/better_nix_eval.rsdiffbeforeafterboth
16use tokio::select;16use tokio::select;
17use tokio::sync::{mpsc, oneshot};17use tokio::sync::{mpsc, oneshot};
18use tokio_util::codec::{FramedRead, LinesCodec};18use tokio_util::codec::{FramedRead, LinesCodec};
19use tracing::{debug, error, warn};19use tracing::{debug, error, warn, Level};
2020
21use crate::command::{ClonableHandler, Handler, NixHandler, NoopHandler};21use crate::command::{ClonableHandler, Handler, NixHandler, NoopHandler};
2222
247 Ok(())247 Ok(())
248 }248 }
249 async fn send_command(&mut self, cmd: impl AsRef<[u8]>) -> Result<()> {249 async fn send_command(&mut self, cmd: impl AsRef<[u8]>) -> Result<()> {
250 if tracing::enabled!(Level::DEBUG) {
251 let cmd_str = String::from_utf8_lossy(cmd.as_ref());
252 tracing::debug!("{cmd_str}");
253 };
250 self.stdin.write_all(cmd.as_ref()).await?;254 self.stdin.write_all(cmd.as_ref()).await?;
251 self.stdin.write_all(b"\n").await?;255 self.stdin.write_all(b"\n").await?;
252 Ok(())256 Ok(())
420 }424 }
421 pub fn apply(v: impl Serialize) -> Self {425 pub fn apply(v: impl Serialize) -> Self {
422 let serialized = nixlike::serialize(v).expect("invalid value for apply");426 let serialized = nixlike::serialize(v).expect("invalid value for apply");
423 Self::Apply(serialized)427 Self::Apply(serialized.trim_end().to_owned())
424 }428 }
425}429}
426impl Display for Index {430impl Display for Index {
434 write!(f, ".{v}")438 write!(f, ".{v}")
435 }439 }
436 Index::Apply(o) => {440 Index::Apply(o) => {
437 let v = nixlike::serialize(o).map_err(|_| fmt::Error)?;
438 write!(f, "<apply>({v})")441 write!(f, "<apply>({o})")
439 }442 }
440 Index::Idx(i) => {443 Index::Idx(i) => {
441 write!(f, "[{i}]")444 write!(f, "[{i}]")
451struct PathDisplay<'i>(&'i [Index]);454struct PathDisplay<'i>(&'i [Index]);
452impl Display for PathDisplay<'_> {455impl Display for PathDisplay<'_> {
453 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {456 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
454 write!(f, "flake")?;
455 for i in self.0 {457 for i in self.0 {
456 write!(f, "{i}")?;458 write!(f, "{i}")?;
457 }459 }
508 query.push_str(escaped.trim());510 query.push_str(escaped.trim());
509 }511 }
510 Index::Apply(a) => {512 Index::Apply(a) => {
513 // In cases like `a {}.b` first `{}.b` will be evaluated, so `a {}` should be encased in `()`
511 query.push(' ');514 query = format!("({query} {a})");
512 query.push_str(&a);
513 }515 }
514 Index::Idx(idx) => {516 Index::Idx(idx) => {
515 query = format!("builtins.elemAt ({query}) {idx}");517 query = format!("builtins.elemAt ({query}) {idx}");
560 .await562 .await
561 .execute_expression_raw(&format!(":b sess_field_{id}"), &mut NixHandler::default())563 .execute_expression_raw(&format!(":b sess_field_{id}"), &mut NixHandler::default())
562 .await?;564 .await?;
563 ensure!(!vid.is_empty(), "build failed");565 ensure!(!vid.is_empty(), "build failed: {}", PathDisplay(&self.full_path));
564 let Some(vid) = vid.strip_prefix("This derivation produced the following outputs:\n")566 let Some(vid) = vid.strip_prefix("This derivation produced the following outputs:\n")
565 else {567 else {
566 panic!("unexpected build output: {vid:?}");568 panic!("unexpected build output: {vid:?}");
modifiedcmds/fleet/src/cmds/build_systems.rsdiffbeforeafterboth
--- a/cmds/fleet/src/cmds/build_systems.rs
+++ b/cmds/fleet/src/cmds/build_systems.rs
@@ -5,7 +5,7 @@
 use crate::command::MyCommand;
 use crate::host::Config;
 use crate::nix_path;
-use anyhow::{anyhow, Result};
+use anyhow::{anyhow, Result, Context};
 use clap::Parser;
 use itertools::Itertools;
 use tokio::{task::LocalSet, time::sleep};
@@ -292,13 +292,14 @@
 		let action = Action::from(self.subcommand.clone());
 		let drv = config
 			.fleet_field
-			.select(nix_path!(.buildSystems.{action.build_attr()}.{&host}))
-			.await?;
+			.select(nix_path!(.buildSystems((serde_json::json!({
+				"localSystem": config.local_system.clone(),
+			}))).{action.build_attr()}.{&host}))
+			.await.context("system attribute")?;
 		let outputs = drv.build().await.map_err(|e| {
 			if action.build_attr() == "sdImage" {
 				info!("sd-image build failed");
 				info!("Make sure you have imported modulesPath/installer/sd-card/sd-image-<arch>[-installer].nix (For installer, you may want to check config)");
-				info!("This module was automatically imported before, but was removed for better customization")
 			}
 			e
 		})?;
@@ -311,6 +312,10 @@
 				if !config.is_local(&host) {
 					info!("uploading system closure");
 					{
+						// Alternatively, nix store make-content-addressed can be used,
+						// at least for the first deployment, to provide trusted store key.
+						//
+						// It is much slower, yet doesn't require root on the deployer machine.
 						let mut sign = MyCommand::new("nix");
 						// Private key for host machine is registered in nix-sign.nix
 						sign.arg("store")
modifiedcmds/fleet/src/host.rsdiffbeforeafterboth
--- a/cmds/fleet/src/host.rs
+++ b/cmds/fleet/src/host.rs
@@ -13,7 +13,7 @@
 use tempfile::NamedTempFile;
 
 use crate::{
-	better_nix_eval::{Field, Index, NixSessionPool},
+	better_nix_eval::{Field, NixSessionPool},
 	command::MyCommand,
 	fleetdata::{FleetData, FleetSecret, FleetSharedSecret},
 	nix_path,
@@ -250,7 +250,6 @@
 	#[clap(long)]
 	pub localhost: Option<String>,
 
-	// TODO: unhardcode x86_64-linux
 	/// Override detected system for host, to perform builds via
 	/// binfmt-declared qemu instead of trying to crosscompile
 	#[clap(long, default_value = "detect")]
@@ -280,7 +279,7 @@
 		let fleet_root = Field::field(root_field, "fleetConfigurations").await?;
 
 		let fleet_field = fleet_root
-			.select(nix_path!(.default.{&local_system}))
+			.select(nix_path!(.default))
 			.await?;
 		let config_field = fleet_field
 			.select(nix_path!(.configUnchecked))
modifiedcmds/fleet/src/main.rsdiffbeforeafterboth
--- a/cmds/fleet/src/main.rs
+++ b/cmds/fleet/src/main.rs
@@ -24,7 +24,7 @@
 use host::{Config, FleetOpts};
 use human_repr::HumanCount;
 use indicatif::{ProgressState, ProgressStyle};
-use tracing::{info, metadata::LevelFilter};
+use tracing::info;
 use tracing::{info_span, Instrument};
 use tracing_indicatif::IndicatifLayer;
 use tracing_subscriber::{prelude::*, EnvFilter};
@@ -99,27 +99,6 @@
 	Ok(())
 }
 
-// fn main() -> Result<()> {
-// 	let pool = r2d2::Builder::<NixSessionPool>::new()
-// 		.min_idle(Some(1))
-// 		.max_lifetime(Some(Duration::from_secs(10)))
-// 		.build(NixSessionPool {
-// 			flake: ".".to_owned(),
-// 			nix_args: vec![],
-// 		})?;
-// 	let conn = pool.get()?;
-// 	let field = Field::root(conn);
-// 	// let builtins = field.get_field("builtins")?;
-// 	let cur_sys: String = field.get_field("builtins")?.as_json()?;
-// 	eprintln!("current system = {cur_sys}");
-// 	let v = field.get_field("fleetConfigurations")?;
-// 	eprintln!("configs = {:?}", v.list_fields()?);
-// 	let d = v.get_field("default")?;
-// 	dbg!(d.list_fields());
-// 	Ok(())
-// }
-//
-
 fn setup_logging() {
 	let indicatif_layer = IndicatifLayer::new().with_progress_style(
 		ProgressStyle::with_template(
@@ -157,7 +136,7 @@
 		),
 	);
 
-	let filter = EnvFilter::from_default_env().add_directive(LevelFilter::INFO.into());
+	let filter = EnvFilter::from_default_env();
 
 	tracing_subscriber::registry()
 		.with(
modifiedlib/default.nixdiffbeforeafterboth
--- a/lib/default.nix
+++ b/lib/default.nix
@@ -11,8 +11,7 @@
       inherit nixpkgs hostNames;
     };
   in
-    # Top-level arg is the builder system (not the target system!)
-    nixpkgs.lib.genAttrs flake-utils.lib.defaultSystems (system: let
+    let
       withData = data: rec {
         root = nixpkgs.lib.evalModules {
           modules = (import ../modules/fleet/_modules.nix) ++ [config data];
@@ -36,21 +35,7 @@
                 inherit name;
                 value = nixpkgs.lib.nixosSystem {
                   system = configuredHosts.${name}.system;
-                  modules =
-                    configuredHosts.${name}.modules
-                    ++ extraModules
-                    ++ [
-                      ({...}: {
-                        nixpkgs.system = system;
-                        nixpkgs.localSystem.system = system;
-                        nixpkgs.crossSystem =
-                          if system == configuredHosts.${name}.system
-                          then null
-                          else {
-                            system = configuredHosts.${name}.system;
-                          };
-                      })
-                    ];
+                  modules = configuredHosts.${name}.modules ++ extraModules;
                   specialArgs = {
                     inherit fleetLib;
                     fleet = fleetLib.hostsToAttrs (host: configuredSystems.${host}.config);
@@ -60,19 +45,28 @@
             )
             (builtins.attrNames rootAssertWarn.config.hosts)
           );
-        buildSystems = {
+        buildSystems = {localSystem}: let
+          buildConfigurationModule = {config, ...}: {
+            # Equivalent to nixpkgs.localSystem
+            # nixpkgs.system = localSystem;
+            nixpkgs.buildPlatform.system = localSystem;
+          };
+        in {
           toplevel = builtins.mapAttrs (_name: value: value.config.system.build.toplevel) (configuredSystemsWithExtraModules [
+            buildConfigurationModule
             ({...}: {
               buildTarget = "toplevel";
             })
           ]);
           sdImage = builtins.mapAttrs (_name: value: value.config.system.build.sdImage) (configuredSystemsWithExtraModules [
+            buildConfigurationModule
             #(nixpkgs + "/nixos/modules/installer/sd-card/sd-image-aarch64-installer.nix")
             ({...}: {
               buildTarget = "sd-image";
             })
           ]);
           installationCd = builtins.mapAttrs (_name: value: value.config.system.build.isoImage) (configuredSystemsWithExtraModules [
+            buildConfigurationModule
             (nixpkgs + "/nixos/modules/installer/cd-dvd/installation-cd-minimal.nix")
             ({lib, ...}: {
               buildTarget = "installation-cd";
@@ -91,5 +85,5 @@
       in {
         inherit (injectedData) configuredHosts configuredSecrets configuredSystems buildSystems configUnchecked;
       };
-    });
+    };
 }