rattler_config 0.6.0

A crate to configure rattler and derived tools.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
//! The shared configuration model for rattler-based tools.
//!
//! The entry point is [`ConfigBase<T>`]: the set of configuration keys that
//! every rattler-based tool (pixi, rattler-build, rattler-index, …)
//! understands, plus a tool-specific *extension* `T` whose keys live at the
//! top level of the same TOML document.
//!
//! See the crate-level documentation for a full example of writing an
//! extension.

use std::{
    collections::BTreeSet,
    ops::{Deref, DerefMut},
    path::{Path, PathBuf},
};

use indexmap::IndexMap;
use rattler_conda_types::{ChannelConfig, NamedChannelOrUrl};
use serde::{Deserialize, Serialize, de::DeserializeOwned};
use thiserror::Error;
use url::Url;

use crate::config::s3::S3OptionsMap;
use crate::config::{
    build::BuildConfig, concurrency::ConcurrencyConfig, index::IndexConfig, proxy::ProxyConfig,
    repodata_config::RepodataConfig, run_post_link_scripts::RunPostLinkScripts,
};

pub mod build;
pub mod channel_config;
pub mod concurrency;
pub mod index;
pub mod proxy;
pub mod repodata_config;
pub mod run_post_link_scripts;
pub mod s3;
pub mod tls;
use crate::config::channel_config::default_channel_config;

#[derive(Error, Debug, Clone, PartialEq, Eq)]
pub enum ValidationError {
    /// Missing required field.
    #[error("Missing required field: {0}")]
    MissingRequiredField(String),

    /// Invalid value for a field.
    #[error("Invalid value for field {0}: {1}")]
    InvalidValue(String, String),

    /// Invalid configuration for various reason.
    #[error("Invalid configuration: {0}")]
    Invalid(String),
}

#[derive(Error, Debug, Clone, PartialEq, Eq)]
pub enum MergeError {
    /// Error merging configurations.
    #[error("Error merging configurations: {0}")]
    Error(String),
}

#[derive(Error, Debug)]
pub enum LoadError {
    /// Error loading configuration.
    #[error("Error merging configuration files: {0} ({1})")]
    MergeError(MergeError, PathBuf),

    /// IO error while reading configuration file.
    #[error("IO error while reading configuration file: {0}")]
    IoError(#[from] std::io::Error),

    /// Error parsing configuration file.
    #[error("Error parsing configuration file: {0}")]
    ParseError(#[from] toml::de::Error),

    /// Error validating configuration.
    #[error("Error validating configuration: {0}")]
    ValidationError(#[from] ValidationError),
}

/// A fragment of configuration: either a nested section (like
/// [`ConcurrencyConfig`]) or a tool-specific extension plugged into
/// [`ConfigBase`].
///
/// Implementors only *have* to provide [`Config::merge_config`]; everything
/// else has a sensible default. Extensions must **not** use
/// `#[serde(deny_unknown_fields)]`: an extension is deserialized from the
/// full configuration document, so it has to tolerate the keys handled by
/// [`CommonConfig`] (and report them as ignored, which the derive does by
/// default).
pub trait Config:
    Serialize + DeserializeOwned + std::fmt::Debug + Clone + PartialEq + Default
{
    /// Merge another configuration (file) into this one.
    /// Note: the "other" configuration takes priority over the current one.
    fn merge_config(self, other: &Self) -> Result<Self, MergeError>;

    /// Validate the configuration. Called after all configuration files have
    /// been merged.
    fn validate(&self) -> Result<(), ValidationError> {
        Ok(())
    }

    /// Returns true if the configuration equals its default value.
    fn is_default(&self) -> bool {
        self == &Self::default()
    }

    /// The dotted TOML key paths understood by this fragment, used to build
    /// "supported keys" listings for error messages of `set`.
    ///
    /// Keys are relative to the fragment itself; [`ConfigBase`] adds the
    /// proper prefixes for nested sections.
    fn keys(&self) -> Vec<String> {
        Vec::new()
    }
}

/// Extension type for tools that have no tool-specific configuration keys.
///
/// This is the default extension of [`ConfigBase`].
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct NoExtension {}

impl Config for NoExtension {
    fn merge_config(self, _other: &Self) -> Result<Self, MergeError> {
        Ok(self)
    }
}

/// The configuration keys shared by all rattler-based tools.
///
/// This struct deliberately contains no `#[serde(flatten)]` fields: unknown
/// top-level keys must surface through `serde_ignored` so that
/// [`ConfigBase::from_toml_str`] can report typos and tool-specific keys
/// reliably (`flatten` swallows unknown keys before `serde_ignored` can see
/// them).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct CommonConfig {
    #[serde(default)]
    #[serde(alias = "default_channels")] // BREAK: remove to stop supporting snake_case alias
    #[serde(skip_serializing_if = "Option::is_none")]
    pub default_channels: Option<Vec<NamedChannelOrUrl>>,

    /// Path to the file containing the authentication token.
    #[serde(default)]
    #[serde(alias = "authentication_override_file")] // BREAK: remove to stop supporting snake_case alias
    #[serde(skip_serializing_if = "Option::is_none")]
    pub authentication_override_file: Option<PathBuf>,

    /// If set to true, the HTTPS client will not verify TLS server
    /// certificates.
    #[serde(default)]
    #[serde(alias = "tls_no_verify")] // BREAK: remove to stop supporting snake_case alias
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tls_no_verify: Option<bool>,

    /// Which TLS root certificates to use (webpki vs system).
    /// Accepts legacy spellings `"native"` and `"all"` as aliases for
    /// `"system"`. See [`tls::TlsRootCerts`] for details.
    #[serde(default)]
    #[serde(alias = "tls_root_certs")] // BREAK: remove to stop supporting snake_case alias
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tls_root_certs: Option<tls::TlsRootCerts>,

    #[serde(default)]
    #[serde(skip_serializing_if = "IndexMap::is_empty")]
    pub mirrors: IndexMap<Url, Vec<Url>>,

    #[serde(default, skip_serializing_if = "BuildConfig::is_default")]
    pub build: BuildConfig,

    #[serde(skip, default = "default_channel_config")]
    pub channel_config: ChannelConfig,

    /// Configuration for repodata fetching.
    #[serde(alias = "repodata_config")] // BREAK: remove to stop supporting snake_case alias
    #[serde(default, skip_serializing_if = "RepodataConfig::is_empty")]
    pub repodata_config: RepodataConfig,

    /// Configuration for the concurrency of rattler.
    #[serde(default)]
    #[serde(skip_serializing_if = "ConcurrencyConfig::is_default")]
    pub concurrency: ConcurrencyConfig,

    /// Https/Http proxy configuration.
    #[serde(default)]
    #[serde(skip_serializing_if = "ProxyConfig::is_default")]
    pub proxy_config: ProxyConfig,

    /// Configuration for S3.
    #[serde(default)]
    #[serde(skip_serializing_if = "S3OptionsMap::is_default")]
    pub s3_options: S3OptionsMap,

    /// Per-channel configuration for `rattler-index`.
    #[serde(default, skip_serializing_if = "IndexConfig::is_empty")]
    pub index_config: IndexConfig,

    /// Run the post link scripts
    #[serde(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub run_post_link_scripts: Option<RunPostLinkScripts>,

    /// If set to false, symbolic links will not be used during package
    /// installation.
    #[serde(default)]
    #[serde(alias = "allow_symbolic_links")] // BREAK: remove to stop supporting snake_case alias
    #[serde(skip_serializing_if = "Option::is_none")]
    pub allow_symbolic_links: Option<bool>,

    /// If set to false, hard links will not be used during package
    /// installation.
    #[serde(default)]
    #[serde(alias = "allow_hard_links")] // BREAK: remove to stop supporting snake_case alias
    #[serde(skip_serializing_if = "Option::is_none")]
    pub allow_hard_links: Option<bool>,

    /// If set to false, ref links (copy-on-write) will not be used during
    /// package installation.
    #[serde(default)]
    #[serde(alias = "allow_ref_links")] // BREAK: remove to stop supporting snake_case alias
    #[serde(skip_serializing_if = "Option::is_none")]
    pub allow_ref_links: Option<bool>,
    // Missing in rattler but should be available in pixi:
    //   experimental
    //   shell
    //   pinning_strategy
    //   detached_environments
    //   pypi_config
    //
    // Deprecated fields:
    //   change_ps1
    //   force_activate
}

// ChannelConfig does not implement `Default` so we need to provide a default
// implementation.
impl Default for CommonConfig {
    fn default() -> Self {
        Self {
            default_channels: None,
            authentication_override_file: None,
            tls_no_verify: None,
            tls_root_certs: None,
            mirrors: IndexMap::new(),
            build: BuildConfig::default(),
            channel_config: default_channel_config(),
            repodata_config: RepodataConfig::default(),
            concurrency: ConcurrencyConfig::default(),
            proxy_config: ProxyConfig::default(),
            s3_options: S3OptionsMap::default(),
            index_config: IndexConfig::default(),
            run_post_link_scripts: None,
            allow_symbolic_links: None,
            allow_hard_links: None,
            allow_ref_links: None,
        }
    }
}

/// Prefix every key in `keys` with `prefix.`.
fn prefixed_keys(prefix: &str, keys: Vec<String>) -> Vec<String> {
    keys.into_iter().map(|k| format!("{prefix}.{k}")).collect()
}

/// Is `key` equal to, or nested inside, one of the `ignored` paths?
fn covered_by(key: &str, ignored: &BTreeSet<String>) -> bool {
    ignored.iter().any(|path| {
        key == path
            || key
                .strip_prefix(path.as_str())
                .is_some_and(|rest| rest.starts_with('.'))
    })
}

impl Config for CommonConfig {
    /// Merge another configuration (file) into this one.
    /// Note: the "other" configuration takes priority over the current one.
    fn merge_config(self, other: &Self) -> Result<Self, MergeError> {
        Ok(Self {
            default_channels: other
                .default_channels
                .as_ref()
                .or(self.default_channels.as_ref())
                .cloned(),
            authentication_override_file: other
                .authentication_override_file
                .as_ref()
                .or(self.authentication_override_file.as_ref())
                .cloned(),
            tls_no_verify: other.tls_no_verify.or(self.tls_no_verify),
            tls_root_certs: other.tls_root_certs.or(self.tls_root_certs),
            mirrors: self
                .mirrors
                .iter()
                .chain(other.mirrors.iter())
                .map(|(k, v)| (k.clone(), v.clone()))
                .collect(),
            build: self.build.merge_config(&other.build)?,
            // Currently this is always the default so it doesn't matter which one we take.
            channel_config: self.channel_config,
            repodata_config: self.repodata_config.merge_config(&other.repodata_config)?,
            concurrency: self.concurrency.merge_config(&other.concurrency)?,
            proxy_config: self.proxy_config.merge_config(&other.proxy_config)?,
            s3_options: self.s3_options.merge_config(&other.s3_options)?,
            index_config: self.index_config.merge_config(&other.index_config)?,
            run_post_link_scripts: other
                .run_post_link_scripts
                .clone()
                .or(self.run_post_link_scripts),
            allow_symbolic_links: other.allow_symbolic_links.or(self.allow_symbolic_links),
            allow_hard_links: other.allow_hard_links.or(self.allow_hard_links),
            allow_ref_links: other.allow_ref_links.or(self.allow_ref_links),
        })
    }

    fn validate(&self) -> Result<(), ValidationError> {
        self.build.validate()?;
        self.repodata_config.validate()?;
        self.concurrency.validate()?;
        self.proxy_config.validate()?;
        self.s3_options.validate()?;
        self.index_config.validate()?;
        Ok(())
    }

    fn keys(&self) -> Vec<String> {
        let mut keys = vec![
            "default-channels".to_string(),
            "authentication-override-file".to_string(),
            "tls-no-verify".to_string(),
            "tls-root-certs".to_string(),
            "mirrors".to_string(),
            "run-post-link-scripts".to_string(),
            "allow-symbolic-links".to_string(),
            "allow-hard-links".to_string(),
            "allow-ref-links".to_string(),
            "s3-options".to_string(),
            "index-config".to_string(),
        ];
        keys.extend(prefixed_keys("build", self.build.keys()));
        keys.extend(prefixed_keys(
            "repodata-config",
            self.repodata_config.keys(),
        ));
        keys.extend(prefixed_keys("concurrency", self.concurrency.keys()));
        keys.extend(prefixed_keys("proxy-config", self.proxy_config.keys()));
        keys.extend(prefixed_keys("s3-options", self.s3_options.keys()));
        keys
    }
}

/// The complete configuration of a rattler-based tool: the
/// [common keys](CommonConfig) plus a tool-specific extension `T` whose keys
/// live at the top level of the same TOML document.
///
/// `ConfigBase` dereferences to [`CommonConfig`], so the shared keys can be
/// accessed directly: `config.default_channels`, `config.concurrency`, ….
#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)]
pub struct ConfigBase<T = NoExtension> {
    /// The configuration keys shared by all rattler-based tools.
    #[serde(flatten)]
    pub common: CommonConfig,

    /// Tool-specific configuration keys.
    #[serde(flatten)]
    pub extensions: T,

    /// The configuration files this configuration was loaded from, in load
    /// order (lowest precedence first).
    #[serde(skip)]
    pub loaded_from: Vec<PathBuf>,
}

impl<T> Deref for ConfigBase<T> {
    type Target = CommonConfig;

    fn deref(&self) -> &Self::Target {
        &self.common
    }
}

impl<T> DerefMut for ConfigBase<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.common
    }
}

impl<T> Config for ConfigBase<T>
where
    T: Config,
{
    /// Merge another configuration (file) into this one.
    /// Note: the "other" configuration takes priority over the current one.
    fn merge_config(self, other: &Self) -> Result<Self, MergeError> {
        Ok(Self {
            common: self.common.merge_config(&other.common)?,
            extensions: self.extensions.merge_config(&other.extensions)?,
            loaded_from: self
                .loaded_from
                .iter()
                .chain(&other.loaded_from)
                .cloned()
                .collect(),
        })
    }

    fn validate(&self) -> Result<(), ValidationError> {
        self.common.validate()?;
        self.extensions.validate()
    }

    /// Gather all the keys of the configuration, including the extension
    /// keys.
    fn keys(&self) -> Vec<String> {
        let mut keys = self.common.keys();
        keys.extend(self.extensions.keys());
        keys
    }
}

impl<T> ConfigBase<T>
where
    T: Config,
{
    /// Parse a configuration from a TOML string.
    ///
    /// Returns the parsed configuration together with the set of keys that
    /// neither [`CommonConfig`] nor the extension `T` recognized. Callers
    /// should surface these to the user as warnings (they are typos or
    /// keys of other tools).
    pub fn from_toml_str(input: &str) -> Result<(Self, BTreeSet<String>), toml::de::Error> {
        // The document is deserialized twice: once into the common
        // configuration and once into the extension. Each pass records the
        // keys it did not recognize; only keys unknown to *both* passes are
        // truly unused. This is also why `CommonConfig` must not contain
        // `#[serde(flatten)]` fields: flattening swallows unknown keys
        // before `serde_ignored` can see them.
        let mut unknown_to_common = BTreeSet::new();
        let common: CommonConfig = serde_ignored::deserialize(
            toml::de::Deserializer::parse(input)?,
            |path: serde_ignored::Path<'_>| {
                unknown_to_common.insert(path.to_string());
            },
        )?;

        let mut unknown_to_extension = BTreeSet::new();
        let extensions: T = serde_ignored::deserialize(
            toml::de::Deserializer::parse(input)?,
            |path: serde_ignored::Path<'_>| {
                unknown_to_extension.insert(path.to_string());
            },
        )?;

        // A key is truly unused when neither pass consumed it. The two
        // passes may record it at different depths (the extension ignores
        // an entire table that the common configuration partially
        // consumed, or vice versa), so match against ancestors too and
        // keep the most specific path.
        let unused = unknown_to_common
            .iter()
            .filter(|key| covered_by(key, &unknown_to_extension))
            .chain(
                unknown_to_extension
                    .iter()
                    .filter(|key| covered_by(key, &unknown_to_common)),
            )
            .cloned()
            .collect::<BTreeSet<String>>();
        // Drop entries whose descendants are also reported: the deeper
        // path is the actionable one.
        let unused = unused
            .iter()
            .filter(|key| {
                !unused.iter().any(|other| {
                    other
                        .strip_prefix(key.as_str())
                        .is_some_and(|rest| rest.starts_with('.'))
                })
            })
            .cloned()
            .collect();

        Ok((
            Self {
                common,
                extensions,
                loaded_from: Vec::new(),
            },
            unused,
        ))
    }

    /// Load the configuration by merging all the given files, in order:
    /// later files take precedence over earlier ones. Unrecognized keys are
    /// reported as `tracing` warnings; the merged configuration is validated
    /// before it is returned.
    ///
    /// Missing files result in an error; callers that search default
    /// locations should filter for existing files first (see
    /// [`ConfigBase::load_from_default_locations`]).
    pub fn load_from_files<I, P>(paths: I) -> Result<Self, LoadError>
    where
        I: IntoIterator<Item = P>,
        P: AsRef<Path>,
    {
        let mut config = Self::default();

        for path in paths {
            let path = path.as_ref();
            let content = fs_err::read_to_string(path)?;
            let (mut other, unused) = Self::from_toml_str(&content)?;
            for key in &unused {
                tracing::warn!(
                    "Ignoring unknown configuration key `{key}` in {}",
                    path.display()
                );
            }
            other.loaded_from.push(path.to_path_buf());
            config = config
                .merge_config(&other)
                .map_err(|e| LoadError::MergeError(e, path.to_path_buf()))?;
        }

        config.validate()?;
        Ok(config)
    }

    /// Load the configuration from the default locations of the given tools
    /// (e.g. `&["pixi", "rattler-build"]`), skipping files that do not
    /// exist. See [`crate::locations::config_search_paths`] for the exact
    /// search order.
    pub fn load_from_default_locations(tool_dirs: &[&str]) -> Result<Self, LoadError> {
        Self::load_from_files(
            crate::locations::config_search_paths(tool_dirs)
                .into_iter()
                .filter(|path| path.is_file()),
        )
    }
}