github.com/anchore/syft@v1.38.2/syft/pkg/cataloger/nix/cataloger.go (about)

     1  package nix
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/anchore/syft/syft/artifact"
     8  	"github.com/anchore/syft/syft/file"
     9  	"github.com/anchore/syft/syft/pkg"
    10  )
    11  
    12  type Config struct {
    13  	// CaptureOwnedFiles determines whether to record the list of files owned by each Nix package discovered in the store. Recording owned files provides more detailed information but increases processing time and memory usage.
    14  	// app-config: nix.capture-owned-files
    15  	CaptureOwnedFiles bool `json:"capture-owned-files" yaml:"capture-owned-files" mapstructure:"capture-owned-files"`
    16  }
    17  
    18  func (c Config) WithCaptureOwnedFiles(set bool) Config {
    19  	c.CaptureOwnedFiles = set
    20  	return c
    21  }
    22  
    23  func DefaultConfig() Config {
    24  	return Config{
    25  		CaptureOwnedFiles: false,
    26  	}
    27  }
    28  
    29  // cataloger finds package outputs installed in the Nix store location (/nix/store/*) or in the internal nix database (/nix/var/nix/db/db.sqlite).
    30  type cataloger struct {
    31  	dbParser       dbCataloger
    32  	storeCataloger storeCataloger
    33  }
    34  
    35  func NewCataloger(cfg Config) pkg.Cataloger {
    36  	name := "nix-cataloger"
    37  	return cataloger{
    38  		dbParser:       newDBCataloger(cfg, name),
    39  		storeCataloger: newStoreCataloger(cfg, name),
    40  	}
    41  }
    42  
    43  func (c cataloger) Name() string {
    44  	return c.dbParser.catalogerName
    45  }
    46  
    47  func (c cataloger) Catalog(ctx context.Context, resolver file.Resolver) ([]pkg.Package, []artifact.Relationship, error) {
    48  	// always try the DB cataloger first (based off of information recorded by actions taken by nix tooling)
    49  	pkgs, rels, err := c.dbParser.catalog(resolver)
    50  	if err != nil {
    51  		return nil, nil, fmt.Errorf("failed to catalog nix packages from database: %w", err)
    52  	}
    53  	if len(pkgs) > 0 {
    54  		return pkgs, rels, nil
    55  	}
    56  
    57  	// there are no results from the DB cataloger, then use the store path cataloger (not as accurate / detailed in information)
    58  	return c.storeCataloger.Catalog(ctx, resolver)
    59  }