paralin / docker-nixos

Docker multi-stage build for compiling nixos.
MIT License
13 stars 4 forks source link

Running commands in the container? #1

Open TRPB opened 3 months ago

TRPB commented 3 months ago

Very interesting concept!

Is there any way we can get a functional OS when running commands inside the container?

running docker exec -it nix /bin/sh opens a shell but the path isn't set correctly so even things like ls don't work.

Is there a way to set the PATH as intended?

paralin commented 3 months ago

Hi, I built this for SkiffOS and on there skiff-core starts the shell with the path set correctly. I don't remember the specifics but feel free to check there under core/nixos and see what I did. I plan to update all this soon but it's on a back burner as I'm not using nixos right now.

TRPB commented 3 months ago

thanks, using the skiffos/skiff-core-nixos:latest image suffers the same problem. I wonder if it's to do with host config.

It seems to be to do with the filesystem:

nix-1  | /init: line 60: /dev/kmsg: Permission denied
nix-1  | running activation script...
nix-1  | setting up /etc...
nix-1  | /nix/store/in5sln8zqi7cm37zzs6hsxd30m4c1qps-local-cmds: line 23: /run/systemd/container: No such file or directory
nix-1  | unpacking the NixOS/Nixpkgs sources...
nix-1  | ln: failed to create symbolic link '/root/.nix-defexpr/channels/channels': Read-only file system
nix-1  | starting systemd...

I'm guessing it's the failed ln that's the underlying issue as it's not configuring the channels.

I did try forcing /root/.nix-defexpr to be a volume or mounting it as tmpfs but had the same issue.

I'll keep digging but if you have any pointers I'd be happy to hear them

TRPB commented 2 months ago

To answer my own question using bash works but you have to get the full path to it e.g.

docker exec -it container /nix/store/306znyj77fv49kwnkpxmb0j2znqpa8bj-bash-5.2p26/bin/bash

When that's launched PATH is set and everything works.

If you're interested with what I did with the project I made it so you can load an arbitrary nixos configuration into the container without rebuilding the image. It's a little janky and proof of concept but working well: https://github.com/TRPB/docker-nixos

I had to replace your first build stage with arch as I couldn't get rapidcheck on debian which is now a build requirement. I also updated NixOS in the image to 24.05 and managed to avoid the need to run the container in privileged mode (though you do need rw cgroups from the host)

paralin commented 2 months ago

Thanks for the notes! I'll check it out