Oci-containers with systemd unit dependencies?

Greetings all,
something I’ve continuously butted up against (with my current knowledge) I’m unsure on how to deal with specific oci-containers that require other systemd services or mount units to operate.

my most common headache is ensuring that the container is started after the nfs mounts are up but also the appropriate order on shutdown.

At least from my testing the dependsOn variable only works for container names so entering something like mnt-containers.mount wouldn’t work.

any advice would be appreciated, I’ve attached some snippets from my server config below.

{ config, pkgs, ... }:

let
    mkServerMount = path: {
      device = "192.168.5.1:/mnt/myDATA-RZ2${path}";
      fsType = "nfs";
    };
in {
  fileSystems."/mnt/storage" = mkServerMount("/servers/nixos-server/mnt/storage");
  fileSystems."/mnt/containers" = mkServerMount("/servers/home-server/containers");
}
{ config, pkgs, ... }:

{
  virtualisation.oci-containers.containers = {
    nextcloud_database = {
		  image = "postgres:15.1";
		  autoStart = true;
		  volumes = [ 
			  "/mnt/containers/home-nextcloud/postgres_data:/var/lib/postgresql/data"
		  ];
		  environmentFiles = [
		    /etc/nixos/oci-containers/default.env
		    /etc/nixos/oci-containers/nextcloud/database.env
		  ];
    };
    
    nextcloud = {
      dependsOn = [ "nextcloud_database" ];
		  image = "nextcloud:25.0.3";
		  autoStart = true;
		  volumes = [ 
			  "/mnt/containers/home-nextcloud/nextcloud_data:/var/www/html"
		  ];
		  environmentFiles = [
		    /etc/nixos/oci-containers/default.env
		    /etc/nixos/oci-containers/nextcloud/database.env
		  ];
		  environment = {
		    POSTGRES_HOST = "nextcloud_database";
		  }; 
    };
  };
}

I haven’t tested it, but if I’m reading someone else’s config right, you should be able to “edit” the unit created by oci-container by calling systemd.services.xxxx for the generated service. So, for your nextcloud container (in addition to your oci-container config):

services.systemd.podman-nextcloud = {
  wantedBy = [ "multi-user.target" ];
  after = [ "mnt-container.mount" ];
  description = "Nextcloud container";
};

podman-nextcloud should be the generated name of the service unit created by oci-container. Not sure if you’re using podman or not, but you get the idea. Give a try and let me know if it works!

1 Like

Thank you I’ve embarrassingly been skirting around this issue for months.
I knew I had to be missing something simple but I guess I still coming to grasps with the power and flexibility of nix.

to help others in the future

systemd.services.podman-nextcloud = {
  requires = [ "mnt-containers.mount" ];
  after = [ "mnt-containers.mount" ];
};