Repository: aksiksi/compose2nix (go)

Issue:
Fix root target wantedBy logic for volumes & networks
Two options:

1. Move this to code instead of doing it within the template (like containers)
2. Define a `rootTarget` method on the config to be able to check `NoCreateRootTarget`

Pull request:
fix root targets for networks and volumes
Fixes #42 

Patch:
diff --git a/nix.go b/nix.go
index 3bef7ab..8eb36de 100644
--- a/nix.go
+++ b/nix.go
@@ -252,6 +252,9 @@ func rootTarget(runtime ContainerRuntime, project *Project) string {
 }
 
 func (c *NixContainerConfig) rootTargetTemplateFunc() string {
+	if !c.CreateRootTarget {
+		return ""
+	}
 	return rootTarget(c.Runtime, c.Project)
 }
 


Test patch:
diff --git a/nix_test.go b/nix_test.go
index 20c04ac..227cd2a 100644
--- a/nix_test.go
+++ b/nix_test.go
@@ -110,9 +110,8 @@ func TestProject(t *testing.T) {
 func TestUnusedResources(t *testing.T) {
 	composePath, _ := getPaths(t, false)
 	g := &Generator{
-		Project:            NewProject("myproject"),
-		Inputs:             []string{composePath},
-		NoCreateRootTarget: true,
+		Project: NewProject("myproject"),
+		Inputs:  []string{composePath},
 	}
 	runSubtestsWithGenerator(t, g)
 }
@@ -292,3 +291,13 @@ func TestDeployDevices(t *testing.T) {
 	}
 	runSubtestsWithGenerator(t, g)
 }
+
+func TestNoCreateRootTarget(t *testing.T) {
+	composePath, _ := getPaths(t, false)
+	g := &Generator{
+		Inputs:             []string{composePath},
+		Project:            NewProject("test"),
+		NoCreateRootTarget: true,
+	}
+	runSubtestsWithGenerator(t, g)
+}
diff --git a/testdata/TestNoCreateRootTarget.compose.yml b/testdata/TestNoCreateRootTarget.compose.yml
new file mode 100644
index 0000000..093220f
--- /dev/null
+++ b/testdata/TestNoCreateRootTarget.compose.yml
@@ -0,0 +1,14 @@
+services:
+  service:
+    image: nginx:latest
+    volumes:
+      - my-volume:/mnt/volume
+    networks:
+      - my-network
+
+networks:
+  my-network:
+    driver: bridge
+
+volumes:
+  my-volume:
diff --git a/testdata/TestNoCreateRootTarget.docker.nix b/testdata/TestNoCreateRootTarget.docker.nix
new file mode 100644
index 0000000..6b61143
--- /dev/null
+++ b/testdata/TestNoCreateRootTarget.docker.nix
@@ -0,0 +1,62 @@
+{ pkgs, lib, ... }:
+
+{
+  # Runtime
+  virtualisation.docker = {
+    enable = true;
+    autoPrune.enable = true;
+  };
+  virtualisation.oci-containers.backend = "docker";
+
+  # Containers
+  virtualisation.oci-containers.containers."test-service" = {
+    image = "nginx:latest";
+    volumes = [
+      "test_my-volume:/mnt/volume:rw"
+    ];
+    log-driver = "journald";
+    autoStart = false;
+    extraOptions = [
+      "--network-alias=service"
+      "--network=test_my-network"
+    ];
+  };
+  systemd.services."docker-test-service" = {
+    serviceConfig = {
+      Restart = lib.mkOverride 500 "no";
+    };
+    after = [
+      "docker-network-test_my-network.service"
+      "docker-volume-test_my-volume.service"
+    ];
+    requires = [
+      "docker-network-test_my-network.service"
+      "docker-volume-test_my-volume.service"
+    ];
+  };
+
+  # Networks
+  systemd.services."docker-network-test_my-network" = {
+    path = [ pkgs.docker ];
+    serviceConfig = {
+      Type = "oneshot";
+      RemainAfterExit = true;
+      ExecStop = "docker network rm -f test_my-network";
+    };
+    script = ''
+      docker network inspect test_my-network || docker network create test_my-network --driver=bridge
+    '';
+  };
+
+  # Volumes
+  systemd.services."docker-volume-test_my-volume" = {
+    path = [ pkgs.docker ];
+    serviceConfig = {
+      Type = "oneshot";
+      RemainAfterExit = true;
+    };
+    script = ''
+      docker volume inspect test_my-volume || docker volume create test_my-volume
+    '';
+  };
+}
diff --git a/testdata/TestNoCreateRootTarget.podman.nix b/testdata/TestNoCreateRootTarget.podman.nix
new file mode 100644
index 0000000..7089ed6
--- /dev/null
+++ b/testdata/TestNoCreateRootTarget.podman.nix
@@ -0,0 +1,67 @@
+{ pkgs, lib, ... }:
+
+{
+  # Runtime
+  virtualisation.podman = {
+    enable = true;
+    autoPrune.enable = true;
+    dockerCompat = true;
+    defaultNetwork.settings = {
+      # Required for container networking to be able to use names.
+      dns_enabled = true;
+    };
+  };
+  virtualisation.oci-containers.backend = "podman";
+
+  # Containers
+  virtualisation.oci-containers.containers."test-service" = {
+    image = "nginx:latest";
+    volumes = [
+      "test_my-volume:/mnt/volume:rw"
+    ];
+    log-driver = "journald";
+    autoStart = false;
+    extraOptions = [
+      "--network-alias=service"
+      "--network=test_my-network"
+    ];
+  };
+  systemd.services."podman-test-service" = {
+    serviceConfig = {
+      Restart = lib.mkOverride 500 "no";
+    };
+    after = [
+      "podman-network-test_my-network.service"
+      "podman-volume-test_my-volume.service"
+    ];
+    requires = [
+      "podman-network-test_my-network.service"
+      "podman-volume-test_my-volume.service"
+    ];
+  };
+
+  # Networks
+  systemd.services."podman-network-test_my-network" = {
+    path = [ pkgs.podman ];
+    serviceConfig = {
+      Type = "oneshot";
+      RemainAfterExit = true;
+      ExecStop = "podman network rm -f test_my-network";
+    };
+    script = ''
+      podman network inspect test_my-network || podman network create test_my-network --driver=bridge
+    '';
+  };
+
+  # Volumes
+  systemd.services."podman-volume-test_my-volume" = {
+    path = [ pkgs.podman ];
+    serviceConfig = {
+      Type = "oneshot";
+      RemainAfterExit = true;
+    };
+    script = ''
+      podman volume inspect test_my-volume || podman volume create test_my-volume
+    '';
+  };
+}
diff --git a/testdata/TestUnusedResources.docker.nix b/testdata/TestUnusedResources.docker.nix
index 0745719..048b4cd 100644
--- a/testdata/TestUnusedResources.docker.nix
+++ b/testdata/TestUnusedResources.docker.nix
@@ -7,4 +7,13 @@
     autoPrune.enable = true;
   };
   virtualisation.oci-containers.backend = "docker";
+
+  # Root service
+  # When started, this will automatically create all resources and start
+  # the containers. When stopped, this will teardown all resources.
+  systemd.targets."docker-compose-myproject-root" = {
+    unitConfig = {
+      Description = "Root target generated by compose2nix.";
+    };
+  };
 }
diff --git a/testdata/TestUnusedResources.podman.nix b/testdata/TestUnusedResources.podman.nix
index cbbf4d3..595331a 100644
--- a/testdata/TestUnusedResources.podman.nix
+++ b/testdata/TestUnusedResources.podman.nix
@@ -12,4 +12,13 @@
     };
   };
   virtualisation.oci-containers.backend = "podman";
+
+  # Root service
+  # When started, this will automatically create all resources and start
+  # the containers. When stopped, this will teardown all resources.
+  systemd.targets."podman-compose-myproject-root" = {
+    unitConfig = {
+      Description = "Root target generated by compose2nix.";
+    };
+  };
 }