Skip to content

Commit b23df52

Browse files
authored
feat: allow to set environment variables from configuration (#118)
* feat: allow to set environment variables from configuration Fixes #117 * Fix clippy lint * Add tests * Rename `env_vars` to `env`
1 parent 8b62409 commit b23df52

File tree

8 files changed

+147
-7
lines changed

8 files changed

+147
-7
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/pixi-build-cmake/src/cmake.rs

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use rattler_build::{
1616
hash::HashInfo,
1717
metadata::{BuildConfiguration, PackagingSettings},
1818
recipe::{
19-
parser::{Build, Dependency, Package, Requirements, ScriptContent},
19+
parser::{Build, Dependency, Package, Requirements, Script, ScriptContent},
2020
variable::Variable,
2121
Recipe,
2222
},
@@ -152,7 +152,11 @@ impl<P: ProjectModel> CMakeBuildBackend<P> {
152152
string: Default::default(),
153153

154154
// skip: Default::default(),
155-
script: ScriptContent::Commands(build_script).into(),
155+
script: Script {
156+
content: ScriptContent::Commands(build_script),
157+
env: self.config.env.clone(),
158+
..Default::default()
159+
},
156160
noarch: noarch_type,
157161

158162
// TODO: Python is not exposed properly
@@ -254,14 +258,38 @@ pub(crate) fn construct_configuration(
254258
mod tests {
255259
use std::{collections::BTreeMap, path::PathBuf};
256260

261+
use indexmap::IndexMap;
257262
use pixi_build_type_conversions::to_project_model_v1;
258263
use pixi_manifest::Manifests;
259-
use rattler_build::console_utils::LoggingOutputHandler;
264+
use rattler_build::{console_utils::LoggingOutputHandler, recipe::Recipe};
260265
use rattler_conda_types::{ChannelConfig, Platform};
261266
use tempfile::tempdir;
262267

263268
use crate::{cmake::CMakeBuildBackend, config::CMakeBackendConfig};
264269

270+
fn recipe(manifest_source: &str, config: CMakeBackendConfig) -> Recipe {
271+
let tmp_dir = tempdir().unwrap();
272+
let tmp_manifest = tmp_dir.path().join("pixi.toml");
273+
std::fs::write(&tmp_manifest, manifest_source).unwrap();
274+
let manifest = Manifests::from_workspace_manifest_path(tmp_manifest.clone()).unwrap();
275+
let package = manifest.value.package.unwrap();
276+
let channel_config = ChannelConfig::default_with_root_dir(tmp_dir.path().to_path_buf());
277+
let project_model = to_project_model_v1(&package.value, &channel_config).unwrap();
278+
279+
let cmake_backend = CMakeBuildBackend::new(
280+
&tmp_manifest,
281+
project_model,
282+
config,
283+
LoggingOutputHandler::default(),
284+
None,
285+
)
286+
.unwrap();
287+
288+
cmake_backend
289+
.recipe(Platform::current(), &channel_config, &BTreeMap::new())
290+
.unwrap()
291+
}
292+
265293
#[tokio::test]
266294
async fn test_setting_host_and_build_requirements() {
267295
// get cargo manifest dir
@@ -355,4 +383,33 @@ mod tests {
355383

356384
Manifests::from_workspace_manifest_path(tmp_manifest).unwrap();
357385
}
386+
387+
#[test]
388+
fn test_env_vars_are_set() {
389+
let manifest_source = r#"
390+
[workspace]
391+
platforms = []
392+
channels = []
393+
preview = ["pixi-build"]
394+
395+
[package]
396+
name = "foobar"
397+
version = "0.1.0"
398+
399+
[package.build]
400+
backend = { name = "pixi-build-rust", version = "*" }
401+
"#;
402+
403+
let env = IndexMap::from([("foo".to_string(), "bar".to_string())]);
404+
405+
let recipe = recipe(
406+
manifest_source,
407+
CMakeBackendConfig {
408+
env: env.clone(),
409+
..Default::default()
410+
},
411+
);
412+
413+
assert_eq!(recipe.build.script.env, env);
414+
}
358415
}

crates/pixi-build-cmake/src/config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
use indexmap::IndexMap;
12
use serde::Deserialize;
23

34
#[derive(Debug, Default, Deserialize)]
45
#[serde(rename_all = "kebab-case")]
56
pub struct CMakeBackendConfig {
67
/// Extra args for CMake invocation
78
pub extra_args: Vec<String>,
9+
/// Environment Variables
10+
pub env: IndexMap<String, String>,
811
}

crates/pixi-build-python/src/config.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use indexmap::IndexMap;
12
use serde::Deserialize;
23
use std::convert::identity;
34

@@ -8,6 +9,9 @@ pub struct PythonBackendConfig {
89
/// to `true`.
910
#[serde(default)]
1011
pub noarch: Option<bool>,
12+
13+
/// Environment Variables
14+
pub env: IndexMap<String, String>,
1115
}
1216

1317
impl PythonBackendConfig {

crates/pixi-build-python/src/python.rs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rattler_build::{
1212
hash::HashInfo,
1313
metadata::{BuildConfiguration, PackagingSettings},
1414
recipe::{
15-
parser::{Build, Package, PathSource, Python, Requirements, ScriptContent, Source},
15+
parser::{Build, Package, PathSource, Python, Requirements, Script, ScriptContent, Source},
1616
variable::Variable,
1717
Recipe,
1818
},
@@ -187,7 +187,11 @@ impl<P: ProjectModel> PythonBuildBackend<P> {
187187
string: Default::default(),
188188

189189
// skip: Default::default(),
190-
script: ScriptContent::Commands(build_script).into(),
190+
script: Script {
191+
content: ScriptContent::Commands(build_script),
192+
env: self.config.env.clone(),
193+
..Default::default()
194+
},
191195
noarch: noarch_type,
192196

193197
python,
@@ -293,6 +297,7 @@ mod tests {
293297

294298
use std::{collections::BTreeMap, path::PathBuf};
295299

300+
use indexmap::IndexMap;
296301
use pixi_build_type_conversions::to_project_model_v1;
297302

298303
use pixi_manifest::Manifests;
@@ -346,6 +351,7 @@ mod tests {
346351
backend = { name = "pixi-build-python", version = "*" }
347352
"#, PythonBackendConfig {
348353
noarch: Some(false),
354+
..Default::default()
349355
}), {
350356
".source[0].path" => "[ ... path ... ]",
351357
".build.script" => "[ ... script ... ]",
@@ -566,4 +572,33 @@ requires = ["hatchling"]
566572
".build.script" => "[ ... script ... ]",
567573
});
568574
}
575+
576+
#[test]
577+
fn test_env_vars_are_set() {
578+
let manifest_source = r#"
579+
[workspace]
580+
platforms = []
581+
channels = []
582+
preview = ["pixi-build"]
583+
584+
[package]
585+
name = "foobar"
586+
version = "0.1.0"
587+
588+
[package.build]
589+
backend = { name = "pixi-build-python", version = "*" }
590+
"#;
591+
592+
let env = IndexMap::from([("foo".to_string(), "bar".to_string())]);
593+
594+
let recipe = recipe(
595+
manifest_source,
596+
PythonBackendConfig {
597+
env: env.clone(),
598+
..Default::default()
599+
},
600+
);
601+
602+
assert_eq!(recipe.build.script.env, env);
603+
}
569604
}

crates/pixi-build-rust/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ edition.workspace = true
66
[dependencies]
77
async-trait = { workspace = true }
88
chrono = { workspace = true }
9+
indexmap = { workspace = true }
910
miette = { workspace = true }
1011
minijinja = { workspace = true, features = ["json"] }
1112
rattler_conda_types = { workspace = true }

crates/pixi-build-rust/src/config.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
use indexmap::IndexMap;
2+
13
use serde::Deserialize;
24

35
#[derive(Debug, Default, Deserialize)]
46
#[serde(rename_all = "kebab-case")]
57
pub struct RustBackendConfig {
68
/// Extra args to pass for cargo
79
pub extra_args: Vec<String>,
10+
11+
/// Environment Variables
12+
pub env: IndexMap<String, String>,
813
}

crates/pixi-build-rust/src/rust.rs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rattler_build::{
1313
hash::HashInfo,
1414
metadata::{BuildConfiguration, PackagingSettings},
1515
recipe::{
16-
parser::{Build, Dependency, Package, Requirements, ScriptContent},
16+
parser::{Build, Dependency, Package, Requirements, Script, ScriptContent},
1717
variable::Variable,
1818
Recipe,
1919
},
@@ -127,7 +127,11 @@ impl<P: ProjectModel> RustBuildBackend<P> {
127127
build: Build {
128128
number: build_number,
129129
string: Default::default(),
130-
script: ScriptContent::Commands(build_script).into(),
130+
script: Script {
131+
content: ScriptContent::Commands(build_script),
132+
env: self.config.env.clone(),
133+
..Default::default()
134+
},
131135
noarch: noarch_type,
132136
..Build::default()
133137
},
@@ -202,6 +206,7 @@ mod tests {
202206

203207
use std::collections::BTreeMap;
204208

209+
use indexmap::IndexMap;
205210
use pixi_build_type_conversions::to_project_model_v1;
206211

207212
use pixi_manifest::Manifests;
@@ -257,4 +262,33 @@ mod tests {
257262
}),
258263
});
259264
}
265+
266+
#[test]
267+
fn test_env_vars_are_set() {
268+
let manifest_source = r#"
269+
[workspace]
270+
platforms = []
271+
channels = []
272+
preview = ["pixi-build"]
273+
274+
[package]
275+
name = "foobar"
276+
version = "0.1.0"
277+
278+
[package.build]
279+
backend = { name = "pixi-build-rust", version = "*" }
280+
"#;
281+
282+
let env = IndexMap::from([("foo".to_string(), "bar".to_string())]);
283+
284+
let recipe = recipe(
285+
manifest_source,
286+
RustBackendConfig {
287+
env: env.clone(),
288+
..Default::default()
289+
},
290+
);
291+
292+
assert_eq!(recipe.build.script.env, env);
293+
}
260294
}

0 commit comments

Comments
 (0)