Can't get NixOS Containers using docker as a backend to start

Hi,

I’m pretty new to NixOS and am attempting to setup declarative docker containers using Nix containers as described here. In this case I’m trying to do this for jellyfin for a home server. I am aware that a better way todo this would be to use jellyfin from nix packages however I also am planning on setting up other containers some of which would likely be easier using this method and I’d like to be consistent. For context my user is called user.
I have defined a jellyfin.nix file as such

{
  pkgs,
  lib,
  ...
}: {
  virtualisation.docker = {
    enable = true;
    autoPrune.enable = true;
  };
  virtualisation.oci-containers.backend = "docker";
  virtualisation.oci-containers.containers."jellyfin" = {
    image = "jellyfin/jellyfin";
    volumes = [
      "/root/home/user/docker/jellyfin/cache/:/cache:rw"
      "/root/home/user/docker/jellyfin/config/:/config:rw"
      "/root/home/user/media:/media:rw"
    ];
    ports = ["8096:8096"];
    user = "uid:gid";
    log-driver = "journald";
    extraOptions = [
      "--network=host"
    ];
  };
  systemd.services."docker-jellyfin" = {
    serviceConfig = {
      Restart = lib.mkOverride 500 "always";
      RestartMaxDelaySec = lib.mkOverride 500 "1m";
      RestartSec = lib.mkOverride 500 "100ms";
      RestartSteps = lib.mkOverride 500 9;
    };
    partOf = [
      "docker-compose-jellyfin-root.target"
    ];
    wantedBy = [
      "docker-compose-jellyfin-root.target"
    ];
  };

  systemd.targets."docker-compose-jellyfin-root" = {
    unitConfig = {
    };
    wantedBy = ["multi-user.target"];
  };
}

After rebuilding and running this container using

sudo systemctl start container@adguardhome

Nothing starts, when I inspect the process using systemctl status (container info) I get:

○ container@jellyfin.service - Container 'jellyfin'
     Loaded: loaded (/etc/systemd/system/container@.service; static)
     Active: inactive (dead)

Jun 17 23:22:05 nixos systemd[1]: container@jellyfin.service: Scheduled restart job, restart counter is at 4.
Jun 17 23:22:05 nixos systemd[1]: Starting Container 'jellyfin'...
Jun 17 23:22:05 nixos container jellyfin[2606]: /nix/store/w30lysmbfcljfzv5hpyg994gmqfly1dm-unit-script-container_-start/bin/container_-start: line 54: /nix/var/nix/profiles/per-container/jellyfin/system/system: >
Jun 17 23:22:05 nixos systemd[1]: container@jellyfin.service: Main process exited, code=exited, status=1/FAILURE
Jun 17 23:22:05 nixos systemd[1]: container@jellyfin.service: Failed with result 'exit-code'.
Jun 17 23:22:05 nixos systemd[1]: Failed to start Container 'jellyfin'.

If I look at the journalctl logs I get that the error is:

Jun 17 23:22:04 nixos systemd[1]: Starting Container 'jellyfin'...
░░ Subject: A start job for unit container@jellyfin.service has begun execution
░░ Defined-By: systemd
░░ Support: https://lists.freedesktop.org/mailman/listinfo/systemd-devel
░░ 
░░ A start job for unit container@jellyfin.service has begun execution.
░░ 
░░ The job identifier is 1109.
Jun 17 23:22:04 nixos container jellyfin[2570]: /nix/store/w30lysmbfcljfzv5hpyg994gmqfly1dm-unit-script-container_-start/bin/container_-start: line 54: /nix/var/nix/profiles/per-container/jellyfin/system/system: No such file or directory
Jun 17 23:22:04 nixos systemd[1]: container@jellyfin.service: Main process exited, code=exited, status=1/FAILURE
░░ Subject: Unit process exited
░░ Defined-By: systemd
░░ Support: https://lists.freedesktop.org/mailman/listinfo/systemd-devel
░░ 
░░ An ExecStart= process belonging to unit container@jellyfin.service has exited.
░░ 
░░ The process' exit code is 'exited' and its exit status is 1.
Jun 17 23:22:04 nixos systemd[1]: container@jellyfin.service: Failed with result 'exit-code'.
░░ Subject: Unit failed
░░ Defined-By: systemd
░░ Support: https://lists.freedesktop.org/mailman/listinfo/systemd-devel
░░ 
░░ The unit container@jellyfin.service has entered the 'failed' state with result 'exit-code'.
Jun 17 23:22:04 nixos systemd[1]: Failed to start Container 'jellyfin'.
░░ Subject: A start job for unit container@jellyfin.service has failed
░░ Defined-By: systemd
░░ Support: https://lists.freedesktop.org/mailman/listinfo/systemd-devel
░░ 
░░ A start job for unit container@jellyfin.service has finished with a failure.
░░ 
░░ The job identifier is 1109 and the job result is failed.

I can see that the error is that there is no file or directory at nix/var/nix/profiles/per-container/jellyfin/system/system but beyond that I am unsure of how to fix this. I’ve looked through most of the documentation on Nix containers using docker and past issues but can’t seem to find anything, I realise I’ve probably just made a small mistake in my jellyfin.nix file but can’t seem to figure out where it is.
Any help would be appreciated, Thanks so much.

NixOS containers and OCI (“Docker”) containers are two entirely separate things.

NixOS containers spawn an entire NixOS instance using systemd-nspawn and are defined using containers.* options. Their systemd units are named container@name

OCI containers are run as in other distros (e.g. docker run), just wrapped in a systemd unit, and are defined using the virtualisation.oci-containers.containers.* options. Their systemd units are named docker-name or podman-name.

Seems like you’re trying to use a Jellyfin OCI container, so the correct command would be systemctl start docker-jellyfin. Although autoStart is true by default, so it should automatically start after a rebuild or boot.

2 Likes

Thanks so much!
Yes that fixed my problem.
To add some more context for anyone who searches for that error log, it was specificially an error because I hadn’t defined the UI & GID properly in my jellyfin.nix file, but I wasn’t able to get the correct logs to see that till I knew to do systemctl status docker-jellyfin