Skip to content

Commit 5f70d1d

Browse files
authored
ProvisionDeterminateNixd: cleanup /etc/nix/nix.conf and /etc/nix (if empty) (#1417)
determinate-nixd fully manages /etc/nix/nix.conf, so it's safe to remove that file unconditionally. If there are no other files in /etc/nix, then we'll also remove that directory; otherwise, we leave it alone. If we don't clean this up, it prevents uninstall-reinstalls from working.
1 parent 18a6949 commit 5f70d1d

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/action/common/place_nix_configuration.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::settings::UrlOrPathOrString;
1414
use std::path::PathBuf;
1515

1616
pub const NIX_CONF_FOLDER: &str = "/etc/nix";
17-
const NIX_CONF: &str = "/etc/nix/nix.conf";
17+
pub const NIX_CONF: &str = "/etc/nix/nix.conf";
1818
const CUSTOM_NIX_CONF: &str = "/etc/nix/nix.custom.conf";
1919

2020
const NIX_CONFIG_HEADER: &str = r#"# Generated by https://github.com/DeterminateSystems/nix-installer.

src/action/common/provision_determinate_nixd.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use crate::{
88
util::OnMissing,
99
};
1010

11+
use super::place_nix_configuration::{NIX_CONF, NIX_CONF_FOLDER};
12+
1113
const DETERMINATE_NIXD_BINARY_PATH: &str = "/usr/local/bin/determinate-nixd";
1214
/**
1315
Provision the determinate-nixd binary
@@ -101,6 +103,26 @@ impl Action for ProvisionDeterminateNixd {
101103
.map_err(|e| ActionErrorKind::Remove(self.binary_location.clone(), e))
102104
.map_err(Self::error)?;
103105

106+
// NOTE(cole-h): If /etc/nix/nix.conf exists and we're reverting Determinate, we can safely
107+
// remove it, since determinate-nixd manages it.
108+
let nix_conf_path = PathBuf::from(NIX_CONF);
109+
crate::util::remove_file(&nix_conf_path, OnMissing::Ignore)
110+
.await
111+
.map_err(|e| ActionErrorKind::Remove(nix_conf_path, e))
112+
.map_err(Self::error)?;
113+
114+
// NOTE(cole-h): If /etc/nix/nix.conf was the last file in /etc/nix, then let's clean up the
115+
// entire directory too.
116+
let nix_conf_dir = PathBuf::from(NIX_CONF_FOLDER);
117+
if let Some(mut entries) = tokio::fs::read_dir(&nix_conf_dir).await.ok() {
118+
if entries.next_entry().await.ok().flatten().is_none() {
119+
crate::util::remove_dir_all(&nix_conf_dir, OnMissing::Ignore)
120+
.await
121+
.map_err(|e| ActionErrorKind::Remove(nix_conf_dir, e))
122+
.map_err(Self::error)?;
123+
}
124+
}
125+
104126
Ok(())
105127
}
106128
}

0 commit comments

Comments
 (0)