From 7de62040eec5a79edbe3e898c8eb03946cfdd9df Mon Sep 17 00:00:00 2001 From: Yaroslav Bolyukin Date: Sat, 30 Nov 2024 04:38:30 +0000 Subject: [PATCH] refactor: replace builtins.currentSystem with pure alternative --- --- /dev/null +++ b/crates/fleet-base/build.rs @@ -0,0 +1,15 @@ +use std::env; + +fn main() { + let target = env::var("TARGET").expect("TARGET env var is set by cargo"); + + let nix_system = if target.starts_with("x86_64-unknown-linux-") { + "x86_64-linux" + } else if target.starts_with("aarch64-unknown-linux-") { + "aarch64-linux" + } else { + panic!("unknown nix system name for rust {target} triple!"); + }; + + println!("cargo:rustc-env=NIX_SYSTEM={nix_system}"); +} --- a/crates/fleet-base/src/opts.rs +++ b/crates/fleet-base/src/opts.rs @@ -8,7 +8,7 @@ use anyhow::Result; use clap::Parser; -use nix_eval::{nix_go, nix_go_json, util::assert_warn, NixSessionPool, Value}; +use nix_eval::{nix_go, util::assert_warn, NixSessionPool, Value}; use nom::{ bytes::complete::take_while1, character::complete::char, @@ -85,14 +85,16 @@ /// Override detected system for host, to perform builds via /// binfmt-declared qemu instead of trying to crosscompile - // TODO: Remove, as it is not used anymore. - #[clap(long, default_value = "detect")] + #[clap(long, default_value = env!("NIX_SYSTEM"))] pub local_system: String, } impl FleetOpts { - pub async fn filter_skipped(&self, hosts: impl IntoIterator) -> Result> { - let mut out = Vec::new(); + pub async fn filter_skipped( + &self, + hosts: impl IntoIterator, + ) -> Result> { + let mut out = Vec::new(); for host in hosts { if self.should_skip(&host).await? { continue; @@ -182,15 +184,15 @@ pub async fn build(&self, nix_args: Vec) -> Result { let directory = current_dir()?; - let pool = NixSessionPool::new(directory.as_os_str().to_owned(), nix_args.clone()).await?; + let pool = NixSessionPool::new( + directory.as_os_str().to_owned(), + nix_args.clone(), + self.local_system.clone(), + ) + .await?; let nix_session = pool.get().await?; let builtins_field = Value::binding(nix_session.clone(), "builtins").await?; - let local_system = if self.local_system == "detect" { - nix_go_json!(builtins_field.currentSystem) - } else { - self.local_system.clone() - }; let mut fleet_data_path = directory.clone(); fleet_data_path.push("fleet.nix"); @@ -210,14 +212,14 @@ let default_pkgs = nix_go!(nixpkgs(Obj { overlays, - system: local_system.clone(), + system: self.local_system.clone(), })); Ok(Config(Arc::new(FleetConfigInternals { nix_session, directory, data, - local_system, + local_system: self.local_system.clone(), nix_args, config_field, default_pkgs, --- a/crates/nix-eval/src/lib.rs +++ b/crates/nix-eval/src/lib.rs @@ -41,8 +41,8 @@ #[instrument(skip(session, values))] async fn build_multiple(name: String, session: NixSession, values: Vec) -> Result<()> { + let system = session.0.lock().await.nix_system.clone(); let builtins = Value::binding(session, "builtins").await?; - let system = nix_go!(builtins.currentSystem); let drv = nix_go!(builtins.derivation(Obj { system, name, --- a/crates/nix-eval/src/pool.rs +++ b/crates/nix-eval/src/pool.rs @@ -1,18 +1,23 @@ -use std::ffi::OsString; -use std::sync::{Arc, OnceLock}; +use std::{ + ffi::OsString, + sync::{Arc, OnceLock}, +}; use r2d2::Pool; -use crate::session::NixSessionInner; -use crate::{Error, NixSession, Result}; +use crate::{session::NixSessionInner, Error, NixSession, Result}; pub struct NixSessionPool(Pool); impl NixSessionPool { - pub async fn new(flake: OsString, nix_args: Vec) -> Result { + pub async fn new(flake: OsString, nix_args: Vec, nix_system: String) -> Result { let inner = tokio::task::block_in_place(|| { r2d2::Builder::::new() .min_idle(Some(0)) - .build(NixSessionPoolInner { flake, nix_args }) + .build(NixSessionPoolInner { + flake, + nix_args, + nix_system, + }) })?; Ok(Self(inner)) } @@ -25,6 +30,7 @@ pub(crate) struct NixSessionPoolInner { flake: OsString, nix_args: Vec, + pub(crate) nix_system: String, } impl r2d2::ManageConnection for NixSessionPoolInner { @@ -38,6 +44,7 @@ Ok(futures::executor::block_on(NixSessionInner::new( self.flake.as_os_str(), self.nix_args.iter().map(OsString::as_os_str), + self.nix_system.clone(), ))?) } --- a/crates/nix-eval/src/session.rs +++ b/crates/nix-eval/src/session.rs @@ -206,6 +206,8 @@ next_id: u32, pub(crate) free_list: Vec, + + pub nix_system: String, } /// Discover inter-message repl delimiter @@ -222,6 +224,7 @@ pub(crate) async fn new( flake: &OsStr, extra_args: impl IntoIterator, + nix_system: String, ) -> Result { let mut cmd = Command::new("nix"); cmd.arg("repl") @@ -280,6 +283,8 @@ next_id: 0, free_list: vec![], + + nix_system, }; res.train().await?; Ok(res) -- gitstuff