code: def clone_layer(layer, keep_selection=True):
      
      if is_vector_layer(layer):
          new_layer = QgsVectorLayer(
              layer.source(), layer.name(), layer.providerType())
          if keep_selection and layer.selectedFeatureCount() > 0:
              request = QgsFeatureRequest()
              request.setFilterFids(layer.selectedFeatureIds())
              request.setFlags(QgsFeatureRequest.NoGeometry)
              iterator = layer.getFeatures(request)
              new_layer.setSelectedFeatures([k.id() for k in iterator])
      else:
          new_layer = QgsRasterLayer(
              layer.source(), layer.name(), layer.providerType())
  
      new_layer.keywords = copy_layer_keywords(layer.keywords)
  
      return layer
docs: Duplicate the layer by taking the same source and copying keywords.
  
      :param keep_selection: If we should keep the selection. Default to true.
      :type keep_selection: bool
  
      :param layer: Layer to be duplicated.
      :type layer: QgsMapLayer
  
      :return: The new QgsMapLayer object.
      :rtype: QgsMapLayer