Skip to content

Commit d7de0e4

Browse files
cole-hgrahamc
andauthored
Fixup various issues with the nix.conf / nix.custom.conf split (#1393)
* fixup: no newline at the beginning of the config * Use extra-experimental-features instead of experimental-features * Move the user's ssl cert file into the custom config * Add a footer to the config So that we can place the "include" directive at the end of our standard config, allowing users to override our defaults. * Force `experimental-features` passed in `--extra-conf` to become `extra-experimental-features` Otherwise, users may accidentally override our `experimental-features` default of `nix-command` and `flakes`. If a user truly wanted to override this, they can manually modify the `nix.conf` / `nix.custom.conf` after the install completes. * Temporarily write trusted-users specified in `--extra-conf` to nix.conf _AND_ nix.custom.conf Cachix relies on the presence of this setting in the system `/etc/nix/nix.conf` so that it can provide users with a helpful error if `cachix use`ing a cache would not actually work for them (because only trusted users can modify the trusted caches and trusted cache signing keys in their user-specific configuration). * Test trusted-user, experimental-features changes * fixup: spacing, naming nits --------- Co-authored-by: Graham Christensen <graham@grahamc.com>
1 parent 35c93ca commit d7de0e4

File tree

3 files changed

+329
-54
lines changed

3 files changed

+329
-54
lines changed

src/action/base/create_directory.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ If `force_prune_on_revert` is set, the folder will always be deleted on
2121
#[serde(tag = "action_name", rename = "create_directory")]
2222
pub struct CreateDirectory {
2323
pub(crate) path: PathBuf,
24-
user: Option<String>,
25-
group: Option<String>,
26-
mode: Option<u32>,
27-
is_mountpoint: bool,
28-
force_prune_on_revert: bool,
24+
pub(crate) user: Option<String>,
25+
pub(crate) group: Option<String>,
26+
pub(crate) mode: Option<u32>,
27+
pub(crate) is_mountpoint: bool,
28+
pub(crate) force_prune_on_revert: bool,
2929
}
3030

3131
impl CreateDirectory {

src/action/base/create_or_merge_nix_config.rs

Lines changed: 59 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@ use crate::action::{
1515
Action, ActionDescription, ActionError, ActionErrorKind, ActionTag, StatefulAction,
1616
};
1717

18+
pub(crate) const TRUSTED_USERS_CONF_NAME: &str = "trusted-users";
19+
pub(crate) const EXPERIMENTAL_FEATURES_CONF_NAME: &str = "experimental-features";
20+
pub(crate) const EXTRA_EXPERIMENTAL_FEATURES_CONF_NAME: &str = "extra-experimental-features";
1821
/// The `nix.conf` configuration names that are safe to merge.
1922
// FIXME(@cole-h): make configurable by downstream users?
20-
const MERGEABLE_CONF_NAMES: &[&str] = &["experimental-features"];
23+
// NOTE(cole-h): evaluate if any additions here need to be handled in PlaceNixConfiguration::setup_extra_config
24+
const MERGEABLE_CONF_NAMES: &[&str] = &[EXPERIMENTAL_FEATURES_CONF_NAME];
2125
const NIX_CONF_MODE: u32 = 0o644;
2226
const NIX_CONF_COMMENT_CHAR: char = '#';
2327

@@ -48,6 +52,7 @@ pub struct CreateOrMergeNixConfig {
4852
pub(crate) path: PathBuf,
4953
pending_nix_config: NixConfig,
5054
header: String,
55+
footer: Option<String>,
5156
}
5257

5358
impl CreateOrMergeNixConfig {
@@ -56,13 +61,15 @@ impl CreateOrMergeNixConfig {
5661
path: impl AsRef<Path>,
5762
pending_nix_config: NixConfig,
5863
header: String,
64+
footer: Option<String>,
5965
) -> Result<StatefulAction<Self>, ActionError> {
6066
let path = path.as_ref().to_path_buf();
6167

6268
let this = Self {
6369
path,
6470
pending_nix_config,
6571
header,
72+
footer,
6673
};
6774

6875
if this.path.exists() {
@@ -405,6 +412,12 @@ impl Action for CreateOrMergeNixConfig {
405412
new_config.push('\n');
406413
}
407414

415+
if let Some(footer) = &self.footer {
416+
new_config.push('\n');
417+
new_config.push_str(footer);
418+
new_config.push('\n');
419+
}
420+
408421
temp_file
409422
.write_all(new_config.as_bytes())
410423
.await
@@ -466,15 +479,21 @@ mod test {
466479
nix_config
467480
.settings_mut()
468481
.insert("experimental-features".into(), "ca-references".into());
469-
let mut action =
470-
CreateOrMergeNixConfig::plan(&test_file, nix_config, "# Generated by".to_string())
471-
.await?;
482+
let mut action = CreateOrMergeNixConfig::plan(
483+
&test_file,
484+
nix_config,
485+
"# Generated by".to_string(),
486+
Some("# opa".into()),
487+
)
488+
.await?;
472489

473490
action.try_execute().await?;
474491

475492
let s = std::fs::read_to_string(&test_file)?;
476493
assert!(s.contains("# Generated by"));
477494
assert!(s.contains("ca-references"));
495+
496+
assert!(s.contains("# opa"));
478497
assert!(NixConfig::parse_file(&test_file).is_ok());
479498

480499
action.try_revert().await?;
@@ -494,9 +513,13 @@ mod test {
494513
nix_config
495514
.settings_mut()
496515
.insert("experimental-features".into(), "ca-references".into());
497-
let mut action =
498-
CreateOrMergeNixConfig::plan(&test_file, nix_config, "# Generated by".to_string())
499-
.await?;
516+
let mut action = CreateOrMergeNixConfig::plan(
517+
&test_file,
518+
nix_config,
519+
"# Generated by".to_string(),
520+
None,
521+
)
522+
.await?;
500523

501524
action.try_execute().await?;
502525

@@ -524,9 +547,13 @@ mod test {
524547
nix_config
525548
.settings_mut()
526549
.insert("experimental-features".into(), "flakes".into());
527-
let mut action =
528-
CreateOrMergeNixConfig::plan(&test_file, nix_config, "# Generated by".to_string())
529-
.await?;
550+
let mut action = CreateOrMergeNixConfig::plan(
551+
&test_file,
552+
nix_config,
553+
"# Generated by".to_string(),
554+
None,
555+
)
556+
.await?;
530557

531558
action.try_execute().await?;
532559

@@ -558,9 +585,13 @@ mod test {
558585
nix_config
559586
.settings_mut()
560587
.insert("allow-dirty".into(), "false".into());
561-
let mut action =
562-
CreateOrMergeNixConfig::plan(&test_file, nix_config, "# Generated by".to_string())
563-
.await?;
588+
let mut action = CreateOrMergeNixConfig::plan(
589+
&test_file,
590+
nix_config,
591+
"# Generated by".to_string(),
592+
None,
593+
)
594+
.await?;
564595

565596
action.try_execute().await?;
566597

@@ -605,7 +636,7 @@ mod test {
605636
nix_config
606637
.settings_mut()
607638
.insert("warn-dirty".into(), "false".into());
608-
match CreateOrMergeNixConfig::plan(&test_file, nix_config, "".to_string()).await {
639+
match CreateOrMergeNixConfig::plan(&test_file, nix_config, "".to_string(), None).await {
609640
Err(err) => {
610641
if let ActionErrorKind::Custom(e) = err.kind() {
611642
match e.downcast_ref::<CreateOrMergeNixConfigError>() {
@@ -647,9 +678,13 @@ mod test {
647678
nix_config
648679
.settings_mut()
649680
.insert("experimental-features".into(), "ca-references".into());
650-
let mut action =
651-
CreateOrMergeNixConfig::plan(&test_file, nix_config, "# Generated by".to_string())
652-
.await?;
681+
let mut action = CreateOrMergeNixConfig::plan(
682+
&test_file,
683+
nix_config,
684+
"# Generated by".to_string(),
685+
None,
686+
)
687+
.await?;
653688

654689
action.try_execute().await?;
655690

@@ -681,9 +716,13 @@ mod test {
681716
nix_config
682717
.settings_mut()
683718
.insert("experimental-features".into(), "ca-references".into());
684-
let mut action =
685-
CreateOrMergeNixConfig::plan(&test_file, nix_config, "# Generated by".to_string())
686-
.await?;
719+
let mut action = CreateOrMergeNixConfig::plan(
720+
&test_file,
721+
nix_config,
722+
"# Generated by".to_string(),
723+
None,
724+
)
725+
.await?;
687726

688727
action.try_execute().await?;
689728

0 commit comments

Comments
 (0)