Commit Graph

187 Commits

Author SHA1 Message Date
Jared Baur
e21b5dd8b9 Revert "nixos/systemd-initrd: allow /nixos-closure to always exist"
This reverts commit b3c789f621.
2026-09-18 21:15:14 -07:00
Jared Baur
b3c789f621 nixos/systemd-initrd: allow /nixos-closure to always exist
The symlink at /nixos-closure that is used by
initrd-nixos-activation is created by
initrd-find-nixos-closure, but there is no relationship declared
between the two services. In some environments, I've encountered issues
where systemd schedules initrd-nixos-activation before
initrd-find-nixos-closure, resulting in a nixos system that never leaves
the initrd. This change ensures that initrd-nixos-activation only runs
after /nixos-closure is populated.
2026-08-14 18:11:11 -07:00
Antoine Cotten
d6e843ab30 nixos/systemd-initrd: disambiguate doc for root="gpt-auto" 2026-08-03 11:56:07 +02:00
Vladimír Čunát
105327541b Reapply "staging-nixos merge for 2026-06-19" (#534864)
This reverts commit 532f984da0, reversing
changes made to 421ceaeef8.
2026-06-24 12:17:06 +02:00
Vladimír Čunát
a6c3b1d396 Merge master into staging-nixos 2026-06-24 12:16:37 +02:00
Vladimír Čunát
6e628a4f79 Revert "staging-nixos merge for 2026-06-19" 2026-06-24 10:55:53 +02:00
Will Fancher
d54bf2afa2 nixos/systemd-initrd: add systemd.*.services.path to initrd store (#462515) 2026-06-20 03:42:37 +00:00
Oskar Philipsson
2e23af70a8 nixos/systemd-initrd: add systemd.*.services.path to initrd store 2026-06-16 01:15:07 +02:00
Ilan Joselevich
feaf19ea26 nixos/systemd-initrd: skip activation when init= is not a NixOS system
initrd-nixos-activation ran the closure's prepare-root unconditionally. For a
non-NixOS init= there is no prepare-root, so the service failed, and since
initrd-switch-root.service requires it, switch-root never ran and the machine
dropped to the emergency shell. This broke init=/bin/sh recovery, and microVMs
that serve /nix/store over virtiofs and boot an arbitrary binary as init=.

initrd-find-nixos-closure already detects this and writes a non-empty NEW_INIT
to /etc/switch-root.conf (empty for a NixOS init). Read it via the same
EnvironmentFile= initrd-switch-root uses and skip activation when it's set, so
a non-NixOS init= switch-roots into its target directly. The NixOS path is
unchanged.

Also add a test booting a non-NixOS init=. It uses a store path rather than
/bin/sh: a real root already has /bin/sh and an os-release, but a fresh test
root has neither, so the test uses a tmpfs root and writes os-release first.

Assisted-by: Claude:claude-opus-4-8
2026-06-15 19:28:19 +03:00
Will Fancher
983d84d401 nixos/systemd/initrd: Check MODULES accurately
`config.system.build.kernel.config` is not actually accurate. When a
kconfig is specified in the `config` argument to `kernel/build.nix`
(a.k.a. `manualConfig` or `linuxManualConfig`), then `isSet` will
return `true` and other queries like `isYes` will be accurate. But if
a kconfig is not specified in that `config` argument, then `isSet`
will return `false` and other queries will be inaccurate, e.g. `isYes`
"MODULES"` can return `false` even though your `configfile` has it
enabled.

Note the difference between the `config` argument and the `configfile`
argument. The `configfile` is how the kernel will be actually built,
while `config` is merely passed through as a source of eval-time
information. Importantly, neither is derived from the other *in any
way*, unless `builtins.isPath configfile || allowImportFromDerivation`
in which case the default value for `config` is derived from reading
`configfile`.

The more generic `kernel/generic.nix` (a.k.a. `buildLinux`) creates
its `configfile` in a derivation, so it cannot be read at eval time by
default. It calls into `kernel/build.nix`, and only passes a `config`
with `CONFIG_MODULES`, `CONFIG_FW_LOADER`, and `CONFIG_RUST` set. So
almost nothing in `kernel.config` is accurate in the typical
case. `MODULES` happens to be one of the three that *is* accurate
typically, but regardless we obviously can't rely on that since a user
of `kernel/build.nix` is likely to mess it up. Even worse,
`structuredExtraConfig` is not incorporated into `kernel.config` at
all, which leads to the incredibly confusing scenario where a kconfig
is specified in `structuredExtraConfig` but still is not represented
accurately by these queries.

All of this is why, in most cases, the implementation of
`requiredKernelConfig` deliberately does absolutely nothing and
creates an empty list of assertions, and it's all extremely confusing.

With all that in mind:

TODO:

- The structured config used to generate the `configfile` should be
  reflected in the `config` argument to `kernel/build.nix`, and
  consequently `kernel.config`.
- The three kconfigs represented by `config` in `kernel/generic.nix`
  now, `CONFIG_MODULES`, `CONFIG_FW_LOADER`, and `CONFIG_RUST`, should
  be set in the structured config.
- Queries for kconfigs that we don't actually know the value of at
  eval time should fail to evaluate, rather than evaluating
  inaccurately.
- Most of the ways we use these eval-time queries should instead be
  done at build time, so they can use the complete `configfile` rather
  than the incomplete eval-time `config` value.
- The ones that we still want to happen at eval-time should be more
  prepared for the possibility that we can't know the value of
  arbitrary kconfigs at eval time.

---

Anyway, all that is to say: I'd like for this all to be better, but am
not willing to work on the kernel expressions myself at the moment, so
I thought I'd write down the reasons why this change was necessary,
and the extent of the problem.

We had already successfully considered this issue in one place in
`systemd/initrd.nix`, but it seems there are two more places where we
should have taken the same care.
2026-04-23 00:33:19 -04:00
Will Fancher
61d18372d6 nixos/systemd/initrd: Remove with lib 2026-04-23 00:33:19 -04:00
K900
911507e404 staging-nixos merge for 2026-04-22 (#512449) 2026-04-22 16:08:39 +00:00
confusedalex
9880f9dd70 nixos-init: fix example of extraBin 2026-04-22 16:04:14 +02:00
nikstur
7fed7a400d nixos/systemd/initrd: fix modprobe
Contains the same fix as in d36077c0b6 but
now for the initrd.
2026-04-21 19:11:22 +02:00
Florian Klink
ce5163a619 nixos/systemd-initrd: support omitting kernel parameter root (#506753) 2026-04-13 09:20:53 +00:00
Will Fancher
5620d245ad nixos/systemd-stage-1: Enable by default 2026-04-07 21:53:09 -04:00
Will Fancher
2af908228d nixos/systemd-stage-1: Support rd.systemd.break= 2026-04-07 21:53:09 -04:00
Christian Kögler
e6909f5ac7 nixos/systemd-initrd: support omitting kernel parameter root 2026-04-04 22:57:44 +02:00
nikstur
83c336fb18 nixos-init: use libpathrs instead of chroot to resolve path in sysroot 2026-02-01 13:08:23 +01:00
r-vdp
b5bb5d22da nixos-init: read the required paths from a file in the toplevel instead of from the environment
This is a first step towards using nixos-init also during re-activation.
2026-01-26 11:54:07 +01:00
WilliButz
9bac056a72 nixos/systemd: expose factory-reset units 2026-01-21 17:23:14 +01:00
NAHO
a2ed7e8d88 nixos: remove optional builtins prefixes from prelude functions
Remove optional builtins prefixes from prelude functions by running:

    builtins=(
      abort
      baseNameOf
      break
      derivation
      derivationStrict
      dirOf
      false
      fetchGit
      fetchMercurial
      fetchTarball
      fetchTree
      fromTOML
      import
      isNull
      map
      null
      placeholder
      removeAttrs
      scopedImport
      throw
      toString
      true
    )

    fd \
      --exclude doc/manual/release-notes \
      --type file \
      . \
      nixos \
      --exec-batch sed --in-place --regexp-extended "
        s/\<builtins\.($(
          printf '%s\n' "${builtins[@]}" |
            paste --delimiter '|' --serial -
        ))\>/\1/g
      "

    nix fmt
2026-01-15 16:07:55 +01:00
K900
a905b1d8e1 nixos/systemd-initrd: fix eval under allowAliases = false 2025-10-20 11:19:41 +03:00
matthewcroughan
253a0165f0 nixos/systemd-initrd: only include glibc nss modules on glibc systems 2025-10-18 14:20:15 +01:00
nikstur
8002da18ab nixos/systemd-initrd: add boot.initrd.systemd.shell.enable option 2025-10-09 23:52:38 +02:00
K900
83dfcbb306 Merge remote-tracking branch 'origin/master' into staging-next 2025-09-23 21:15:13 +03:00
nikstur
3c24952c6d chroot-realpath: drop in favour of nixos-init 2025-09-23 00:21:34 +02:00
nikstur
d9e6299acc nixos/etc: use nixos-init to find etc 2025-09-23 00:21:33 +02:00
nikstur
d0c03e4c5a nixos-init: init at 0.1.0 2025-09-23 00:21:33 +02:00
Will Fancher
f1f9ee2028 nixos/systemd: introduce settingsToSections util, migrate existing rfc42 options to use settingsToSections, migrate oomd to rfc42 (#437477) 2025-09-12 03:41:59 +00:00
Philip Taron
54a65ab16f nixos/systemd-initrd: silence various warnings (#432704) 2025-08-28 15:53:59 -07:00
Grimmauld
51ac6e36ba nixos/systemd: write boot.initrd.systemd.settings using settingsToSections 2025-08-27 12:39:35 +02:00
andre4ik3
216d98ab97 nixos/systemd-initrd: silence various warnings
Silences 2 warning messages that appear when using the systemd initrd:

1. "System tainted (var-run-bad)": occurs because `/var/run` isn't a
   symlink to `/run`. Fixed by making /run and linking /var/run to it.

2. "Failed to make /usr a mountpoint": occurs because ProtectSystem
   defaults to true in the initrd, which makes systemd try to remount
   `/usr` as read-only, which doesn't exist in the initrd. Fixed by
   linking `/usr/bin` and `/usr/sbin` to the initrd bin directories.

Also moves the `/tmp` creation from the initrd module to make-initrd-ng,
to avoid making an unnecessary `/tmp/.keep`, saving a store path and a
few bytes in the initrd image.
2025-08-16 00:28:13 +00:00
Fiona Behrens
c8e3d79271 nixos/boot/kernel: allow kernel without MODULES support
Allow building a systemd initrd with a kernel that does not have
modules support enabled (`CONFIG_MODULES=n`), by removing the
assertion and only include the modulesClosure, kmod and support files
if MODULES is enabled or unset in the kernel.
2025-08-11 13:39:29 +00:00
Grimmauld
5bc2d42ba9 nixos/systemd: make boot.initrd.systemd.managerEnvironment affect boot.initrd.systemd.settings.Manager 2025-07-28 11:24:33 +02:00
Grimmauld
071ce0b44a nixos/systemd: remove boot.initrd.systemd.extraConfig 2025-07-28 11:24:29 +02:00
Grimmauld
f0b71eebdc nixos/systemd: add boot.initrd.systemd.settings.Manager option 2025-07-28 11:24:06 +02:00
Wolfgang Walther
5a0711127c treewide: run nixfmt 1.0.0 2025-07-24 13:55:40 +02:00
Bogdan-Cristian Tătăroiu
3332613add nixos/systemd-initrd: Fix fsck.xfs failing due to missing sh
When running with a xfs root partition and using systemd for stage 1
initrd, I noticed in journalctl that fsck.xfs always failed to execute.

The issue is that it is trying to use the below sh interpreter:

`#!/nix/store/xy4jjgw87sbgwylm5kn047d9gkbhsr9x-bash-5.2p37/bin/sh -f`

but the file does not exist in the initrd image.

/nix/store/xy4jjgw87sbgwylm5kn047d9gkbhsr9x-bash-5.2p37/bin/**bash**
exists since it gets pulled in by some package, but the rest of the
directory is not being pulled in.

boot/systemd/initrd.nix mentions that xfs_progs references the sh
interpreter and seems to explicitly try to address this by adding
${pkgs.bash}/bin to storePaths, but that's the wrong bash package.

Update the `storePaths` value to pull in `pkgs.bashNonInteractive`
rather than `pkgs.bash`.
2025-06-28 14:50:51 +01:00
nixpkgs-ci[bot]
bc2e6e0ae4 Merge master into staging-next 2025-05-29 18:05:35 +00:00
Florian Klink
9dd801dea7 nixos/systemd-initrd: honor the enable option in contents (#411800) 2025-05-29 18:06:12 +03:00
nixpkgs-ci[bot]
96ecef502b Merge master into staging-next 2025-05-29 12:06:54 +00:00
Fiona Behrens
99da5ec2bc nixos/systemd-initrd: honor the enable option in contents
The enable attribute of `boot.initrd.systemd.contents.<name>` was
ignored for building initrd storePaths. This resulted in building
derivations for the initrd even if it was disabled.

Found while testing a to build a nixos system with a kernel without
lodable modules[0]

[0]: https://github.com/NixOS/nixpkgs/pull/411792
2025-05-28 17:10:02 +00:00
Peter Marshall
7d36daa76a nixos/systemd-stage-1: follow systemd /run propagation
We currently bypass systemd's switch-root logic by premounting
/sysroot/run. Make sure to propagate its sub-mounts with the recursive
flag, in accordance with the default switch-root logic.

This is required for creds at /run/credentials to survive the transition
from initrd -> host.
2025-05-28 13:02:39 -04:00
nixpkgs-ci[bot]
cc7ffdded8 Merge master into staging-next 2025-05-26 18:05:25 +00:00
DavHau
d5d323a907 emergencyMode, emergencyAccess: cross reference options in docs.
I was confused why I could not get an emergency access console despite setting systemd.emergencyMode=true.

Turns out there is another similar option `boot.initrd.systemd.emergencyAccess` that I should have used.

This is confusing and this change should make it more clear vie the docs of both these options.
2025-05-24 14:01:22 +07:00
Winter
b7ea2eaa7d nixos/kernel: Module inclusion improvements (#375975) 2025-05-16 01:44:53 -04:00
Will Fancher
028b3e1411 nixos: Use common modulesClosure in scripted and systemd initrd 2025-05-15 00:00:56 -04:00
Gerg-L
98313e2b81 nixos/systemd-initrd: deprecate strip
It only saved ~1MiB of initramfs size, but caused a few issues
like unloadable kernel modules.
2025-05-08 19:08:14 -04:00
Will Fancher
b15e9e7d3d nixos/initrd: add extraFirmwarePaths option (#391166) 2025-03-19 16:51:26 -04:00