Skip to content

Commit

Permalink
Default to systemd, refer to documentation if systemd is not available (
Browse files Browse the repository at this point in the history
  • Loading branch information
cole-h authored Mar 13, 2023
1 parent 8e27adc commit 96d8870
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 17 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
7 changes: 7 additions & 0 deletions src/action/common/configure_init_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?;
},
Expand Down
5 changes: 5 additions & 0 deletions src/action/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
25 changes: 8 additions & 17 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -364,7 +362,7 @@ async fn linux_detect_init() -> (InitSystem, bool) {
}

// TODO: Other inits
(detected, started)
started
}

// Builder Pattern
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 96d8870

Please sign in to comment.