Default to systemd, refer to documentation if systemd is not available (#336)

This commit is contained in:
Cole Helbling 2023-03-13 14:12:33 -07:00 committed by GitHub
parent 8e27adcf98
commit 96d8870902
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 17 deletions

View file

@ -147,6 +147,21 @@ jobs:
run: nix build .
```
## Without systemd (Linux only)
> **Warning**
> When installed this way, _only_ `root` or users who can elevate to `root` privileges can run Nix:
>
> ```bash
> sudo -i nix run nixpkgs#hello
> ```
If you don't use [systemd], you can still install Nix by explicitly specifying the `linux` plan and `--init none`:
```bash
curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install linux --init none
```
## In a container
In Docker/Podman containers or WSL2 instances where an init (like `systemd`) is not present, pass `--init none`.

View file

@ -75,6 +75,13 @@ impl ConfigureInitService {
},
#[cfg(target_os = "linux")]
InitSystem::Systemd => {
// If /run/systemd/system exists, we can be reasonably sure the machine is booted
// with systemd: https://www.freedesktop.org/software/systemd/man/sd_booted.html
if !(Path::new("/run/systemd/system").exists() || which::which("systemctl").is_ok())
{
return Err(ActionError::SystemdMissing);
}
Self::check_if_systemd_unit_exists(SERVICE_SRC, SERVICE_DEST).await?;
Self::check_if_systemd_unit_exists(SOCKET_SRC, SOCKET_DEST).await?;
},

View file

@ -462,6 +462,11 @@ pub enum ActionError {
MissingGroupDeletionCommand,
#[error("Could not find a supported command to remove users from groups in PATH; please install `gpasswd` or `deluser`")]
MissingRemoveUserFromGroupCommand,
#[error("\
Could not detect systemd; you may be able to get up and running without systemd with `nix-installer install linux --init none`.\n\
See https://github.com/DeterminateSystems/nix-installer#without-systemd-linux-only for documentation on usage and drawbacks.\
")]
SystemdMissing,
}
impl ActionError {

View file

@ -339,13 +339,11 @@ impl CommonSettings {
}
}
#[cfg(target_os = "linux")]
async fn linux_detect_init() -> (InitSystem, bool) {
async fn linux_detect_systemd_started() -> bool {
use std::process::Stdio;
let mut detected = InitSystem::None;
let mut started = false;
if std::path::Path::new("/run/systemd/system").exists() {
detected = InitSystem::Systemd;
started = if tokio::process::Command::new("systemctl")
.arg("status")
.stdin(Stdio::null())
@ -364,7 +362,7 @@ async fn linux_detect_init() -> (InitSystem, bool) {
}
// TODO: Other inits
(detected, started)
started
}
// Builder Pattern
@ -464,33 +462,26 @@ pub struct InitSettings {
impl InitSettings {
/// The default settings for the given Architecture & Operating System
pub async fn default() -> Result<Self, InstallSettingsError> {
let init;
let start_daemon;
use target_lexicon::{Architecture, OperatingSystem};
match (Architecture::host(), OperatingSystem::host()) {
let (init, start_daemon) = match (Architecture::host(), OperatingSystem::host()) {
#[cfg(target_os = "linux")]
(Architecture::X86_64, OperatingSystem::Linux) => {
(init, start_daemon) = linux_detect_init().await;
(InitSystem::Systemd, linux_detect_systemd_started().await)
},
#[cfg(target_os = "linux")]
(Architecture::X86_32(_), OperatingSystem::Linux) => {
(init, start_daemon) = linux_detect_init().await;
(InitSystem::Systemd, linux_detect_systemd_started().await)
},
#[cfg(target_os = "linux")]
(Architecture::Aarch64(_), OperatingSystem::Linux) => {
(init, start_daemon) = linux_detect_init().await;
(InitSystem::Systemd, linux_detect_systemd_started().await)
},
#[cfg(target_os = "macos")]
(Architecture::X86_64, OperatingSystem::MacOSX { .. })
| (Architecture::X86_64, OperatingSystem::Darwin) => {
(init, start_daemon) = (InitSystem::Launchd, true);
},
| (Architecture::X86_64, OperatingSystem::Darwin) => (InitSystem::Launchd, true),
#[cfg(target_os = "macos")]
(Architecture::Aarch64(_), OperatingSystem::MacOSX { .. })
| (Architecture::Aarch64(_), OperatingSystem::Darwin) => {
(init, start_daemon) = (InitSystem::Launchd, true);
},
| (Architecture::Aarch64(_), OperatingSystem::Darwin) => (InitSystem::Launchd, true),
_ => {
return Err(InstallSettingsError::UnsupportedArchitecture(
target_lexicon::HOST,