Home-manager: "don’t know how to build these paths" using systemd-repart image

Hello,

Following the 23.11 I tried creating a NixOS image using the very helpful systemd-repart integration, and home-manager inside to seed the user configuration.

When starting the resulting image, the home-manager-user systemd service start fails with the following error :

Dec 25 10:49:20 nixos systemd[1]: Failed to start Home Manager environment for example.
Dec 25 10:49:20 nixos systemd[1]: home-manager-example.service: Failed with result 'exit-code'.
Dec 25 10:49:20 nixos systemd[1]: home-manager-example.service: Main process exited, code=exited, status=1/FAILURE
Dec 25 10:49:20 nixos hm-activate-example[945]: Then try activating your Home Manager configuration again.
Dec 25 10:49:20 nixos hm-activate-example[945]:     nix-env -e {package name}
Dec 25 10:49:20 nixos hm-activate-example[945]: and if there is a conflicting package you can remove it with
Dec 25 10:49:20 nixos hm-activate-example[945]:     nix-env -q
Dec 25 10:49:20 nixos hm-activate-example[945]: "nix-env -i"? Try running
Dec 25 10:49:20 nixos hm-activate-example[945]: Perhaps there is a conflict with a package that was installed using
Dec 25 10:49:20 nixos hm-activate-example[945]: Oops, Nix failed to install your new Home Manager profile!
Dec 25 10:49:20 nixos hm-activate-example[969]: error: path '/nix/store/dn95z7893jvhzjy0zdvpphx5yb6mp1n2-home-manager-path' does not exist and cannot be created
Dec 25 10:49:20 nixos hm-activate-example[969]:   /nix/store/dn95z7893jvhzjy0zdvpphx5yb6mp1n2-home-manager-path
Dec 25 10:49:20 nixos hm-activate-example[969]: don't know how to build these paths:
Dec 25 10:49:20 nixos hm-activate-example[969]: installing 'home-manager-path'
Dec 25 10:49:20 nixos hm-activate-example[945]: Activating installPackages
Dec 25 10:49:20 nixos hm-activate-example[945]: Activating writeBoundary
Dec 25 10:49:20 nixos hm-activate-example[945]: Activating checkLinkTargets
Dec 25 10:49:20 nixos hm-activate-example[945]: Activating checkFilesChanged
Dec 25 10:49:20 nixos hm-activate-example[945]: Starting Home Manager activation
Dec 25 10:49:20 nixos systemd[1]: Starting Home Manager environment for example...

Here is the default.nix configuration outputing a raw disk image, very similar to the manual :

let
  pkgs = import <nixos-23.11> { };
  efiArch = pkgs.stdenv.hostPlatform.efiArch;
  home-manager = fetchTarball https://github.com/nix-community/home-manager/archive/release-23.11.tar.gz;
in
(pkgs.nixos [
  ({ config, lib, pkgs, modulesPath, ... }: {

    imports = [
      "${modulesPath}/image/repart.nix"
      (import "${home-manager}/nixos")
      "${modulesPath}/profiles/qemu-guest.nix"
    ];

    boot.loader.grub.enable = false;

    fileSystems."/".device = "/dev/disk/by-label/nixos";

    users.users.example = {
      isNormalUser = true;
      password = "example";
    };

    users.users.root.password = "root";

    home-manager.users.example = {
      home.file."testhm".text = "OK";
      home.stateVersion = "23.11";
    };

    image.repart = {
      name = "image";
      partitions = {
        "esp" = {
          contents = {
            "/EFI/BOOT/BOOT${lib.toUpper efiArch}.EFI".source =
              "${pkgs.systemd}/lib/systemd/boot/efi/systemd-boot${efiArch}.efi";

            "/loader/entries/nixos.conf".source = pkgs.writeText "nixos.conf" ''
              title NixOS
              linux /EFI/nixos/kernel.efi
              initrd /EFI/nixos/initrd.efi
              options init=${config.system.build.toplevel}/init ${toString config.boot.kernelParams}
            '';

            "/EFI/nixos/kernel.efi".source =
              "${config.boot.kernelPackages.kernel}/${config.system.boot.loader.kernelFile}";

            "/EFI/nixos/initrd.efi".source =
              "${config.system.build.initialRamdisk}/${config.system.boot.loader.initrdFile}";
          };
          repartConfig = {
            Type = "esp";
            Format = "vfat";
            SizeMinBytes = "96M";
          };
        };
        "root" = {
          storePaths = [ config.system.build.toplevel ];
          repartConfig = {
            Type = "root";
            Format = "ext4";
            Label = "nixos";
            Minimize = "guess";
          };
        };
      };
    };

  })
]).image

The resulting disk image can be converted easily in a qcow2 and booted (UEFI paths may need to be adjusted, those are for Archlinux) :

qemu-img convert -O qcow2 ./result/image.raw ./nix.qcow2 qemu-system-x86_64 -machine pc-q35-8.2 -m size=512000k -cpu host -drive media=disk,format=qcow2,file=./nix.qcow2 -drive if=pflash,format=raw,readonly=on,file=/usr/share/edk2/x64/OVMF_CODE.fd -drive if=pflash,format=raw,readonly=on,file=/usr/share/edk2/x64/OVMF_VARS.fd -enable-kvm

Contrary to what nix-env -i reports during HM service activation, the store path does exist. I can’t figure out why nix-env fails.

  File: /nix/store/dn95z7893jvhzjy0zdvpphx5yb6mp1n2-home-manager-path/
  Size: 4096      	Blocks: 8          IO Block: 4096   directory
Device: 253,2	Inode: 30689       Links: 5
Access: (0555/dr-xr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2023-12-25 10:39:01.000000000 +0000
Modify: 1970-01-01 00:00:01.000000000 +0000
Change: 2023-12-25 10:38:55.000000000 +0000
 Birth: 2023-12-25 10:39:07.000000000 +0000

The issue looks like the one reported here.

Thanks for your help

I have the same problem when try to use repartd. Temporary workaround: use standalone home-manager configuration.

Thanks for the confirmation @mlyxshi. You activate the home-manager config manually at runtime, correct ?

I don’t see a way to seed ~/.config/nixpkgs/home.nix and launch home-manager switch without using home-manager first, or reimplement part of it with for example a systemd. Do you have another way ?

nix build .#homeConfigurations.NAME.activation-script

./result/activate