Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 45 additions & 24 deletions nixos/modules/system/activation/nixos-init.nix
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,49 @@ in
package = lib.mkPackageOption pkgs "nixos-init" { };
};

config = lib.mkIf cfg.enable {
assertions = [
{
assertion = config.boot.initrd.systemd.enable;
message = "nixos-init can only be used with boot.initrd.systemd.enable";
}
{
assertion = config.system.etc.overlay.enable;
message = "nixos-init can only be used with system.etc.overlay.enable";
}
{
assertion = config.services.userborn.enable || config.systemd.sysusers.enable;
message = "nixos-init can only be used with services.userborn.enable or systemd.sysusers.enable";
}
{
assertion = config.boot.postBootCommands == "";
message = "nixos-init cannot be used with boot.postBootCommands";
}
{
assertion = config.powerManagement.powerUpCommands == "";
message = "nixos-init cannot be used with powerManagement.powerUpCommands";
}
];
};
config = lib.mkMerge [
{
boot.bootspec.extensions = {
"org.nixos.nixos-init.v1" = {
firmware = "${config.hardware.firmware}/lib/firmware";
modprobe_binary = "${pkgs.kmod}/bin/modprobe";
nix_store_mount_opts = config.boot.nixStoreMountOpts;
}
// lib.optionalAttrs (config.environment.binsh != null) {
sh_binary = config.environment.binsh;
}
// lib.optionalAttrs (config.environment.usrbinenv != null) {
env_binary = config.environment.usrbinenv;
}
// lib.optionalAttrs config.system.etc.overlay.enable {
etc_metadata_image = config.system.build.etcMetadataImage;
etc_basedir = config.system.build.etcBasedir;
};
};
}
(lib.mkIf cfg.enable {
assertions = [
{
assertion = config.boot.initrd.systemd.enable;
message = "nixos-init can only be used with boot.initrd.systemd.enable";
}
{
assertion = config.system.etc.overlay.enable;
message = "nixos-init can only be used with system.etc.overlay.enable";
}
{
assertion = config.services.userborn.enable || config.systemd.sysusers.enable;
message = "nixos-init can only be used with services.userborn.enable or systemd.sysusers.enable";
}
{
assertion = config.boot.postBootCommands == "";
message = "nixos-init cannot be used with boot.postBootCommands";
}
{
assertion = config.powerManagement.powerUpCommands == "";
message = "nixos-init cannot be used with powerManagement.powerUpCommands";
}
];
})
];
}
5 changes: 0 additions & 5 deletions nixos/modules/system/activation/top-level.nix
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ let

ln -s ${config.system.build.etc}/etc $out/etc

${lib.optionalString config.system.etc.overlay.enable ''
ln -s ${config.system.build.etcMetadataImage} $out/etc-metadata-image
ln -s ${config.system.build.etcBasedir} $out/etc-basedir
''}

ln -s ${config.system.path} $out/sw
ln -s "$systemd" $out/systemd

Expand Down
11 changes: 0 additions & 11 deletions nixos/modules/system/boot/systemd/initrd.nix
Original file line number Diff line number Diff line change
Expand Up @@ -733,17 +733,6 @@ in
cfg.package.util-linux
config.system.nixos-init.package
];
environment = {
FIRMWARE = "${config.hardware.firmware}/lib/firmware";
MODPROBE_BINARY = "${pkgs.kmod}/bin/modprobe";
NIX_STORE_MOUNT_OPTS = lib.concatStringsSep "," config.boot.nixStoreMountOpts;
}
// lib.optionalAttrs (config.environment.usrbinenv != null) {
ENV_BINARY = config.environment.usrbinenv;
}
// lib.optionalAttrs (config.environment.binsh != null) {
SH_BINARY = config.environment.binsh;
};
serviceConfig = {
ExecStart = [
""
Expand Down
130 changes: 130 additions & 0 deletions pkgs/by-name/ni/nixos-init/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions pkgs/by-name/ni/nixos-init/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ edition = "2024"
anyhow = "1.0.98"
log = "0.4.27"
env_logger = { version = "0.11.8", default-features = false }
serde = { version = "1.0.228", features = ["derive"] }
serde_json = "1.0.145"
bootspec = "2.0.0"
Comment thread
r-vdp marked this conversation as resolved.

[dev-dependencies]
tempfile = "3.20.0"
Expand Down
19 changes: 11 additions & 8 deletions pkgs/by-name/ni/nixos-init/src/activate.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::{fs, path::Path};
use std::{
fs,
path::{Path, PathBuf},
};

use anyhow::{Context, Result};

Expand All @@ -9,7 +12,8 @@ use crate::{config::Config, fs::atomic_symlink};
/// This runs both during boot and during re-activation initiated by switch-to-configuration.
pub fn activate(prefix: &str, toplevel: impl AsRef<Path>, config: &Config) -> Result<()> {
log::info!("Setting up /run/current-system...");
atomic_symlink(&toplevel, format!("{prefix}/run/current-system"))?;
let system_path = PathBuf::from(prefix).join("run/current-system");
atomic_symlink(&toplevel, system_path)?;

log::info!("Setting up modprobe...");
setup_modprobe(&config.modprobe_binary)?;
Expand Down Expand Up @@ -92,17 +96,16 @@ fn setup_firmware_search_path(firmware: impl AsRef<Path>) -> Result<()> {
///
/// We do this here accidentally. `/usr/bin/env` is currently load-bearing for `NixOS`.
fn setup_usrbinenv(prefix: &str, env_binary: impl AsRef<Path>) -> Result<()> {
const USRBINENV_PATH: &str = "/usr/bin/env";

fs::create_dir_all(format!("{prefix}/usr/bin")).context("Failed to create /usr/bin")?;
atomic_symlink(&env_binary, format!("{prefix}{USRBINENV_PATH}"))
let usrbin_path = PathBuf::from(prefix).join("usr/bin");
fs::create_dir_all(&usrbin_path).context("Failed to create /usr/bin")?;
atomic_symlink(&env_binary, usrbin_path.join("env"))
}

/// Setup /bin/sh.
///
/// `/bin/sh` is an essential part of a Linux system as this path is hardcoded in the `system()` call
/// from libc. See `man systemd(3)`.
fn setup_binsh(prefix: &str, sh_binary: impl AsRef<Path>) -> Result<()> {
const BINSH_PATH: &str = "/bin/sh";
atomic_symlink(&sh_binary, format!("{prefix}{BINSH_PATH}"))
let binsh_path = PathBuf::from(prefix).join("bin/sh");
atomic_symlink(&sh_binary, binsh_path)
}
Loading
Loading