Nix.channel.enable in containers

I’m playing with containers and /nix/var/nix/profiles/ is empty and various nix commands fail (ex: build, can’t find <nixpkgs>). AFAICT nix.channel.enable says that per-user/root/... will be created, and that’s on by default (although I tried setting it explicitly too), so I’m not sure what’s going on.

Some of that happens during build I assume, but some must happen dynamically - does that happen at first login? I didn’t define a root user, but machinectl login root@dev didn’t trigger it to be created either.

I was thinking of opening an issue but I thought maybe this is expected behavior and I missed something. I didn’t see anything in virtualisation/nixos-containers.nix about channels, that file seems pretty simple, but I might have missed something.

Okay, the documentation for nix.channel.enable says

Whether the nix-channel command and state files are made available on the machine.

The following files are initialized when enabled:

  • /nix/var/nix/profiles/per-user/root/channels
  • /root/.nix-channels
  • $HOME/.nix-defexpr/channels (on login)

Disabling this option will not remove the state files from the system.

I see the latter two. The former is added to NIX_PATH as per the code, so the option is enabled and I’ve confirmed it’s operational.

The text is wrong though, channel.enable doesn’t initialize /nix/var/nix/profiles/per-user/.... Digging in, AFAICT that’s done once at installation, via nixos-install.sh or the cd installer.

Strangely, that also sets up root/.nix-defexpr/channels. I’m not sure why that’s done in two places.

Anyway, it looks more intense than I thought so I’m going to try just bind-mounting the host-side profile and see if that works…

Simple nix build worked with the bind mount. I’m not sure if that’s guaranteed to be mounted in the correct order (after the nix tree is set up) or if there’s other things that will bite me later but :crossed_fingers: