Docker daemon config in shell.nix

Hello Nix community,

I am new to Nix. I find it perfect for my usecase, and have decided to dive deeper.
I just started exploring it today. I have been stuck on one issue.
Some details: I am using a nonNix OS (WSL2 with systemd). I have created a shell.nix file.
How do I get docker daemon running using a shell.nix file?
I have tried the shellHook and within it I have added a dockerd command. But it says I need to be root.

I see there is something called nix modules - and maybe that is the way. But I need help. When I get the nix shell, I want the docker daemon to start automatically.

Here is the content of my shell.nix:

let
  nixpkgs = fetchTarball "https://github.com/NixOS/nixpkgs/tarball/nixos-23.05";
  pkgs = import nixpkgs { config = {}; overlays = []; };
in

pkgs.mkShell {
  packages = with pkgs; [
    git
    jq
    neovim
    docker
    temurin-bin-17
    mysql80
  ];

  GIT_EDITOR = "${pkgs.neovim}/bin/nvim";

  shellHook = ''
    alias ll="ls -l"
    alias vim="nvim"
    alias python="python3.12"
  '';

}

Thanks
Chetan

Have you considered podman? It is a near-drop-in-replacement for docker that supports running rootless, thus not requiring a daemon.

Yes, I have used podman, and it’s a good replacement. However, I also use minikube (and I am aware minikube works with podman as well). But I remember running into some issue with podman due to which I had to use docker again.
So would prefer docker.
Besides I would like to know how we can configure services(systemd services) using nix. Another usecase would be starting MySQL server.

You would probably want to use the docker daemon from your host system, or set up a systemd service to run the docker daemon from Nix. Since its a system-wide root thing, you probably would not have that in your shell.

For stuff like database server, which you can run as your user, shellHook would work, using something like this link. There were some libraries that do this for you as well, though I can’t remember/find the name at the moment.

1 Like

It’s one thing to configure systemd services using nix on NixOS, there you have systemd.services config options. You can also simply enable docker. It’s an entirely different thing doing this with nix, but outside of NixOS.

@accelbread just wrote what I wanted to add too. I think it might be easier to run the docker daemon independently of your nix shell.

1 Like

I think it might be GitHub - numtide/devshell: Per project developer environments that has shell services, but docs are hard to follow so can’t tell.

Eventually, system-manager might be a solution to what you need?

1 Like

Thanks, I see why starting a daemon (a systemwide thing) is not something that nix does by default. system-manager looks interesting. Thanks for sharing