Schroot from Debian into NixOS

I have Debian running and want to maintain a NixOS installation on a separate btrfs partition. I have this schroot configuration:

$ cat /etc/schroot/chroot.d/nixos 
[nixos]
description=NixOS partition
type=directory
directory=/mnt/nixos/@
users=myself

And try to enter the nixos installation this way:

$ schroot -c nixos
E: 15binfmt: update-binfmts: unable to open /var/run/schroot/mount/nixos-0cc7367a-84b4-4a8d-9ba3-1d36b05
391c4/bin/sh: No such file or directory
E: 20nssdatabases: /etc/schroot/setup.d/20nssdatabases: 32: cannot create /var/run/schroot/mount/nixos-0
cc7367a-84b4-4a8d-9ba3-1d36b05391c4/etc/services: Directory nonexistent
E: 15binfmt: update-binfmts: unable to open /var/run/schroot/mount/nixos-0cc7367a-84b4-4a8d-9ba3-1d36b05
391c4/bin/sh: No such file or directory
E: nixos-0cc7367a-84b4-4a8d-9ba3-1d36b05391c4: Chroot setup failed: stage=setup-start

What is going wrong here and how can I fix it? There are no problems schrooting into other Debian and Ubuntu installations in the same way.

This is a guess, I’ve never used schroot in anger, but since NixOS doesn’t use an fhs directory structure /bin/sh only exists for compatibility reasons. It’s a symlink to some path in the nix store on this distro. Chances are it doesn’t exist until after the first boot, and schroot clearly needs it.

1 Like

What does this schroot do?
Maybe it does need patches in order to run in NixOS.

schroot is like chroot but it also mounts some devices and connects to the network.

Btw. plain chroot fails with

chroot: failed to run command ‘/bin/bash’: No such file or directory

but maybe I should treat this as a separate issue.

1 Like

Yes, you also cannot execute /bin/bash with chroot, bash does not exist in that location, NixOS is not an fhs-compliant distro. Try chroot <root> /nix/store/<some hash>-bash-<version>/bin/bash.

The root cause is the same, NixOS is just quite different from traditional distros, and therefore some of the assumptions of the tools you use may not hold. In this case, the assumption that /bin/bash and /bin/sh exist. NixOS isn’t the only distro where this might fail, alpine for example would break this too, at least in the bash case.

To fix this, you’ll need to know a bit more about what schroot needs. What exactly is your use case for this, why are you chrooting at all? Maybe we can point you in the right direction from there.

1 Like

I have a NixOS installation on a separate partition and use mainly Debian (still, maybe?). I want to build and test my programs in different environments (other Debian and Ubuntu versions and NixOS) without booting into those operating systems. I want to update packages in the NixOS partition.
Alternatively using Nix from Debian user-space does not work here. All Nix commands nix-env, nix-shell etc. terminate all my sessions after doing some work. Looks like a very ugly bug, but it prevents me from using Nix in user space, but this is a different issue.

It looks suspiciously similar to nixos-enter:

From the sources:

Maybe we (we, who? O.O) can look at the original code of schroot and send some patches from nixos-enter!
Unfortunately I believe the easiest patch is if itsanixos then print ("hey, you are trying to chroot on NixOS! Use nixos-enter instead.")

I am happy with nixos-enter, if that one works.

I have pulled the Nix Git repository and try:

nixpkgs$ bash nixos/modules/installer/tools/nixos-enter.sh --help
unshare: failed to execute nixos/modules/installer/tools/nixos-enter.sh: Datei oder Verzeichnis nicht gefunden

The German error message means: “File or directory not found”.

It seems I need a command called “unshare”. Never heard of it.

With this chroot I can actually enter the NixOS partition:

sudo chroot /mnt/nixos /nix/store/96ky1zdkpq871h2dlk198fz0zvklr1dr-bash-5.1-p16/bin/bash
1 Like

First:
unshare is part of util-linux. You can learn more in the manpage.

Second, unfortunately nixos-enter pressuposes a NixOS system. Sorry about that.

But I believe it is possible to use schroot as-is.
I am reading the schroot.conf manpage, but I am not finding nothing about setting the login shell.

P.S.:

  1. It looks like we don’t have it on Nixpkgs yet. I will package it!
  2. It is obsolete: codelibre / schroot ¡ GitLab
  3. new house: shelter/reschroot: Fork of schroot - Codeberg.org
  4. stuck in CMake 2.8 while Nixpkgs ships 3.26.4. I will not pursue this anymore.
  5. In case anyone is interested, the branch schroot-1.7 was updated 3 years ago: shelter/reschroot: Fork of schroot - Codeberg.org
1 Like