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
5use crate::command::MyCommand;5use crate::command::MyCommand;
6use crate::host::Config;6use crate::host::Config;
7use crate::nix_path;7use crate::nix_path;
8use anyhow::{anyhow, Result};8use anyhow::{anyhow, Result, Context};
9use clap::Parser;9use clap::Parser;
10use itertools::Itertools;10use itertools::Itertools;
11use tokio::{task::LocalSet, time::sleep};11use tokio::{task::LocalSet, time::sleep};
292 let action = Action::from(self.subcommand.clone());292 let action = Action::from(self.subcommand.clone());
293 let drv = config293 let drv = config
294 .fleet_field294 .fleet_field
295 .select(nix_path!(.buildSystems.{action.build_attr()}.{&host}))295 .select(nix_path!(.buildSystems((serde_json::json!({
296 "localSystem": config.local_system.clone(),
297 }))).{action.build_attr()}.{&host}))
296 .await?;298 .await.context("system attribute")?;
297 let outputs = drv.build().await.map_err(|e| {299 let outputs = drv.build().await.map_err(|e| {
298 if action.build_attr() == "sdImage" {300 if action.build_attr() == "sdImage" {
299 info!("sd-image build failed");301 info!("sd-image build failed");
300 info!("Make sure you have imported modulesPath/installer/sd-card/sd-image-<arch>[-installer].nix (For installer, you may want to check config)");302 info!("Make sure you have imported modulesPath/installer/sd-card/sd-image-<arch>[-installer].nix (For installer, you may want to check config)");
301 info!("This module was automatically imported before, but was removed for better customization")
302 }303 }
303 e304 e
304 })?;305 })?;
311 if !config.is_local(&host) {312 if !config.is_local(&host) {
312 info!("uploading system closure");313 info!("uploading system closure");
313 {314 {
315 // Alternatively, nix store make-content-addressed can be used,
316 // at least for the first deployment, to provide trusted store key.
317 //
318 // It is much slower, yet doesn't require root on the deployer machine.
314 let mut sign = MyCommand::new("nix");319 let mut sign = MyCommand::new("nix");
315 // Private key for host machine is registered in nix-sign.nix320 // Private key for host machine is registered in nix-sign.nix
316 sign.arg("store")321 sign.arg("store")
modifiedcmds/fleet/src/host.rsdiffbeforeafterboth
13use tempfile::NamedTempFile;13use tempfile::NamedTempFile;
1414
15use crate::{15use crate::{
16 better_nix_eval::{Field, Index, NixSessionPool},16 better_nix_eval::{Field, NixSessionPool},
17 command::MyCommand,17 command::MyCommand,
18 fleetdata::{FleetData, FleetSecret, FleetSharedSecret},18 fleetdata::{FleetData, FleetSecret, FleetSharedSecret},
19 nix_path,19 nix_path,
250 #[clap(long)]250 #[clap(long)]
251 pub localhost: Option<String>,251 pub localhost: Option<String>,
252252
253 // TODO: unhardcode x86_64-linux
254 /// Override detected system for host, to perform builds via253 /// Override detected system for host, to perform builds via
255 /// binfmt-declared qemu instead of trying to crosscompile254 /// binfmt-declared qemu instead of trying to crosscompile
256 #[clap(long, default_value = "detect")]255 #[clap(long, default_value = "detect")]
280 let fleet_root = Field::field(root_field, "fleetConfigurations").await?;279 let fleet_root = Field::field(root_field, "fleetConfigurations").await?;
281280
282 let fleet_field = fleet_root281 let fleet_field = fleet_root
283 .select(nix_path!(.default.{&local_system}))282 .select(nix_path!(.default))
284 .await?;283 .await?;
285 let config_field = fleet_field284 let config_field = fleet_field
286 .select(nix_path!(.configUnchecked))285 .select(nix_path!(.configUnchecked))
modifiedcmds/fleet/src/main.rsdiffbeforeafterboth
24use host::{Config, FleetOpts};24use host::{Config, FleetOpts};
25use human_repr::HumanCount;25use human_repr::HumanCount;
26use indicatif::{ProgressState, ProgressStyle};26use indicatif::{ProgressState, ProgressStyle};
27use tracing::{info, metadata::LevelFilter};27use tracing::info;
28use tracing::{info_span, Instrument};28use tracing::{info_span, Instrument};
29use tracing_indicatif::IndicatifLayer;29use tracing_indicatif::IndicatifLayer;
30use tracing_subscriber::{prelude::*, EnvFilter};30use tracing_subscriber::{prelude::*, EnvFilter};
99 Ok(())99 Ok(())
100}100}
101
102// fn main() -> Result<()> {
103// let pool = r2d2::Builder::<NixSessionPool>::new()
104// .min_idle(Some(1))
105// .max_lifetime(Some(Duration::from_secs(10)))
106// .build(NixSessionPool {
107// flake: ".".to_owned(),
108// nix_args: vec![],
109// })?;
110// let conn = pool.get()?;
111// let field = Field::root(conn);
112// // let builtins = field.get_field("builtins")?;
113// let cur_sys: String = field.get_field("builtins")?.as_json()?;
114// eprintln!("current system = {cur_sys}");
115// let v = field.get_field("fleetConfigurations")?;
116// eprintln!("configs = {:?}", v.list_fields()?);
117// let d = v.get_field("default")?;
118// dbg!(d.list_fields());
119// Ok(())
120// }
121//
122101
123fn setup_logging() {102fn setup_logging() {
124 let indicatif_layer = IndicatifLayer::new().with_progress_style(103 let indicatif_layer = IndicatifLayer::new().with_progress_style(
157 ),136 ),
158 );137 );
159138
160 let filter = EnvFilter::from_default_env().add_directive(LevelFilter::INFO.into());139 let filter = EnvFilter::from_default_env();
161140
162 tracing_subscriber::registry()141 tracing_subscriber::registry()
163 .with(142 .with(
modifiedlib/default.nixdiffbeforeafterboth
11 inherit nixpkgs hostNames;11 inherit nixpkgs hostNames;
12 };12 };
13 in13 in
14 # Top-level arg is the builder system (not the target system!)
15 nixpkgs.lib.genAttrs flake-utils.lib.defaultSystems (system: let14 let
16 withData = data: rec {15 withData = data: rec {
17 root = nixpkgs.lib.evalModules {16 root = nixpkgs.lib.evalModules {
18 modules = (import ../modules/fleet/_modules.nix) ++ [config data];17 modules = (import ../modules/fleet/_modules.nix) ++ [config data];
39 modules =38 modules = configuredHosts.${name}.modules ++ extraModules;
40 configuredHosts.${name}.modules
41 ++ extraModules
42 ++ [
43 ({...}: {
44 nixpkgs.system = system;
45 nixpkgs.localSystem.system = system;
46 nixpkgs.crossSystem =
47 if system == configuredHosts.${name}.system
48 then null
49 else {
50 system = configuredHosts.${name}.system;
51 };
52 })
53 ];
54 specialArgs = {39 specialArgs = {
55 inherit fleetLib;40 inherit fleetLib;
56 fleet = fleetLib.hostsToAttrs (host: configuredSystems.${host}.config);41 fleet = fleetLib.hostsToAttrs (host: configuredSystems.${host}.config);
60 )45 )
61 (builtins.attrNames rootAssertWarn.config.hosts)46 (builtins.attrNames rootAssertWarn.config.hosts)
62 );47 );
63 buildSystems = {48 buildSystems = {localSystem}: let
49 buildConfigurationModule = {config, ...}: {
50 # Equivalent to nixpkgs.localSystem
51 # nixpkgs.system = localSystem;
52 nixpkgs.buildPlatform.system = localSystem;
53 };
54 in {
64 toplevel = builtins.mapAttrs (_name: value: value.config.system.build.toplevel) (configuredSystemsWithExtraModules [55 toplevel = builtins.mapAttrs (_name: value: value.config.system.build.toplevel) (configuredSystemsWithExtraModules [
56 buildConfigurationModule
65 ({...}: {57 ({...}: {
66 buildTarget = "toplevel";58 buildTarget = "toplevel";
67 })59 })
68 ]);60 ]);
69 sdImage = builtins.mapAttrs (_name: value: value.config.system.build.sdImage) (configuredSystemsWithExtraModules [61 sdImage = builtins.mapAttrs (_name: value: value.config.system.build.sdImage) (configuredSystemsWithExtraModules [
62 buildConfigurationModule
70 #(nixpkgs + "/nixos/modules/installer/sd-card/sd-image-aarch64-installer.nix")63 #(nixpkgs + "/nixos/modules/installer/sd-card/sd-image-aarch64-installer.nix")
71 ({...}: {64 ({...}: {
72 buildTarget = "sd-image";65 buildTarget = "sd-image";
73 })66 })
74 ]);67 ]);
75 installationCd = builtins.mapAttrs (_name: value: value.config.system.build.isoImage) (configuredSystemsWithExtraModules [68 installationCd = builtins.mapAttrs (_name: value: value.config.system.build.isoImage) (configuredSystemsWithExtraModules [
69 buildConfigurationModule
76 (nixpkgs + "/nixos/modules/installer/cd-dvd/installation-cd-minimal.nix")70 (nixpkgs + "/nixos/modules/installer/cd-dvd/installation-cd-minimal.nix")
77 ({lib, ...}: {71 ({lib, ...}: {
78 buildTarget = "installation-cd";72 buildTarget = "installation-cd";
91 in {85 in {
92 inherit (injectedData) configuredHosts configuredSecrets configuredSystems buildSystems configUnchecked;86 inherit (injectedData) configuredHosts configuredSecrets configuredSystems buildSystems configUnchecked;
93 };87 };
94 });88 };
95}89}
9690