Use nix-shell inside of a nixos-container

I’m using a nixos nspawn container for development, and would like to be able to use nix-shell (e.g. nix-shell -p inetutils) while logged in to this container. When I do so, I get the error error: file 'nixpkgs' was not found in the Nix search path as /nix/var/nix/profiles/per-user/root is empty. I’ve tried adding channels with nix-channel --add https://nixos.org/channels/nixpkgs-unstable and nix-channel --update (with and without sudo), and though they don’t fail, it doesn’t work.

Is there a way to make this work inside a container? I could try mounting the nix profile from the host though I doubt that would work and it sounds like a horrible idea.

Sorry, I lost you there. What does “for development” mean, here, please?

What’re the outputs of

  • printenv NIX_PATH
  • nix-channel --list

as both root and non-root?

Guess I tried to hide behind “development” to avoid an explanation, sorry :sweat_smile:. I use a nix container as a hacking box, and it’s pretty common that I need a piece of software that I haven’t defined in its config, and resetting the entire container to update the installed packages isn’t feasible.

The container is defined as part of my nix system config, this is right after a nixos-rebuild. Potentially relevant container config is system.autoUpgrade.channel = "https://nixos.org/channels/nixpkgs-unstable";

col0r@hacking:~/ > printenv NIX_PATH
/home/col0r/.nix-defexpr/channels:nixpkgs=/nix/var/nix/profiles/per-user/root/channels/nixos:nixos-config=/etc/nixos/configuration.nix:/nix/var/nix/profiles/per-user/root/channels
col0r@hacking:~/ > nix-channel --list
nixpkgs https://nixos.org/channels/nixpkgs-unstable

And root:

[root@hacking:/home/col0r]# printenv NIX_PATH
nixpkgs=/nix/var/nix/profiles/per-user/root/channels/nixos:nixos-config=/etc/nixos/configuration.nix:/nix/var/nix/profiles/per-user/root/channels
[root@hacking:/home/col0r]# nix-channel --list

I think I discovered the issue. I had mounted the home directory of the container for easy file transfer, and I think that might have prevented the .nix-defexpr directory from being created right. I rebuilt with no config changes and it’s working just fine now using the local channel. Guess this ended up being a rubber duck problem, thanks for pushing me in the right direction!