nixpkgs icon indicating copy to clipboard operation
nixpkgs copied to clipboard

nixos/virtualisation: nixos-container fails to start with private users enabled when build as VM

Open hfxbse opened this issue 1 year ago • 3 comments

Nixpkgs version

  • Unstable (25.11)

Describe the bug

When containers.<name>.privateUsers is set to "pick" or any numeric value, the container fails to start. This appears to be caused by the module appending ":idmap" to bind-mount paths when private users are enabled. Disabling private users but manually adding the same ":idmap" suffix to a separate bind mode produces the same failure.

Steps to reproduce

Given this minimal configuration:

{
  inputs.nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";

  outputs =
    { nixpkgs, ... }:
    let
      system = "x86_64-linux";
      lib = nixpkgs.lib;
    in

    {
      nixosConfigurations.container-test = lib.nixosSystem {
        inherit system;
        modules = [
          {
            services.getty.autologinUser = "root";
            system.stateVersion = "25.11";
            
            # The actual container configuration
            containers.test = {
              autoStart = true;
              privateUsers = "pick";  # This option does not work as expected
              config.system.stateVersion = "25.11";
            };
          }
        ];
      };
    };
}

Build an VM by running nixos-rebuild build-vm --flake .#container-test and executing it afterwards.

Expected behaviour

The container starts correctly with private users enabled.

Screenshots

No response

Relevant log output

Oct 11 22:29:32 nixos systemd-nspawn[918]: ░ Spawning container test on /var/lib/nixos-containers/test.
Oct 11 22:29:32 nixos systemd-nspawn[929]: Selected user namespace base 1492320256 and range 65536.
Oct 11 22:29:32 nixos systemd-nspawn[929]: Failed to map ids for bind mount /var/lib/nixos-containers/test/nix/store: Invalid argument
Oct 11 22:29:32 nixos systemd[1]: [email protected]: Main process exited, code=exited, status=1/FAILURE
Oct 11 22:29:32 nixos systemd[1]: [email protected]: Failed with result 'exit-code'.
Oct 11 22:29:32 nixos systemd[1]: Failed to start Container 'test'.
Oct 11 22:29:32 nixos systemd[1]: [email protected]: Scheduled restart job, restart counter is at 5.
Oct 11 22:29:32 nixos systemd[1]: [email protected]: Start request repeated too quickly.
Oct 11 22:29:32 nixos systemd[1]: [email protected]: Failed with result 'exit-code'.
Oct 11 22:29:32 nixos systemd[1]: Failed to start Container 'test'.

Additional context

Issue #419007 states that the ":idmap" suffix is required to obtain correct permissions for bind mounts inside the container.

The only other information I was able to find that might be related is this issue for systemd nspawn itself as well as one about the general topic user namespaces #57083.

Another WIP container module implementation mentioned in https://github.com/NixOS/nixpkgs/issues/69414#issuecomment-2564755036 seems to have the same issue using noidmap for the store instead.

System metadata

  • system: "x86_64-linux"
  • host os: Linux 6.12.51, NixOS, 25.11 (Xantusia), 25.11.20251009.0b4defa
  • multi-user?: no
  • sandbox: yes
  • version: nix-env (Nix) 2.31.2
  • nixpkgs: not found

Notify maintainers

@jbgi


Note for maintainers: Please tag this issue in your pull request description. (i.e. Resolves #ISSUE.)

I assert that this issue is relevant for Nixpkgs

Is this issue important to you?

Add a :+1: reaction to issues you find important.

hfxbse avatar Oct 11 '25 22:10 hfxbse

Looks like this is due to https://github.com/systemd/systemd/issues/25886: The script generated by build-vm mounts /nix/store using overlayfs, which does not support idmap. Not sure if there is any workaround.

jbgi avatar Oct 19 '25 10:10 jbgi

Looks like is due to systemd/systemd#25886: The script generated by build-vm mounts /nix/store using overlayfs, which does not support idmap. Not sure if there is any workaround.

Thanks for pointing that out! I somehow never thought of running the configuration outside the VM. It turns out the overlayfs really is the root cause of the issue. Fortunately, you can tell Nix to build a separate store image by setting virtualisation.vmVariant.virtualisation.useNixStoreImage = true;, which eliminates the overlayfs. After that, the container starts up normally.

Maybe it’d make sense to add something like ...useNixStoreImage = any (container.privateUsers != "no") to the module? Though just mentioning this quirk in the option’s description might already be enough.

hfxbse avatar Oct 19 '25 11:10 hfxbse

I just realized the Nix store defaults to read-only when using an image disk. Making it writable introduces the overlayfs again.

As a result we either can have a failing containers or a read-only Nix store for the whole VM. The latter would fit my use-case better as I use the VM build to validate my configuration before applying it to my machines. However, I do not feel like the default behavior is for me to decide. I am fine either way as long as the behavior is documented.

hfxbse avatar Oct 22 '25 17:10 hfxbse