(nixpkgs: (final: prev: let
  libnvidia-container = ((
    final.callPackage (nixpkgs+"/pkgs/applications/virtualization/libnvidia-container") { }
  ).overrideAttrs (old: rec {
    pname = "libnvidia-container";
    version = "1.12.0";
    
    src = final.fetchFromGitHub {
      owner = "NVIDIA";
      repo = pname;
      rev = "v${version}";
      sha256 = "Ih8arSrBGGX44SiWcj61qV9z4DRrbi1J+3xxid2GupE=";
    };
    patches = [
      ./libnvidia-container/inline-c-struct.patch
      ./libnvidia-container/avoid-static-libtirpc-build.patch
      ./libnvidia-container/libnvc-ldconfig-and-path-fix.patch
    ];
    postInstall =
      let
        inherit (final.addOpenGLRunpath) driverLink;
        libraryPath = final.lib.makeLibraryPath [ "$out" driverLink "${driverLink}-32" ];
      in
        ''
            remove-references-to -t "${final.go}" $out/lib/libnvidia-container-go.so.${version}
            wrapProgram $out/bin/nvidia-container-cli --prefix LD_LIBRARY_PATH : ${libraryPath}
          '';
  }));
in {
  mkNvidiaContainerPkg = { name, containerRuntimePath, configTemplate, additionalPaths ? [] }:
    let
      nvidia-container-toolkit = final.callPackage (nixpkgs+"/pkgs/applications/virtualization/nvidia-container-toolkit") {
        inherit containerRuntimePath configTemplate libnvidia-container;
      };
    in final.symlinkJoin {
      inherit name;
      paths = [
        libnvidia-container
        nvidia-container-toolkit
      ] ++ additionalPaths;
    };
  nvidia-podman = final.mkNvidiaContainerPkg {
    name = "nvidia-podman";
    containerRuntimePath = "${final.runc}/bin/runc";
    configTemplate = ./libnvidia-container/config.tml;
  };
}))
