I am able to successfuly run rootless docker with nvidia GPU support on NixOS.
I think it’s a little messy and not NixOS style, but (hey!) “it just works”!
The steps to reproduce:
- add your user to docker group:
extraGroups = [ “docker” ];
-
find your rootless docker.service script
run in console
$ systemctl --user status docker.service
check out output
you’ll find something like “Loaded: loaded (/etc/systemd/user/docker.service; enabled; preset: enabled)”
copy the file in your local systemd directory to use as a template for your future modifications.
$ cp /etc/systemd/user/docker.service ~/.config/systemd/user/docker-mod.service
- find your sudo/root docker deamon config
$ ps aux | grep docker
on my system it’s
/nix/store/<hash…>-moby-24.0.5/libexec/docker/dockerd --config-file=/nix/store/<hash…>-daemon.json
copy to change later
$ cp /nix/store/<hash…>-daemon.json ~/.config/systemd/user/daemon.json
-
change your service script and config file
i only changed fd:// to unix:// in [daemon.json]
...
"hosts": [
"unix://"
],
...
the full text of [daemon.json] after that
{
"group": "docker",
"hosts": [
"unix://"
],
"live-restore": true,
"log-driver": "journald",
"runtimes": {
"nvidia": {
"path": "/nix/store/<hash...>-nvidia-docker/bin/nvidia-container-runtime"
}
}
}
then changed [docker-mod.service], only changed the path to the changed config [daemon.json]
...
ExecStart=/nix/store/<hash...>-docker-24.0.5/bin/dockerd-rootless --config-file=/home/<USER-NAME>/.config/systemd/user/daemon.json
...
- Start your new service via systemd
stop the old rootless service
$ systemctl --user stop docker.service
start the new rootless service
$ systemctl --user start docker-mod.service
check status of the new service
$ systemctl --user status docker-mod.service
you will see something like “API listen on /var/run/docker.sock”
- run docker to test setup
$ DOCKER_HOST=unix:///var/run/docker.sock docker run --gpus ‘all’ pytorch/pytorch:2.1.2-cuda11.8-cudnn8-runtime nvidia-smi
or you can use --runtime instead
$ DOCKER_HOST=unix:///var/run/docker.sock docker run --runtime=nvidia --rm -ti nvidia/cuda:12.3.1-base-ubuntu20.04 nvidia-smi