query
On this page

fromSource

lib.fileset.fromSource

Docs pulled from | This Revision | 13 minutes ago


Create a file set with the same files as a lib.sources-based value. This does not import any of the files into the store.

This can be used to gradually migrate from lib.sources-based filtering to lib.fileset.

A file set can be turned back into a source using toSource.

File sets cannot represent empty directories. Turning the result of this function back into a source using toSource will therefore not preserve empty directories.

Inputs

source

1. Function argument

Type

fromSource :: SourceLike -> FileSet

Examples

lib.fileset.fromSource usage example

# There's no cleanSource-like function for file sets yet,
# but we can just convert cleanSource to a file set and use it that way
toSource {
  root = ./.;
  fileset = fromSource (lib.sources.cleanSource ./.);
}

# Keeping a previous sourceByRegex (which could be migrated to `lib.fileset.unions`),
# but removing a subdirectory using file set functions
difference
  (fromSource (lib.sources.sourceByRegex ./. [
    "^README\.md$"
    # This regex includes everything in ./doc
    "^doc(/.*)?$"
  ])
  ./doc/generated

# Use cleanSource, but limit it to only include ./Makefile and files under ./src
intersection
  (fromSource (lib.sources.cleanSource ./.))
  (unions [
    ./Makefile
    ./src
  ]);

Noogle detected

Implementation

The following is the current implementation of this function.

fromSource =
    source:
    let
      # This function uses `._isLibCleanSourceWith`, `.origSrc` and `.filter`,
      # which are technically internal to lib.sources,
      # but we'll allow this since both libraries are in the same code base
      # and this function is a bridge between them.
      isFiltered = source ? _isLibCleanSourceWith;
      path = if isFiltered then source.origSrc else source;
    in
    # We can only support sources created from paths
    if !isPath path then
      if isStringLike path then
        throw ''
          lib.fileset.fromSource: The source origin of the argument is a string-like value ("${toString path}"), but it should be a path instead.
              Sources created from paths in strings cannot be turned into file sets, use `lib.sources` or derivations instead.''
      else
        throw ''lib.fileset.fromSource: The source origin of the argument is of type ${typeOf path}, but it should be a path instead.''
    else if !pathExists path then
      throw ''lib.fileset.fromSource: The source origin (${toString path}) of the argument is a path that does not exist.''
    else if isFiltered then
      _fromSourceFilter path source.filter
    else
      # If there's no filter, no need to run the expensive conversion, all subpaths will be included
      _singleton path;