Skip to content

Commit 26e34cb

Browse files
committed
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).
1 parent 850c2d6 commit 26e34cb

File tree

2 files changed

+49
-12
lines changed

2 files changed

+49
-12
lines changed

src/action/base/create_or_merge_nix_config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use crate::action::{
1515
Action, ActionDescription, ActionError, ActionErrorKind, ActionTag, StatefulAction,
1616
};
1717

18+
pub(crate) const TRUSTED_USERS_CONF_NAME: &str = "trusted-users";
1819
pub(crate) const EXPERIMENTAL_FEATURES_CONF_NAME: &str = "experimental-features";
1920
pub(crate) const EXTRA_EXPERIMENTAL_FEATURES_CONF_NAME: &str = "extra-experimental-features";
2021
/// The `nix.conf` configuration names that are safe to merge.

src/action/common/place_nix_configuration.rs

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use url::Url;
44

55
use crate::action::base::create_or_merge_nix_config::{
66
CreateOrMergeNixConfigError, EXPERIMENTAL_FEATURES_CONF_NAME,
7-
EXTRA_EXPERIMENTAL_FEATURES_CONF_NAME,
7+
EXTRA_EXPERIMENTAL_FEATURES_CONF_NAME, TRUSTED_USERS_CONF_NAME,
88
};
99
use crate::action::base::{CreateDirectory, CreateOrMergeNixConfig};
1010
use crate::action::{
@@ -49,14 +49,18 @@ impl PlaceNixConfiguration {
4949
force: bool,
5050
determinate_nix: bool,
5151
) -> Result<StatefulAction<Self>, ActionError> {
52+
let extra_conf = Self::parse_extra_conf(proxy, ssl_cert_file.as_ref(), extra_conf).await?;
53+
5254
let standard_nix_config = if !determinate_nix {
53-
Some(Self::setup_standard_config().await?)
55+
let maybe_trusted_users = extra_conf.settings().get(TRUSTED_USERS_CONF_NAME);
56+
57+
Some(Self::setup_standard_config(maybe_trusted_users).await?)
5458
} else {
5559
None
5660
};
5761

5862
let custom_nix_config =
59-
Self::setup_extra_config(nix_build_group_name, proxy, ssl_cert_file, extra_conf)
63+
Self::setup_extra_config(extra_conf, nix_build_group_name, ssl_cert_file.as_ref())
6064
.await?;
6165

6266
let create_directory = CreateDirectory::plan(NIX_CONF_FOLDER, None, None, 0o0755, force)
@@ -95,7 +99,9 @@ impl PlaceNixConfiguration {
9599
.into())
96100
}
97101

98-
async fn setup_standard_config() -> Result<nix_config_parser::NixConfig, ActionError> {
102+
async fn setup_standard_config(
103+
maybe_trusted_users: Option<&String>,
104+
) -> Result<nix_config_parser::NixConfig, ActionError> {
99105
let mut nix_config = nix_config_parser::NixConfig::new();
100106
let settings = nix_config.settings_mut();
101107

@@ -154,13 +160,32 @@ impl PlaceNixConfiguration {
154160
"https://install.determinate.systems/nix-upgrade/stable/universal".to_string(),
155161
);
156162

163+
// NOTE(cole-h): This is a workaround to hopefully unbreak users of Cachix.
164+
// When `cachix use`ing a cache, the Cachix CLI will sanity-check the system configuration
165+
// at `/etc/nix/nix.conf` to ensure that the user doing this will actually be able to
166+
// configure trusted settings (such as `trusted-public-keys`).
167+
// However, because we now write the `--extra-conf` into the `nix.custom.conf` (which is how
168+
// users, including our first-party DeterminateSystems/nix-installer-action, would configure
169+
// the `trusted-users` setting), and Cachix does not currently handle `include`s
170+
// properly[1][2], Cachix bails out thinking that the user is not a trusted user[3] even
171+
// though it is (it's just configured in another file).
172+
//
173+
// [1]: https://github.com/cachix/cachix/issues/680
174+
// [2]: https://github.com/cachix/cachix/pull/681
175+
// [3]: https://github.com/DeterminateSystems/nix-installer/issues/1389
176+
if let Some(trusted_users) = maybe_trusted_users {
177+
settings.insert(
178+
TRUSTED_USERS_CONF_NAME.to_string(),
179+
trusted_users.to_owned(),
180+
);
181+
}
182+
157183
Ok(nix_config)
158184
}
159185

160-
async fn setup_extra_config(
161-
nix_build_group_name: String,
186+
async fn parse_extra_conf(
162187
proxy: Option<Url>,
163-
ssl_cert_file: Option<PathBuf>,
188+
ssl_cert_file: Option<&PathBuf>,
164189
extra_conf: Vec<UrlOrPathOrString>,
165190
) -> Result<nix_config_parser::NixConfig, ActionError> {
166191
let mut extra_conf_text = vec![];
@@ -216,11 +241,19 @@ impl PlaceNixConfiguration {
216241
}
217242

218243
let extra_conf = extra_conf_text.join("\n");
219-
let mut nix_config = nix_config_parser::NixConfig::parse_string(extra_conf, None)
244+
let nix_config = nix_config_parser::NixConfig::parse_string(extra_conf, None)
220245
.map_err(CreateOrMergeNixConfigError::ParseNixConfig)
221246
.map_err(Self::error)?;
222247

223-
let settings = nix_config.settings_mut();
248+
Ok(nix_config)
249+
}
250+
251+
async fn setup_extra_config(
252+
mut extra_conf: nix_config_parser::NixConfig,
253+
nix_build_group_name: String,
254+
ssl_cert_file: Option<&PathBuf>,
255+
) -> Result<nix_config_parser::NixConfig, ActionError> {
256+
let settings = extra_conf.settings_mut();
224257

225258
if nix_build_group_name != crate::settings::DEFAULT_NIX_BUILD_USER_GROUP_NAME {
226259
settings.insert("build-users-group".to_string(), nix_build_group_name);
@@ -256,7 +289,7 @@ impl PlaceNixConfiguration {
256289
indexmap::map::Entry::Vacant(_) => {},
257290
}
258291

259-
Ok(nix_config)
292+
Ok(extra_conf)
260293
}
261294
}
262295

@@ -379,8 +412,7 @@ mod tests {
379412

380413
#[tokio::test]
381414
async fn extra_trusted_no_error() -> eyre::Result<()> {
382-
let nix_config = PlaceNixConfiguration::setup_extra_config(
383-
String::from("foo"),
415+
let extra_conf = PlaceNixConfiguration::parse_extra_conf(
384416
None,
385417
None,
386418
vec![
@@ -390,6 +422,10 @@ mod tests {
390422
)
391423
.await?;
392424

425+
let nix_config =
426+
PlaceNixConfiguration::setup_extra_config(extra_conf, String::from("foo"), None)
427+
.await?;
428+
393429
assert!(
394430
nix_config
395431
.settings()

0 commit comments

Comments
 (0)