How to run docker daemon from nix (not NixOS)?


Nix newbie here. How can I run the docker daemon found in nix-shell -p docker_26?

$ command -v dockerd
  • With unix:///var/run/docker.sock
  • Not rootless
  • With docker group
  • As systemd unit
  • Starts up at boot

I’m surprised I couldn’t find a good resource for this. Docker - NixOS Wiki describes how to get it working in NixOS, not nix. Or am I mistaken?

Nix would be ideal for this, since Ubuntu 22.04 only has 20.10, which is now over a year old, and to use the latest and greatest one needs to add a custom apt repo with Install Docker Engine on Ubuntu, and now I already have Nix.

But if this not a good fit for Nix and is going to be an uphill battle, I’ll just follow the non-nix Install Docker Engine on Ubuntu.

My motivation is twofold:

  1. I’d like to understand the limits of nix - the package manager on Ubuntu. What is it good for and what not. Are packages that include daemons or services a bad fit?
  2. I’d like to use Nix for everything! :slight_smile:

I’m just surprised I couldn’t find any canonical documentation on this.

1 Like

In theory you can just run that binary, you’ll “just” need to do all the configuration by hand. YMMV with apparmor and such.

There are no guides for it because it’s not really an expected use case and distros vary too much for good generic guides. Not even docker themselves document how to bootstrap docker from a build.

There’s currently no good mechanism for configuring services on non-NixOS hosts. Some projects exist that do this, such as home-manager and devshell, and there is an RFC for making the nixpkgs services less NixOS-specific, but I think docker specifically is generally quite complex to configure as it needs to do so much to work, so it will probably be among the last services to work if that much ever becomes possible.

NixOS exists for a reason; doing all the work to make these service configurations without controlling the rest of the environment is a bit of a tall order. You might have more luck switching to NixOS if you really want to do everything with nix.

1 Like

Adding to the answer of @TLATER:

  • there is GitHub - numtide/system-manager: Manage system config using nix on any distro to manage OS-level service on e.g. Ubuntu
  • is a devshell on steroids if you want to tie containers to your project rather than to your system
  • You can replace docker with podman in most cases. I’ve successfully used podman from a nix shell with only minimal setup (just a small config file) to build and run containers on Ubuntu
  • You can also use nix to run distrobox, but the benefit of nix is pretty much limited to pinning Distrobox in that case.
1 Like

Perfect answer. Thank you.