Unverified Commit e7fa9ff1 authored by misuzu's avatar misuzu Committed by GitHub
Browse files

nixos-init: init at 0.1.0 (#433154)

parents 958326b9 904a96c6
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@
  and newer series. However, embedded chips without LSX (Loongson SIMD eXtension), such as 2K0300 SoC, are not
  supported. `pkgsCross.loongarch64-linux-embedded` can be used to build software and systems for these platforms.
- The official Nix formatter `nixfmt` is now stable and available as `pkgs.nixfmt`, deprecating the temporary `pkgs.nixfmt-rfc-style` attribute. The classic `nixfmt` will stay available for some more time as `pkgs.nixfmt-classic`.
- Added `nixos-init`, a Rust-based bashless initialization system for systemd initrd. This allows to build NixOS systems without any interpreter. Enable via `system.nixos-init.enable = true;`.

## Backward Incompatibilities {#sec-nixpkgs-release-25.11-incompatibilities}

+1 −0
Original line number Diff line number Diff line
@@ -1803,6 +1803,7 @@
  ./system/activation/activatable-system.nix
  ./system/activation/activation-script.nix
  ./system/activation/bootspec.nix
  ./system/activation/nixos-init.nix
  ./system/activation/pre-switch-check.nix
  ./system/activation/specialisation.nix
  ./system/activation/switchable-system.nix
+31 −0
Original line number Diff line number Diff line
{
  config,
  lib,
  pkgs,
  ...
}:

let
  cfg = config.system.nixos-init;
in
{
  options.system.nixos-init = {
    enable = lib.mkEnableOption ''
      nixos-init, a system for bashless initialization.

      This doesn't use any `activationScripts`. Anything set in these options is
      a no-op here.
    '';

    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 systemd initrd";
      }
    ];
  };
}
+5 −2
Original line number Diff line number Diff line
@@ -14,12 +14,15 @@ let
    ${
      if config.boot.initrd.enable && config.boot.initrd.systemd.enable then
        ''
          cp ${config.system.build.bootStage2} $out/prepare-root
          substituteInPlace $out/prepare-root --subst-var-by systemConfig $out
          # This must not be a symlink or the abs_path of the grub builder for the tests
          # will resolve the symlink and we end up with a path that doesn't point to a
          # system closure.
          cp "$systemd/lib/systemd/systemd" $out/init

          ${lib.optionalString (!config.system.nixos-init.enable) ''
            cp ${config.system.build.bootStage2} $out/prepare-root
            substituteInPlace $out/prepare-root --subst-var-by systemConfig $out
          ''}
        ''
      else
        ''
+54 −15
Original line number Diff line number Diff line
@@ -562,7 +562,12 @@ in
        "${pkgs.glibc}/lib/libnss_files.so.2"

        # Resolving sysroot symlinks without code exec
        "${pkgs.chroot-realpath}/bin/chroot-realpath"
        "${config.system.nixos-init.package}/bin/chroot-realpath"
        # Find the etc paths
        "${config.system.nixos-init.package}/bin/find-etc"
      ]
      ++ lib.optionals config.system.nixos-init.enable [
        "${config.system.nixos-init.package}/bin/initrd-init"
      ]
      ++ jobScripts
      ++ map (c: builtins.removeAttrs c [ "text" ]) (builtins.attrValues cfg.contents);
@@ -594,7 +599,7 @@ in
          ) cfg.automounts
        );

      services.initrd-find-nixos-closure = {
      services.initrd-find-nixos-closure = lib.mkIf (!config.system.nixos-init.enable) {
        description = "Find NixOS closure";

        unitConfig = {
@@ -615,7 +620,12 @@ in
        script = # bash
          ''
            set -uo pipefail
            export PATH="/bin:${cfg.package.util-linux}/bin:${pkgs.chroot-realpath}/bin"
            export PATH="/bin:${
              lib.makeBinPath [
                cfg.package.util-linux
                config.system.nixos-init.package
              ]
            }"

            # Figure out what closure to boot
            closure=
@@ -670,7 +680,7 @@ in
        }
      ];

      services.initrd-nixos-activation = {
      services.initrd-nixos-activation = lib.mkIf (!config.system.nixos-init.enable) {
        after = [ "initrd-switch-root.target" ];
        requiredBy = [ "initrd-switch-root.service" ];
        before = [ "initrd-switch-root.service" ];
@@ -697,17 +707,46 @@ in
          '';
      };

      services.initrd-switch-root =
        if config.system.nixos-init.enable then
          {
            path = [
              cfg.package
              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 = [
                ""
                "${config.system.nixos-init.package}/bin/initrd-init"
              ];
            };
          }
        else
          # This will either call systemctl with the new init as the last parameter (which
          # is the case when not booting a NixOS system) or with an empty string, causing
          # systemd to bypass its verification code that checks whether the next file is a systemd
          # and using its compiled-in value
      services.initrd-switch-root.serviceConfig = {
          {
            serviceConfig = {
              EnvironmentFile = "-/etc/switch-root.conf";
              ExecStart = [
                ""
                ''systemctl --no-block switch-root /sysroot "''${NEW_INIT}"''
              ];
            };
          };

      services.panic-on-fail = {
        wantedBy = [ "emergency.target" ];
Loading