From 6c1f46df15bd5ac3b2552f1f896b50b404a5e83c Mon Sep 17 00:00:00 2001 From: Nicholas Hassan Date: Tue, 18 Mar 2025 14:55:36 +1030 Subject: [PATCH] podman: use type in attr name of built quadlets --- modules/services/podman-linux/containers.nix | 29 +++++++++++-------- .../services/podman-linux/install-quadlet.nix | 5 ++-- .../integration-container-expected.service | 14 ++++----- .../integration-network-expected.service | 10 +++---- .../integration-volume-expected.service | 10 +++---- .../services/podman-linux/integration.nix | 12 ++++---- 6 files changed, 43 insertions(+), 37 deletions(-) diff --git a/modules/services/podman-linux/containers.nix b/modules/services/podman-linux/containers.nix index 8b4c1db87..470eb0a7f 100644 --- a/modules/services/podman-linux/containers.nix +++ b/modules/services/podman-linux/containers.nix @@ -9,11 +9,17 @@ let createQuadletSource = name: containerDef: let - dependencyBySuffix = type: name: - if (hasInfix ".${type}" name) then - let baseName = elemAt (splitString ".${type}" name) 0; - in if (hasAttr baseName cfg.internal.builtQuadlets) then - [ (cfg.internal.builtQuadlets.${baseName}) ] + extractQuadletReference = type: value: + let + regex = "([a-zA-Z0-9_-]+\\." + type + ").*"; + parts = builtins.match regex value; + in if parts == null then value else builtins.elemAt parts 0; + + dependencyBySuffix = type: value: + if (hasInfix ".${type}" value) then + let name = extractQuadletReference type value; + in if (hasAttr name cfg.internal.builtQuadlets) then + [ (cfg.internal.builtQuadlets.${name}) ] else [ ] else @@ -32,28 +38,27 @@ let ++ (withResolverFor "volume" containerDef.volumes); checkQuadletReference = types: value: - let baseName = t: elemAt (splitString ".${t}" value) 0; - in if builtins.isList value then + if builtins.isList value then builtins.concatLists (map (checkQuadletReference types) value) else let type = findFirst (t: hasInfix ".${t}" value) null types; in if (type != null) then let - quadletBaseName = baseName type; + quadletName = extractQuadletReference type value; quadletsOfType = filterAttrs (n: v: v.quadletData.resourceType == type) cfg.internal.builtQuadlets; - in if (hasAttr quadletBaseName quadletsOfType) then + in if (hasAttr quadletName quadletsOfType) then [ - (replaceStrings [ quadletBaseName ] - [ "podman-${quadletBaseName}" ] value) + (replaceStrings [ quadletName ] [ "podman-${quadletName}" ] + value) ] else [ value ] else if ((hasInfix "/nix/store" value) == false && hasAttr value cfg.internal.builtQuadlets) then lib.warn '' - A value for Podman container '${name}' might use a reference to another quadlet: ${value}. + A value for Podman container '${name}' might use a reference to another quadlet: ${value}. Append the type '.${ cfg.internal.builtQuadlets.${value}.quadletData.resourceType }' to '${baseName value}' if this is intended. diff --git a/modules/services/podman-linux/install-quadlet.nix b/modules/services/podman-linux/install-quadlet.nix index 7a329b94e..a079b0be0 100644 --- a/modules/services/podman-linux/install-quadlet.nix +++ b/modules/services/podman-linux/install-quadlet.nix @@ -20,7 +20,7 @@ let unpackPhase = '' mkdir -p $out/quadlets ${concatStringsSep "\n" (map (v: - "ln -s ${v.out}/quadlets/${v.quadletData.serviceName}.${v.quadletData.resourceType} $out/quadlets") + "echo 'linking ${v.quadletData.serviceName}.${v.quadletData.resourceType}'; ln -s ${v.out}/quadlets/${v.quadletData.serviceName}.${v.quadletData.resourceType} $out/quadlets") quadlet.dependencies)} ''; @@ -87,7 +87,8 @@ in { (lib.hm.dag.entryAfter [ "reloadSystemd" ] activationCleanupScript); services.podman.internal.builtQuadlets = listToAttrs (map (pkg: { - name = removePrefix "podman-" pkg.passthru.quadletData.serviceName; + name = (removePrefix "podman-" pkg.passthru.quadletData.serviceName) + "." + + pkg.passthru.quadletData.resourceType; value = pkg; }) builtQuadlets); }; diff --git a/tests/modules/services/podman-linux/integration-container-expected.service b/tests/modules/services/podman-linux/integration-container-expected.service index f9a01d7a0..549bf7dc5 100644 --- a/tests/modules/services/podman-linux/integration-container-expected.service +++ b/tests/modules/services/podman-linux/integration-container-expected.service @@ -9,9 +9,9 @@ ContainerName=my-container Environment= Image=podman-my-img.image Label=nix.home-manager.managed=true -Network=podman-my-net.network +Network=podman-my-app.network Network=externalnet -Volume=podman-my-vol.volume:/data +Volume=podman-my-app.volume:/data [Install] WantedBy=default.target @@ -29,7 +29,7 @@ Delegate=yes Type=notify NotifyAccess=all SyslogIdentifier=%N -ExecStart=/nix/store/00000000000000000000000000000000-podman/bin/podman run --name my-container --cidfile=%t/%N.cid --replace --rm --cgroups=split --network my-net --network externalnet --sdnotify=conmon -d -v my-vol:/data --label nix.home-manager.managed=true docker.io/alpine:latest +ExecStart=/nix/store/00000000000000000000000000000000-podman/bin/podman run --name my-container --cidfile=%t/%N.cid --replace --rm --cgroups=split --network my-app --network externalnet --sdnotify=conmon -d -v my-app:/data --label nix.home-manager.managed=true docker.io/alpine:latest [Unit] Wants=podman-user-wait-network-online.service @@ -39,7 +39,7 @@ SourcePath=/nix/store/00000000000000000000000000000000-home-container-podman-my- Requires=podman-my-img-image.service After=podman-my-img-image.service RequiresMountsFor=%t/containers -Requires=podman-my-net-network.service -After=podman-my-net-network.service -Requires=podman-my-vol-volume.service -After=podman-my-vol-volume.service +Requires=podman-my-app-network.service +After=podman-my-app-network.service +Requires=podman-my-app-volume.service +After=podman-my-app-volume.service diff --git a/tests/modules/services/podman-linux/integration-network-expected.service b/tests/modules/services/podman-linux/integration-network-expected.service index 5af3bccea..e59ad393e 100644 --- a/tests/modules/services/podman-linux/integration-network-expected.service +++ b/tests/modules/services/podman-linux/integration-network-expected.service @@ -3,7 +3,7 @@ # Automatically generated by home-manager for podman network configuration # DO NOT EDIT THIS FILE DIRECTLY # -# my-net.network +# my-app.network [Install] WantedBy=default.target WantedBy=multi-user.target @@ -11,7 +11,7 @@ WantedBy=multi-user.target [X-Network] Gateway=192.168.123.1 Label=nix.home-manager.managed=true -NetworkName=my-net +NetworkName=my-app Subnet=192.168.123.0/24 [Service] @@ -19,7 +19,7 @@ Environment=PATH=/run/wrappers/bin:/usr/bin:/bin:/usr/sbin:/sbin:@shadow@/bin:/n ExecStartPre=/nix/store/00000000000000000000000000000000-await-podman-unshare RemainAfterExit=yes TimeoutStartSec=15 -ExecStart=/nix/store/00000000000000000000000000000000-podman/bin/podman network create --ignore --subnet 192.168.123.0/24 --gateway 192.168.123.1 --label nix.home-manager.managed=true my-net +ExecStart=/nix/store/00000000000000000000000000000000-podman/bin/podman network create --ignore --subnet 192.168.123.0/24 --gateway 192.168.123.1 --label nix.home-manager.managed=true my-app SyslogIdentifier=%N Type=oneshot @@ -27,6 +27,6 @@ Type=oneshot Wants=podman-user-wait-network-online.service After=podman-user-wait-network-online.service After=network.target -Description=Service for network my-net -SourcePath=/nix/store/00000000000000000000000000000000-home-network-podman-my-net/quadlets/podman-my-net.network +Description=Service for network my-app +SourcePath=/nix/store/00000000000000000000000000000000-home-network-podman-my-app/quadlets/podman-my-app.network RequiresMountsFor=%t/containers diff --git a/tests/modules/services/podman-linux/integration-volume-expected.service b/tests/modules/services/podman-linux/integration-volume-expected.service index da5d36968..9ee9bdb3a 100644 --- a/tests/modules/services/podman-linux/integration-volume-expected.service +++ b/tests/modules/services/podman-linux/integration-volume-expected.service @@ -3,7 +3,7 @@ # Automatically generated by home-manager for podman volume configuration # DO NOT EDIT THIS FILE DIRECTLY # -# my-vol.volume +# my-app.volume [Install] WantedBy=default.target WantedBy=multi-user.target @@ -13,15 +13,15 @@ Environment=PATH=/run/wrappers/bin:/usr/bin:/bin:/usr/sbin:/sbin:@shadow@/bin:/n ExecStartPre=/nix/store/00000000000000000000000000000000-await-podman-unshare RemainAfterExit=yes TimeoutStartSec=15 -ExecStart=/nix/store/00000000000000000000000000000000-podman/bin/podman volume create --ignore --opt copy --opt device=tmpfs --opt type=tmpfs --label nix.home-manager.managed=true --label nix.home-manager.preserve=false my-vol +ExecStart=/nix/store/00000000000000000000000000000000-podman/bin/podman volume create --ignore --opt copy --opt device=tmpfs --opt type=tmpfs --label nix.home-manager.managed=true --label nix.home-manager.preserve=false my-app SyslogIdentifier=%N Type=oneshot [Unit] Wants=podman-user-wait-network-online.service After=podman-user-wait-network-online.service -Description=Service for volume my-vol -SourcePath=/nix/store/00000000000000000000000000000000-home-volume-podman-my-vol/quadlets/podman-my-vol.volume +Description=Service for volume my-app +SourcePath=/nix/store/00000000000000000000000000000000-home-volume-podman-my-app/quadlets/podman-my-app.volume RequiresMountsFor=%t/containers [X-Volume] @@ -30,4 +30,4 @@ Device=tmpfs Label=nix.home-manager.managed=true Label=nix.home-manager.preserve=false Type=tmpfs -VolumeName=my-vol +VolumeName=my-app diff --git a/tests/modules/services/podman-linux/integration.nix b/tests/modules/services/podman-linux/integration.nix index 8e2e272fd..1c73093b4 100644 --- a/tests/modules/services/podman-linux/integration.nix +++ b/tests/modules/services/podman-linux/integration.nix @@ -18,17 +18,17 @@ containers = { "my-container" = { image = "my-img.image"; - network = [ "my-net.network" "externalnet" ]; - volumes = [ "my-vol.volume:/data" ]; + network = [ "my-app.network" "externalnet" ]; + volumes = [ "my-app.volume:/data" ]; }; "my-container-bld" = { image = "my-bld.build"; }; }; images."my-img" = { image = "docker.io/alpine:latest"; }; - networks."my-net" = { + networks."my-app" = { gateway = "192.168.123.1"; subnet = "192.168.123.0/24"; }; - volumes."my-vol" = { + volumes."my-app" = { device = "tmpfs"; preserve = false; type = "tmpfs"; @@ -41,8 +41,8 @@ containerFile=$configPath/podman-my-container.service containerBldFile=$configPath/podman-my-container-bld.service imageFile=$configPath/podman-my-img-image.service - networkFile=$configPath/podman-my-net-network.service - volumeFile=$configPath/podman-my-vol-volume.service + networkFile=$configPath/podman-my-app-network.service + volumeFile=$configPath/podman-my-app-volume.service assertFileExists $buildFile assertFileExists $containerFile assertFileExists $containerBldFile