Emergency mode after updating, failed to mount /sysroot

Hi!

After updating my Nixos I get this error:

This is my hardware-configuration.nix and configuration.nix:

# Do not modify this file!  It was generated by ‘nixos-generate-config’
# and may be overwritten by future invocations.  Please make changes
# to /etc/nixos/configuration.nix instead.
{ config, lib, pkgs, modulesPath, ... }:

{
  imports =
    [ (modulesPath + "/installer/scan/not-detected.nix")
    ];

  boot.initrd.availableKernelModules = [ "vmd" "xhci_pci" "ahci" "nvme" ];
  boot.initrd.kernelModules = [ ];
  boot.kernelModules = [ "kvm-intel" ];
  boot.extraModulePackages = [ ];

  fileSystems."/" =
    { device = "zroot/root";
      fsType = "zfs";
    };

  fileSystems."/nix" =
    { device = "zroot/root/nix";
      fsType = "zfs";
    };

  fileSystems."/var" =
    { device = "zroot/root/var";
      fsType = "zfs";
    };

  fileSystems."/home" =
    { device = "zroot/root/home";
      fsType = "zfs";
    };

  fileSystems."/home/helenajurkowska" =
    { device = "zroot/root/home/helenajurkowska";
      fsType = "zfs";
    };

  fileSystems."/home/kazimierzkrauze" =
    { device = "zroot/root/home/kazimierzkrauze";
      fsType = "zfs";
    };

  fileSystems."/boot" =
    { device = "/dev/disk/by-uuid/F899-30FA";
      fsType = "vfat";
      options = [ "fmask=0022" "dmask=0022" ];
    };

  swapDevices =
    [ { device = "/dev/disk/by-uuid/b23cc832-6706-4de0-8657-e2a5d1786a4b"; }
    ];

  nixpkgs.hostPlatform = lib.mkDefault "x86_64-linux";
  hardware.cpu.intel.updateMicrocode = lib.mkDefault config.hardware.enableRedistributableFirmware;
}
{ config, pkgs-stable, pkgs-unstable, lib, inputs, ... }:

{
  imports =
    [
      ./packages.nix
      ./systemd-services.nix
      ./scripts/scripts.nix
      ./user-interfaces/user-interfaces.nix
      ./backup.nix
      ./filesystems.nix
      ./tlp.nix
    ];

  boot = {
    loader = {
      systemd-boot.enable = true;
      efi.canTouchEfiVariables = true;
    };
    initrd.supportedFilesystems = [ "zfs" "vfat" ];
    kernelPackages = pkgs-unstable.linuxPackages;
    kernel.sysctl."vm.swappiness" = 80;
    zfs.forceImportRoot = false;
  };

  networking.networkmanager.enable = true;

  console.keyMap = "pl";

  time.timeZone = "Europe/Warsaw";

  fonts.packages = [
    pkgs-stable.corefonts
  ];

  i18n = {
    defaultLocale = "en_GB.UTF-8";
    extraLocaleSettings = {
      LC_ADDRESS = "pl_PL.UTF-8";
      LC_IDENTIFICATION = "pl_PL.UTF-8";
      LC_MEASUREMENT = "pl_PL.UTF-8";
      LC_MONETARY = "pl_PL.UTF-8";
      LC_NAME = "pl_PL.UTF-8";
      LC_NUMERIC = "pl_PL.UTF-8";
      LC_PAPER = "pl_PL.UTF-8";
      LC_TELEPHONE = "pl_PL.UTF-8";
      LC_TIME = "pl_PL.UTF-8";
    };
  };

  xdg.portal = {
    enable = true;
    xdgOpenUsePortal = true;
    extraPortals = [
      pkgs-stable.kdePackages.xdg-desktop-portal-kde
    ];
  };

  environment = {
    variables.EDITOR = "vim";
    sessionVariables.NIXOS_OZONE_WL = "1";
  };

  services = {
    pulseaudio.enable = false;
    pipewire = {
      enable = true;
      alsa = {
        enable = true;
        support32Bit = true;
      };
      pulse.enable = true;
    };
    printing = {
      enable = true;
      openFirewall = true;
      drivers = [
        pkgs-stable.samsung-unified-linux-driver
        pkgs-stable.hplip
      ];
    };
    clamav = {
      daemon.enable = true;
      updater.enable = true;
    };
    fwupd.enable = true;
  };

  # Bluetooth
  hardware = {
    bluetooth = {
      enable = true;
      powerOnBoot = true;
      settings.General.Experimental = true;
    };
    i2c = {
      enable = true;
      group = "users";
    };
  };


  # Enable sound with pipewire.
  security = {
    rtkit.enable = true;
    polkit.enable = true;
    audit.enable = true;
    auditd.enable = true;
  };

  virtualisation = {
    libvirtd.enable = true;
    #waydroid.enable = true;
  };

  system.autoUpgrade = {
    enable = true;
    flake = inputs.self.outPath;
    dates = "daily";
  };

  nix = {
    extraOptions = ''
      access-tokens = github.com=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
    '';
    settings = {
      experimental-features = [
        "nix-command"
        "flakes"
      ];
      auto-optimise-store = true;
    };
    nixPath = [ "nixos-config=/home/kazimierzkrauze/Git/nixos-configuration" ];
    gc = {
      automatic = true;
      dates = "daily";
      options = "--delete-older-than 120d";
    };
  };

  programs = {
    virt-manager.enable = true;
    kdeconnect.enable = true;
    steam.enable = true;
  };

  networking = {
    firewall.enable = true;
    nameservers = [
      "208.67.222.123"
      "208.67.220.123"
    ];
  };

  system.stateVersion = "24.11"; # Did you read the comment?

Can you, please, help me out?

Best,
Miro

What is the mountpoint property on those datasets? i.e. What does zfs get mountpoint zroot/root say?

f user@kaktus 0s ~ -> zfs get mountpoint zroot/root
NAME        PROPERTY    VALUE       SOURCE
zroot/root  mountpoint  /           local
f user@kaktus 0s ~ ->

I understood that there is a conflict between zfs-mount and systemd mount, is it right?

That’s right. You probably want to sudo zfs set mountpoint=legacy zroot/root, and the same for the other datasets you mention in your NixOS configuration.

See ZFS - Official NixOS Wiki (which I bet was written by @ElvishJerricco ).

Thank you! That helped.

I have a similar issue, but using mountpoint=legacy breaks zfsbootmenu. Using mountpoint=/ and mountpoint=/nix worked fine on 25.11 and earlier by setting options = [ “zfsutil” ];inside my hardware-configuration.nix for the / and /nix filesystems. I can also manually finish the boot sequence by running the correct mount -t zfs -o zfsutils <dataset> <mountpoint> commands inside the emergency shell and then continuing boot.

Note, the reason you shouldn’t do options = [ "zfsutil" ]; is because that’s an implementation detail that indicates your use case just isn’t supported. Indeed, in this case the unsupported thing is just “root on non-legacy ZFS mountpoints”, which I explained here. The zfsutil option is not a feature, it is you signing a contract that you are correctly implementing a feature yourself. Ideally NixOS would just have a proper ZFS integration for stage 1.

Also, I’m curious how you’re doing zfsbootmenu with NixOS, given that NixOS needs to be the one controlling the boot menu and doesn’t have a zfsbootmenu module.

zfsutil is the flag used by stage 1 ZFS implementations, so I think it’s suited for a customized stage 1 boot. It would definitely be nice if nixos would automate it more, but OTOH using disko-zfs with systemd-boot “just works”. (Probably because it uses root=fstab and not root=zfs:.)

I am using the generationsDir bootloader with copyKernels and selecting the kernel and, optionally, snapshot using the zfsbootmenu UI. zfsbootmenu is very flexible, so with the right config it works even if the kernels aren’t exactly where it expects by default. The generations don’t really show up in the right order, but that’s only because I can already do the important part (select the latest by default, but also let me select an older one manually if I want.) and didn’t bother writing more refined hooks. Just booting into the latest generation doesn’t even need a hook. It’s probably even possible to work without “copyKernels” with a bit of metaphorical elbow grease.

I think I found a workaround by explicitly setting the legacy / as a boot environment in zfsbootmenu, but it would still be nice for the systemd stage 1 to handle this case properly. I’ve been booting this way since 23.11.

EDIT: Specifically, zfsutil should only be used for mounting filesystems inside the initramfs or other specific early boot use-cases. It’s how the “root=zfs” style functionality on other distros can work without needing to call directly into the C APIs to perform the mount. It’s certainly inelegant for the root and nix store to show up in /etc/fstab after boot is completed, but since those filesystems will be handled by systemd-remount-fs before zfs-mount runs this isn’t an issue in practice. It would be nice if anything not handled by the remount raised a nixos-rebuild error when defined in the filesystem list and given a non-legacy mountpoint.