Compare commits

...

3 commits

Author SHA1 Message Date
Kate Temkin 7c36a83a86 config: prompt user for flakes 2024-05-05 16:48:16 -06:00
Kate Temkin 83960c7814 fix: provide a convenient message if TERM isn't set 2024-05-05 16:46:41 -06:00
Kate Temkin 9038c2e7b8 cargo: bump the version of nix-rust to avoid issues 2024-05-05 16:45:09 -06:00
12 changed files with 417 additions and 286 deletions

558
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -29,7 +29,7 @@ clap = { version = "4", features = ["std", "color", "usage", "help", "error-cont
color-eyre = { version = "0.6.2", default-features = false, features = [ "track-caller", "issue-url", "tracing-error", "capture-spantrace", "color-spantrace" ], optional = true }
eyre = { version = "0.6.8", default-features = false, features = [ "track-caller" ], optional = true }
glob = { version = "0.3.0", default-features = false }
nix = { version = "0.27.0", default-features = false, features = ["user", "fs", "process", "term"] }
nix = { version = "0.28.0", default-features = false, features = ["user", "fs", "process", "term"] }
owo-colors = { version = "4.0.0", default-features = false, features = [ "supports-colors" ] }
reqwest = { version = "0.11.11", default-features = false, features = ["rustls-tls-native-roots", "stream", "socks"] }
serde = { version = "1.0.144", default-features = false, features = [ "std", "derive" ] }

View file

@ -8,16 +8,17 @@
"rust-analyzer-src": "rust-analyzer-src"
},
"locked": {
"lastModified": 1706768574,
"narHash": "sha256-4o6TMpzBHO659EiJTzd/EGQGUDdbgwKwhqf3u6b23U8=",
"rev": "668102037129923cd0fc239d864fce71eabdc6a3",
"revCount": 1762,
"type": "tarball",
"url": "https://api.flakehub.com/f/pinned/nix-community/fenix/0.1.1762%2Brev-668102037129923cd0fc239d864fce71eabdc6a3/018d63bb-6455-7a2f-98c6-74a36b8216a4/source.tar.gz"
"lastModified": 1714890282,
"narHash": "sha256-0dRK2ChvkhWrLM6H3d4r+rXP/UDxTJ6Vkdr22uGb1H0=",
"owner": "nix-community",
"repo": "fenix",
"rev": "24d83329e95a3bc48cbe9f3cd23813c210a25ea6",
"type": "github"
},
"original": {
"type": "tarball",
"url": "https://flakehub.com/f/nix-community/fenix/0.1.1584.tar.gz"
"owner": "nix-community",
"repo": "fenix",
"type": "github"
}
},
"flake-compat": {
@ -140,16 +141,18 @@
},
"nixpkgs_2": {
"locked": {
"lastModified": 1704538339,
"narHash": "sha256-1734d3mQuux9ySvwf6axRWZRBhtcZA9Q8eftD6EZg6U=",
"rev": "46ae0210ce163b3cba6c7da08840c1d63de9c701",
"revCount": 567011,
"type": "tarball",
"url": "https://api.flakehub.com/f/pinned/NixOS/nixpkgs/0.1.567011%2Brev-46ae0210ce163b3cba6c7da08840c1d63de9c701/018ce71d-40cb-7594-bc00-f31ffedcdfa4/source.tar.gz"
"lastModified": 1714763106,
"narHash": "sha256-DrDHo74uTycfpAF+/qxZAMlP/Cpe04BVioJb6fdI0YY=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "e9be42459999a253a9f92559b1f5b72e1b44c13d",
"type": "github"
},
"original": {
"type": "tarball",
"url": "https://flakehub.com/f/NixOS/nixpkgs/0.1.0.tar.gz"
"owner": "NixOS",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {

View file

@ -37,7 +37,7 @@ impl CreateGroup {
},
}
// Ensure group does not exists
// Ensure group does not exist
if let Some(group) = Group::from_name(name.as_str())
.map_err(|e| ActionErrorKind::GettingGroupId(name.clone(), e))
.map_err(Self::error)?

View file

@ -17,7 +17,7 @@ use crate::action::{
/// The `nix.conf` configuration names that are safe to merge.
// FIXME(@cole-h): make configurable by downstream users?
const MERGEABLE_CONF_NAMES: &[&str] = &["experimental-features"];
const MERGEABLE_CONF_NAMES: &[&str] = &["experimental-features", "substituters", "trusted-public-keys"];
const NIX_CONF_MODE: u32 = 0o664;
const NIX_CONF_COMMENT_CHAR: char = '#';

View file

@ -46,6 +46,7 @@ impl ConfigureNix {
settings.proxy.clone(),
settings.ssl_cert_file.clone(),
settings.extra_conf.clone(),
settings.enable_flakes,
settings.force,
)
.await

View file

@ -30,6 +30,7 @@ impl PlaceNixConfiguration {
proxy: Option<Url>,
ssl_cert_file: Option<PathBuf>,
extra_conf: Vec<UrlOrPathOrString>,
enable_flakes: bool,
force: bool,
) -> Result<StatefulAction<Self>, ActionError> {
let mut extra_conf_text = vec![];
@ -91,7 +92,13 @@ impl PlaceNixConfiguration {
let settings = nix_config.settings_mut();
settings.insert("build-users-group".to_string(), nix_build_group_name);
let experimental_features = ["nix-command", "flakes", "repl-flake"];
let mut experimental_features = vec!["nix-command"];
// Enable flakes if desired.
if enable_flakes {
experimental_features.push("flakes");
}
match settings.entry("experimental-features".to_string()) {
Entry::Occupied(mut slot) => {
let slot_mut = slot.get_mut();
@ -125,10 +132,24 @@ impl PlaceNixConfiguration {
ssl_cert_file_canonical.display().to_string(),
);
}
// Set up our substituters.
settings.insert(
"substituters".to_string(),
"https://cache.nixos.org https://cache.lix.systems".to_string(),
);
settings.insert(
"trusted-public-keys".to_string(),
"cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY= cache.lix.systems:aBnZUw8zA7H35Cz2RyKFVs3H4PlGTLawyY5KRbvJR8o=".to_string()
);
if enable_flakes {
settings.insert(
"extra-nix-path".to_string(),
"nixpkgs=flake:nixpkgs".to_string(),
);
}
let create_directory = CreateDirectory::plan(NIX_CONF_FOLDER, None, None, 0o0755, force)
.await
.map_err(Self::error)?;

View file

@ -16,6 +16,7 @@ pub enum PromptChoice {
// The below method was adopted from Rustup at https://github.com/rust-lang/rustup/blob/3331f34c01474bf216c99a1b1706725708833de1/src/cli/term2.rs#L37
pub(crate) async fn prompt(
question: impl AsRef<str>,
prompt_text: impl AsRef<str>,
default: PromptChoice,
currently_explaining: bool,
) -> eyre::Result<PromptChoice> {
@ -29,7 +30,7 @@ pub(crate) async fn prompt(
{are_you_sure} ({yes}/{no}{maybe_explain}): \
",
question = question.as_ref(),
are_you_sure = "Proceed?".bold(),
are_you_sure = prompt_text.as_ref().bold(),
no = if default == PromptChoice::No {
"[N]o"
} else {

View file

@ -92,7 +92,7 @@ pub fn ensure_root() -> eyre::Result<()> {
if !is_root() {
eprintln!(
"{}",
"`nix-installer` needs to run as `root`, attempting to escalate now via `sudo`..."
"`lix-installer` needs to run as `root`, attempting to escalate now via `sudo`..."
.yellow()
.dimmed()
);

View file

@ -1,4 +1,5 @@
use std::{
io::{stdout, Write},
os::unix::prelude::PermissionsExt,
path::{Path, PathBuf},
process::ExitCode,
@ -80,6 +81,22 @@ impl CommandExecute for Install {
explain,
} = self;
// Get our terminal object, explaining what to do if it's not there.
let term =
term::terminfo::TerminfoTerminal::new(stdout());
if term.is_none() && !no_confirm {
eprintln!(
"{}",
format!("\
\n\
Couldn't figure out which terminal you're using -- check the value of the $TERM variable.\n\n\
If you're using an interactive terminal, it's probably safe to set TERM to \"xterm\",\n\
by e.g. running 'export TERM=xterm'.\n\
").red()
);
interaction::clean_exit_with_message("Couldn't get the terminal! Aborting.").await;
}
ensure_root()?;
let existing_receipt: Option<InstallPlan> = match Path::new(RECEIPT_LOCATION).exists() {
@ -153,7 +170,39 @@ impl CommandExecute for Install {
serde_json::from_str(&install_plan_string)?
},
(None, None) => {
let builtin_planner = BuiltinPlanner::from_common_settings(settings.clone())
let mut settings_to_apply = settings.clone();
if !no_confirm {
// Say hello.
let mut term = term.expect("Internal consistency: term should have been None checked already!");
let hello_message = format!("{}{}", "\n\nWelcome to the Lix installer!".bold(), " Just a couple of quick questions.\n\n");
term.write_all(hello_message.as_bytes())?;
term.flush()?;
// Ask about flakes.
match interaction::prompt(
"Flakes are an experimental feature, but widely used in the community.\nYou can change this later in `/etc/nix/nix.conf`.",
"Enable flakes?",
PromptChoice::Yes,
true,
)
.await?
{
PromptChoice::Yes => settings_to_apply.enable_flakes = true,
PromptChoice::Explain => panic!("This prompt has no explanation."),
PromptChoice::No => settings_to_apply.enable_flakes = false,
}
// Notify the user about the nix command.
let nixcmd_message = format!("{}{}{}{}{}",
"\nQUICK NOTE:".bold().yellow(), " we've enabled the experimental", " nix ".bold(), "command for you!\n",
"Be aware that commands starting with `nix ` such as `nix build` may change syntax.\n\n".green());
term.write_all(nixcmd_message.as_bytes())?;
term.flush()?;
}
let builtin_planner = BuiltinPlanner::from_common_settings(settings_to_apply)
.await
.map_err(|e| eyre::eyre!(e))?;
@ -219,6 +268,7 @@ impl CommandExecute for Install {
.describe_install(currently_explaining)
.await
.map_err(|e| eyre!(e))?,
"Proceed?",
PromptChoice::Yes,
currently_explaining,
)
@ -259,6 +309,7 @@ impl CommandExecute for Install {
.describe_uninstall(currently_explaining)
.await
.map_err(|e| eyre!(e))?,
"Proceed?",
PromptChoice::Yes,
currently_explaining,
)

View file

@ -169,6 +169,7 @@ impl CommandExecute for Uninstall {
plan.describe_uninstall(currently_explaining)
.await
.map_err(|e| eyre!(e))?,
"Proceed?",
PromptChoice::Yes,
currently_explaining,
)

View file

@ -203,6 +203,20 @@ pub struct CommonSettings {
)
)]
pub force: bool,
/// If `nix-installer` should enable flakes.
#[cfg_attr(
feature = "cli",
clap(
long,
action(ArgAction::SetFalse),
default_value = "true",
global = true,
env = "NIX_INSTALLER_ENABLE_FLAKES"
)
)]
pub enable_flakes: bool,
}
impl CommonSettings {
@ -270,6 +284,7 @@ impl CommonSettings {
proxy: Default::default(),
extra_conf: Default::default(),
force: false,
enable_flakes: true,
ssl_cert_file: Default::default(),
})
}
@ -287,6 +302,7 @@ impl CommonSettings {
proxy,
extra_conf,
force,
enable_flakes,
ssl_cert_file,
} = self;
let mut map = HashMap::default();
@ -322,6 +338,7 @@ impl CommonSettings {
map.insert("proxy".into(), serde_json::to_value(proxy)?);
map.insert("ssl_cert_file".into(), serde_json::to_value(ssl_cert_file)?);
map.insert("extra_conf".into(), serde_json::to_value(extra_conf)?);
map.insert("enable_flakes".into(), serde_json::to_value(enable_flakes)?);
map.insert("force".into(), serde_json::to_value(force)?);
Ok(map)
}