2023-08-17 00:55:32 +02:00
|
|
|
{ lib ? import ../. }:
|
|
|
|
let
|
|
|
|
|
|
|
|
inherit (builtins)
|
|
|
|
isAttrs
|
|
|
|
isPath
|
|
|
|
isString
|
|
|
|
pathExists
|
|
|
|
readDir
|
2023-09-21 01:00:59 +02:00
|
|
|
seq
|
2023-08-17 00:55:32 +02:00
|
|
|
split
|
2023-09-21 01:00:59 +02:00
|
|
|
trace
|
|
|
|
typeOf
|
2023-08-17 00:55:32 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
inherit (lib.attrsets)
|
2023-09-21 01:00:59 +02:00
|
|
|
attrNames
|
2023-08-17 00:55:32 +02:00
|
|
|
attrValues
|
|
|
|
mapAttrs
|
2023-09-14 23:34:21 +02:00
|
|
|
setAttrByPath
|
2023-09-14 21:41:03 +02:00
|
|
|
zipAttrsWith
|
2023-08-17 00:55:32 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
inherit (lib.filesystem)
|
|
|
|
pathType
|
|
|
|
;
|
|
|
|
|
|
|
|
inherit (lib.lists)
|
|
|
|
all
|
2023-09-13 23:29:28 +02:00
|
|
|
commonPrefix
|
|
|
|
drop
|
2023-08-17 00:55:32 +02:00
|
|
|
elemAt
|
2023-09-14 21:41:03 +02:00
|
|
|
filter
|
2023-09-26 00:26:00 +02:00
|
|
|
findFirst
|
2023-09-13 23:29:28 +02:00
|
|
|
findFirstIndex
|
2023-09-13 18:50:45 +02:00
|
|
|
foldl'
|
2023-09-13 23:29:28 +02:00
|
|
|
head
|
2023-08-17 00:55:32 +02:00
|
|
|
length
|
2023-09-13 18:50:45 +02:00
|
|
|
sublist
|
2023-09-13 23:29:28 +02:00
|
|
|
tail
|
2023-08-17 00:55:32 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
inherit (lib.path)
|
|
|
|
append
|
|
|
|
splitRoot
|
|
|
|
;
|
|
|
|
|
|
|
|
inherit (lib.path.subpath)
|
|
|
|
components
|
2023-09-13 23:29:28 +02:00
|
|
|
join
|
2023-08-17 00:55:32 +02:00
|
|
|
;
|
|
|
|
|
|
|
|
inherit (lib.strings)
|
|
|
|
isStringLike
|
|
|
|
concatStringsSep
|
|
|
|
substring
|
|
|
|
stringLength
|
|
|
|
;
|
|
|
|
|
|
|
|
in
|
|
|
|
# Rare case of justified usage of rec:
|
|
|
|
# - This file is internal, so the return value doesn't matter, no need to make things overridable
|
|
|
|
# - The functions depend on each other
|
|
|
|
# - We want to expose all of these functions for easy testing
|
|
|
|
rec {
|
|
|
|
|
|
|
|
# If you change the internal representation, make sure to:
|
2023-09-13 18:50:45 +02:00
|
|
|
# - Increment this version
|
|
|
|
# - Add an additional migration function below
|
2023-08-17 00:55:32 +02:00
|
|
|
# - Update the description of the internal representation in ./README.md
|
2023-09-26 00:26:00 +02:00
|
|
|
_currentVersion = 3;
|
2023-09-13 18:50:45 +02:00
|
|
|
|
|
|
|
# Migrations between versions. The 0th element converts from v0 to v1, and so on
|
|
|
|
migrations = [
|
|
|
|
# Convert v0 into v1: Add the _internalBase{Root,Components} attributes
|
|
|
|
(
|
|
|
|
filesetV0:
|
|
|
|
let
|
|
|
|
parts = splitRoot filesetV0._internalBase;
|
|
|
|
in
|
|
|
|
filesetV0 // {
|
|
|
|
_internalVersion = 1;
|
|
|
|
_internalBaseRoot = parts.root;
|
|
|
|
_internalBaseComponents = components parts.subpath;
|
|
|
|
}
|
|
|
|
)
|
2023-09-14 23:34:21 +02:00
|
|
|
|
|
|
|
# Convert v1 into v2: filesetTree's can now also omit attributes to signal paths not being included
|
|
|
|
(
|
|
|
|
filesetV1:
|
|
|
|
# This change is backwards compatible (but not forwards compatible, so we still need a new version)
|
|
|
|
filesetV1 // {
|
|
|
|
_internalVersion = 2;
|
|
|
|
}
|
|
|
|
)
|
2023-09-26 00:26:00 +02:00
|
|
|
|
|
|
|
# Convert v2 into v3: filesetTree's now have a representation for an empty file set without a base path
|
|
|
|
(
|
|
|
|
filesetV2:
|
|
|
|
filesetV2 // {
|
|
|
|
# All v1 file sets are not the new empty file set
|
|
|
|
_internalIsEmptyWithoutBase = false;
|
|
|
|
_internalVersion = 3;
|
|
|
|
}
|
|
|
|
)
|
2023-09-13 18:50:45 +02:00
|
|
|
];
|
2023-08-17 00:55:32 +02:00
|
|
|
|
2023-09-26 00:26:00 +02:00
|
|
|
_noEvalMessage = ''
|
2023-09-26 19:12:58 +02:00
|
|
|
lib.fileset: Directly evaluating a file set is not supported.
|
|
|
|
To turn it into a usable source, use `lib.fileset.toSource`.
|
|
|
|
To pretty-print the contents, use `lib.fileset.trace` or `lib.fileset.traceVal`.'';
|
2023-09-26 00:26:00 +02:00
|
|
|
|
|
|
|
# The empty file set without a base path
|
|
|
|
_emptyWithoutBase = {
|
|
|
|
_type = "fileset";
|
|
|
|
|
|
|
|
_internalVersion = _currentVersion;
|
|
|
|
|
|
|
|
# The one and only!
|
|
|
|
_internalIsEmptyWithoutBase = true;
|
|
|
|
|
2023-09-20 21:08:13 +02:00
|
|
|
# Due to alphabetical ordering, this is evaluated last,
|
|
|
|
# which makes the nix repl output nicer than if it would be ordered first.
|
|
|
|
# It also allows evaluating it strictly up to this error, which could be useful
|
|
|
|
_noEval = throw _noEvalMessage;
|
2023-09-26 00:26:00 +02:00
|
|
|
};
|
|
|
|
|
2023-08-17 00:55:32 +02:00
|
|
|
# Create a fileset, see ./README.md#fileset
|
|
|
|
# Type: path -> filesetTree -> fileset
|
2023-09-13 18:50:45 +02:00
|
|
|
_create = base: tree:
|
|
|
|
let
|
|
|
|
# Decompose the base into its components
|
|
|
|
# See ../path/README.md for why we're not just using `toString`
|
|
|
|
parts = splitRoot base;
|
|
|
|
in
|
|
|
|
{
|
|
|
|
_type = "fileset";
|
2023-08-17 00:55:32 +02:00
|
|
|
|
2023-09-13 18:50:45 +02:00
|
|
|
_internalVersion = _currentVersion;
|
2023-09-26 00:26:00 +02:00
|
|
|
|
|
|
|
_internalIsEmptyWithoutBase = false;
|
2023-09-13 18:50:45 +02:00
|
|
|
_internalBase = base;
|
|
|
|
_internalBaseRoot = parts.root;
|
|
|
|
_internalBaseComponents = components parts.subpath;
|
|
|
|
_internalTree = tree;
|
2023-08-17 00:55:32 +02:00
|
|
|
|
2023-09-20 21:08:13 +02:00
|
|
|
# Due to alphabetical ordering, this is evaluated last,
|
|
|
|
# which makes the nix repl output nicer than if it would be ordered first.
|
|
|
|
# It also allows evaluating it strictly up to this error, which could be useful
|
|
|
|
_noEval = throw _noEvalMessage;
|
2023-09-13 18:50:45 +02:00
|
|
|
};
|
2023-08-17 00:55:32 +02:00
|
|
|
|
|
|
|
# Coerce a value to a fileset, erroring when the value cannot be coerced.
|
|
|
|
# The string gives the context for error messages.
|
2023-09-13 22:27:04 +02:00
|
|
|
# Type: String -> (fileset | Path) -> fileset
|
2023-08-17 00:55:32 +02:00
|
|
|
_coerce = context: value:
|
|
|
|
if value._type or "" == "fileset" then
|
|
|
|
if value._internalVersion > _currentVersion then
|
|
|
|
throw ''
|
|
|
|
${context} is a file set created from a future version of the file set library with a different internal representation:
|
|
|
|
- Internal version of the file set: ${toString value._internalVersion}
|
|
|
|
- Internal version of the library: ${toString _currentVersion}
|
|
|
|
Make sure to update your Nixpkgs to have a newer version of `lib.fileset`.''
|
2023-09-13 18:50:45 +02:00
|
|
|
else if value._internalVersion < _currentVersion then
|
|
|
|
let
|
|
|
|
# Get all the migration functions necessary to convert from the old to the current version
|
|
|
|
migrationsToApply = sublist value._internalVersion (_currentVersion - value._internalVersion) migrations;
|
|
|
|
in
|
|
|
|
foldl' (value: migration: migration value) value migrationsToApply
|
2023-08-17 00:55:32 +02:00
|
|
|
else
|
|
|
|
value
|
|
|
|
else if ! isPath value then
|
|
|
|
if isStringLike value then
|
|
|
|
throw ''
|
2023-10-16 15:49:53 +02:00
|
|
|
${context} ("${toString value}") is a string-like value, but it should be a file set or a path instead.
|
2023-08-17 00:55:32 +02:00
|
|
|
Paths represented as strings are not supported by `lib.fileset`, use `lib.sources` or derivations instead.''
|
|
|
|
else
|
|
|
|
throw ''
|
2023-10-16 15:49:53 +02:00
|
|
|
${context} is of type ${typeOf value}, but it should be a file set or a path instead.''
|
2023-08-17 00:55:32 +02:00
|
|
|
else if ! pathExists value then
|
|
|
|
throw ''
|
2023-09-15 00:08:04 +02:00
|
|
|
${context} (${toString value}) does not exist.''
|
2023-08-17 00:55:32 +02:00
|
|
|
else
|
|
|
|
_singleton value;
|
|
|
|
|
2023-09-13 23:29:28 +02:00
|
|
|
# Coerce many values to filesets, erroring when any value cannot be coerced,
|
|
|
|
# or if the filesystem root of the values doesn't match.
|
|
|
|
# Type: String -> [ { context :: String, value :: fileset | Path } ] -> [ fileset ]
|
|
|
|
_coerceMany = functionContext: list:
|
|
|
|
let
|
|
|
|
filesets = map ({ context, value }:
|
|
|
|
_coerce "${functionContext}: ${context}" value
|
|
|
|
) list;
|
|
|
|
|
2023-09-26 00:26:00 +02:00
|
|
|
# Find the first value with a base, there may be none!
|
|
|
|
firstWithBase = findFirst (fileset: ! fileset._internalIsEmptyWithoutBase) null filesets;
|
|
|
|
# This value is only accessed if first != null
|
|
|
|
firstBaseRoot = firstWithBase._internalBaseRoot;
|
2023-09-13 23:29:28 +02:00
|
|
|
|
|
|
|
# Finds the first element with a filesystem root different than the first element, if any
|
|
|
|
differentIndex = findFirstIndex (fileset:
|
2023-09-26 00:26:00 +02:00
|
|
|
# The empty value without a base doesn't have a base path
|
|
|
|
! fileset._internalIsEmptyWithoutBase
|
|
|
|
&& firstBaseRoot != fileset._internalBaseRoot
|
2023-09-13 23:29:28 +02:00
|
|
|
) null filesets;
|
|
|
|
in
|
2023-09-26 00:26:00 +02:00
|
|
|
# Only evaluates `differentIndex` if there are any elements with a base
|
|
|
|
if firstWithBase != null && differentIndex != null then
|
2023-09-13 23:29:28 +02:00
|
|
|
throw ''
|
|
|
|
${functionContext}: Filesystem roots are not the same:
|
|
|
|
${(head list).context}: root "${toString firstBaseRoot}"
|
|
|
|
${(elemAt list differentIndex).context}: root "${toString (elemAt filesets differentIndex)._internalBaseRoot}"
|
|
|
|
Different roots are not supported.''
|
|
|
|
else
|
|
|
|
filesets;
|
|
|
|
|
2023-08-17 00:55:32 +02:00
|
|
|
# Create a file set from a path.
|
|
|
|
# Type: Path -> fileset
|
|
|
|
_singleton = path:
|
|
|
|
let
|
|
|
|
type = pathType path;
|
|
|
|
in
|
|
|
|
if type == "directory" then
|
|
|
|
_create path type
|
|
|
|
else
|
|
|
|
# This turns a file path ./default.nix into a fileset with
|
|
|
|
# - _internalBase: ./.
|
|
|
|
# - _internalTree: {
|
|
|
|
# "default.nix" = <type>;
|
|
|
|
# }
|
|
|
|
# See ./README.md#single-files
|
|
|
|
_create (dirOf path)
|
2023-09-14 23:34:21 +02:00
|
|
|
{
|
|
|
|
${baseNameOf path} = type;
|
|
|
|
};
|
2023-08-17 00:55:32 +02:00
|
|
|
|
2023-09-14 23:34:21 +02:00
|
|
|
# Expand a directory representation to an equivalent one in attribute set form.
|
|
|
|
# All directory entries are included in the result.
|
2023-08-17 00:55:32 +02:00
|
|
|
# Type: Path -> filesetTree -> { <name> = filesetTree; }
|
|
|
|
_directoryEntries = path: value:
|
2023-09-14 23:34:21 +02:00
|
|
|
if value == "directory" then
|
|
|
|
readDir path
|
2023-08-17 00:55:32 +02:00
|
|
|
else
|
2023-09-14 23:34:21 +02:00
|
|
|
# Set all entries not present to null
|
|
|
|
mapAttrs (name: value: null) (readDir path)
|
|
|
|
// value;
|
2023-08-17 00:55:32 +02:00
|
|
|
|
|
|
|
/*
|
2023-09-21 01:00:59 +02:00
|
|
|
A normalisation of a filesetTree suitable filtering with `builtins.path`:
|
2023-09-21 00:59:09 +02:00
|
|
|
- Replace all directories that have no files with `null`.
|
2023-08-17 00:55:32 +02:00
|
|
|
This removes directories that would be empty
|
2023-09-21 00:59:09 +02:00
|
|
|
- Replace all directories with all files with `"directory"`.
|
2023-08-17 00:55:32 +02:00
|
|
|
This speeds up the source filter function
|
|
|
|
|
|
|
|
Note that this function is strict, it evaluates the entire tree
|
|
|
|
|
|
|
|
Type: Path -> filesetTree -> filesetTree
|
|
|
|
*/
|
2023-09-21 00:59:09 +02:00
|
|
|
_normaliseTreeFilter = path: tree:
|
2023-08-17 00:55:32 +02:00
|
|
|
if tree == "directory" || isAttrs tree then
|
|
|
|
let
|
|
|
|
entries = _directoryEntries path tree;
|
2023-09-21 00:59:09 +02:00
|
|
|
normalisedSubtrees = mapAttrs (name: _normaliseTreeFilter (path + "/${name}")) entries;
|
|
|
|
subtreeValues = attrValues normalisedSubtrees;
|
2023-08-17 00:55:32 +02:00
|
|
|
in
|
|
|
|
# This triggers either when all files in a directory are filtered out
|
|
|
|
# Or when the directory doesn't contain any files at all
|
|
|
|
if all isNull subtreeValues then
|
|
|
|
null
|
|
|
|
# Triggers when we have the same as a `readDir path`, so we can turn it back into an equivalent "directory".
|
|
|
|
else if all isString subtreeValues then
|
|
|
|
"directory"
|
|
|
|
else
|
2023-09-21 00:59:09 +02:00
|
|
|
normalisedSubtrees
|
2023-08-17 00:55:32 +02:00
|
|
|
else
|
|
|
|
tree;
|
|
|
|
|
2023-09-21 01:00:59 +02:00
|
|
|
/*
|
|
|
|
A minimal normalisation of a filesetTree, intended for pretty-printing:
|
|
|
|
- If all children of a path are recursively included or empty directories, the path itself is also recursively included
|
|
|
|
- If all children of a path are fully excluded or empty directories, the path itself is an empty directory
|
|
|
|
- Other empty directories are represented with the special "emptyDir" string
|
|
|
|
While these could be replaced with `null`, that would take another mapAttrs
|
|
|
|
|
|
|
|
Note that this function is partially lazy.
|
|
|
|
|
|
|
|
Type: Path -> filesetTree -> filesetTree (with "emptyDir"'s)
|
|
|
|
*/
|
|
|
|
_normaliseTreeMinimal = path: tree:
|
|
|
|
if tree == "directory" || isAttrs tree then
|
|
|
|
let
|
|
|
|
entries = _directoryEntries path tree;
|
|
|
|
normalisedSubtrees = mapAttrs (name: _normaliseTreeMinimal (path + "/${name}")) entries;
|
|
|
|
subtreeValues = attrValues normalisedSubtrees;
|
|
|
|
in
|
|
|
|
# If there are no entries, or all entries are empty directories, return "emptyDir".
|
|
|
|
# After this branch we know that there's at least one file
|
|
|
|
if all (value: value == "emptyDir") subtreeValues then
|
|
|
|
"emptyDir"
|
|
|
|
|
|
|
|
# If all subtrees are fully included or empty directories
|
|
|
|
# (both of which are coincidentally represented as strings), return "directory".
|
|
|
|
# This takes advantage of the fact that empty directories can be represented as included directories.
|
|
|
|
# Note that the tree == "directory" check allows avoiding recursion
|
|
|
|
else if tree == "directory" || all (value: isString value) subtreeValues then
|
|
|
|
"directory"
|
|
|
|
|
|
|
|
# If all subtrees are fully excluded or empty directories, return null.
|
|
|
|
# This takes advantage of the fact that empty directories can be represented as excluded directories
|
|
|
|
else if all (value: isNull value || value == "emptyDir") subtreeValues then
|
|
|
|
null
|
|
|
|
|
|
|
|
# Mix of included and excluded entries
|
|
|
|
else
|
|
|
|
normalisedSubtrees
|
|
|
|
else
|
|
|
|
tree;
|
|
|
|
|
|
|
|
# Trace a filesetTree in a pretty way when the resulting value is evaluated.
|
|
|
|
# This can handle both normal filesetTree's, and ones returned from _normaliseTreeMinimal
|
|
|
|
# Type: Path -> filesetTree (with "emptyDir"'s) -> Null
|
|
|
|
_printMinimalTree = base: tree:
|
|
|
|
let
|
|
|
|
treeSuffix = tree:
|
|
|
|
if isAttrs tree then
|
|
|
|
""
|
|
|
|
else if tree == "directory" then
|
|
|
|
" (all files in directory)"
|
|
|
|
else
|
|
|
|
# This does "leak" the file type strings of the internal representation,
|
|
|
|
# but this is the main reason these file type strings even are in the representation!
|
|
|
|
# TODO: Consider removing that information from the internal representation for performance.
|
|
|
|
# The file types can still be printed by querying them only during tracing
|
|
|
|
" (${tree})";
|
|
|
|
|
|
|
|
# Only for attribute set trees
|
|
|
|
traceTreeAttrs = prevLine: indent: tree:
|
|
|
|
foldl' (prevLine: name:
|
|
|
|
let
|
|
|
|
subtree = tree.${name};
|
|
|
|
|
|
|
|
# Evaluating this prints the line for this subtree
|
|
|
|
thisLine =
|
|
|
|
trace "${indent}- ${name}${treeSuffix subtree}" prevLine;
|
|
|
|
in
|
|
|
|
if subtree == null || subtree == "emptyDir" then
|
|
|
|
# Don't print anything at all if this subtree is empty
|
|
|
|
prevLine
|
|
|
|
else if isAttrs subtree then
|
|
|
|
# A directory with explicit entries
|
|
|
|
# Do print this node, but also recurse
|
|
|
|
traceTreeAttrs thisLine "${indent} " subtree
|
|
|
|
else
|
|
|
|
# Either a file, or a recursively included directory
|
|
|
|
# Do print this node but no further recursion needed
|
|
|
|
thisLine
|
|
|
|
) prevLine (attrNames tree);
|
|
|
|
|
|
|
|
# Evaluating this will print the first line
|
|
|
|
firstLine =
|
|
|
|
if tree == null || tree == "emptyDir" then
|
|
|
|
trace "(empty)" null
|
|
|
|
else
|
|
|
|
trace "${toString base}${treeSuffix tree}" null;
|
|
|
|
in
|
|
|
|
if isAttrs tree then
|
|
|
|
traceTreeAttrs firstLine "" tree
|
|
|
|
else
|
|
|
|
firstLine;
|
|
|
|
|
|
|
|
# Pretty-print a file set in a pretty way when the resulting value is evaluated
|
|
|
|
# Type: fileset -> Null
|
|
|
|
_printFileset = fileset:
|
|
|
|
if fileset._internalIsEmptyWithoutBase then
|
|
|
|
trace "(empty)" null
|
|
|
|
else
|
|
|
|
_printMinimalTree fileset._internalBase
|
|
|
|
(_normaliseTreeMinimal fileset._internalBase fileset._internalTree);
|
|
|
|
|
2023-08-17 00:55:32 +02:00
|
|
|
# Turn a fileset into a source filter function suitable for `builtins.path`
|
|
|
|
# Only directories recursively containing at least one files are recursed into
|
|
|
|
# Type: Path -> fileset -> (String -> String -> Bool)
|
|
|
|
_toSourceFilter = fileset:
|
|
|
|
let
|
|
|
|
# Simplify the tree, necessary to make sure all empty directories are null
|
|
|
|
# which has the effect that they aren't included in the result
|
2023-09-21 00:59:09 +02:00
|
|
|
tree = _normaliseTreeFilter fileset._internalBase fileset._internalTree;
|
2023-08-17 00:55:32 +02:00
|
|
|
|
|
|
|
# The base path as a string with a single trailing slash
|
|
|
|
baseString =
|
2023-09-13 18:50:45 +02:00
|
|
|
if fileset._internalBaseComponents == [] then
|
2023-08-17 00:55:32 +02:00
|
|
|
# Need to handle the filesystem root specially
|
|
|
|
"/"
|
|
|
|
else
|
2023-09-13 18:50:45 +02:00
|
|
|
"/" + concatStringsSep "/" fileset._internalBaseComponents + "/";
|
2023-08-17 00:55:32 +02:00
|
|
|
|
|
|
|
baseLength = stringLength baseString;
|
|
|
|
|
|
|
|
# Check whether a list of path components under the base path exists in the tree.
|
|
|
|
# This function is called often, so it should be fast.
|
|
|
|
# Type: [ String ] -> Bool
|
|
|
|
inTree = components:
|
|
|
|
let
|
|
|
|
recurse = index: localTree:
|
|
|
|
if isAttrs localTree then
|
|
|
|
# We have an attribute set, meaning this is a directory with at least one file
|
|
|
|
if index >= length components then
|
|
|
|
# The path may have no more components though, meaning the filter is running on the directory itself,
|
|
|
|
# so we always include it, again because there's at least one file in it.
|
|
|
|
true
|
|
|
|
else
|
|
|
|
# If we do have more components, the filter runs on some entry inside this directory, so we need to recurse
|
|
|
|
# We do +2 because builtins.split is an interleaved list of the inbetweens and the matches
|
|
|
|
recurse (index + 2) localTree.${elemAt components index}
|
|
|
|
else
|
|
|
|
# If it's not an attribute set it can only be either null (in which case it's not included)
|
|
|
|
# or a string ("directory" or "regular", etc.) in which case it's included
|
|
|
|
localTree != null;
|
|
|
|
in recurse 0 tree;
|
|
|
|
|
|
|
|
# Filter suited when there's no files
|
|
|
|
empty = _: _: false;
|
|
|
|
|
|
|
|
# Filter suited when there's some files
|
|
|
|
# This can't be used for when there's no files, because the base directory is always included
|
|
|
|
nonEmpty =
|
2023-10-26 01:56:42 +02:00
|
|
|
path: type:
|
2023-08-17 00:55:32 +02:00
|
|
|
let
|
|
|
|
# Add a slash to the path string, turning "/foo" to "/foo/",
|
|
|
|
# making sure to not have any false prefix matches below.
|
|
|
|
# Note that this would produce "//" for "/",
|
|
|
|
# but builtins.path doesn't call the filter function on the `path` argument itself,
|
|
|
|
# meaning this function can never receive "/" as an argument
|
|
|
|
pathSlash = path + "/";
|
2023-10-26 01:56:42 +02:00
|
|
|
|
|
|
|
include =
|
|
|
|
# Same as `hasPrefix pathSlash baseString`, but more efficient.
|
|
|
|
# With base /foo/bar we need to include /foo:
|
|
|
|
# hasPrefix "/foo/" "/foo/bar/"
|
|
|
|
if substring 0 (stringLength pathSlash) baseString == pathSlash then
|
|
|
|
true
|
|
|
|
# Same as `! hasPrefix baseString pathSlash`, but more efficient.
|
|
|
|
# With base /foo/bar we need to exclude /baz
|
|
|
|
# ! hasPrefix "/baz/" "/foo/bar/"
|
|
|
|
else if substring 0 baseLength pathSlash != baseString then
|
|
|
|
false
|
|
|
|
else
|
|
|
|
# Same as `removePrefix baseString path`, but more efficient.
|
|
|
|
# From the above code we know that hasPrefix baseString pathSlash holds, so this is safe.
|
|
|
|
# We don't use pathSlash here because we only needed the trailing slash for the prefix matching.
|
|
|
|
# With base /foo and path /foo/bar/baz this gives
|
|
|
|
# inTree (split "/" (removePrefix "/foo/" "/foo/bar/baz"))
|
|
|
|
# == inTree (split "/" "bar/baz")
|
|
|
|
# == inTree [ "bar" "baz" ]
|
|
|
|
inTree (split "/" (substring baseLength (-1) path));
|
2023-08-17 00:55:32 +02:00
|
|
|
in
|
2023-10-26 01:56:42 +02:00
|
|
|
# This relies on the fact that Nix only distinguishes path types "directory", "regular", "symlink" and "unknown",
|
|
|
|
# so everything except "unknown" is allowed, seems reasonable to rely on that
|
|
|
|
if include && type == "unknown" then
|
|
|
|
throw ''
|
|
|
|
lib.fileset.toSource: `fileset` contains a file that cannot be added to the store: ${path}
|
|
|
|
This file is neither a regular file nor a symlink, the only file types supported by the Nix store.
|
|
|
|
Therefore the file set cannot be added to the Nix store as is. Make sure to not include that file to avoid this error.''
|
2023-08-17 00:55:32 +02:00
|
|
|
else
|
2023-10-26 01:56:42 +02:00
|
|
|
include;
|
2023-08-17 00:55:32 +02:00
|
|
|
in
|
|
|
|
# Special case because the code below assumes that the _internalBase is always included in the result
|
|
|
|
# which shouldn't be done when we have no files at all in the base
|
2023-09-13 22:26:16 +02:00
|
|
|
# This also forces the tree before returning the filter, leads to earlier error messages
|
2023-09-26 00:26:00 +02:00
|
|
|
if fileset._internalIsEmptyWithoutBase || tree == null then
|
2023-08-17 00:55:32 +02:00
|
|
|
empty
|
|
|
|
else
|
|
|
|
nonEmpty;
|
|
|
|
|
2023-10-04 23:19:56 +02:00
|
|
|
# Transforms the filesetTree of a file set to a shorter base path, e.g.
|
|
|
|
# _shortenTreeBase [ "foo" ] (_create /foo/bar null)
|
|
|
|
# => { bar = null; }
|
|
|
|
_shortenTreeBase = targetBaseComponents: fileset:
|
|
|
|
let
|
|
|
|
recurse = index:
|
|
|
|
# If we haven't reached the required depth yet
|
|
|
|
if index < length fileset._internalBaseComponents then
|
|
|
|
# Create an attribute set and recurse as the value, this can be lazily evaluated this way
|
|
|
|
{ ${elemAt fileset._internalBaseComponents index} = recurse (index + 1); }
|
|
|
|
else
|
|
|
|
# Otherwise we reached the appropriate depth, here's the original tree
|
|
|
|
fileset._internalTree;
|
|
|
|
in
|
|
|
|
recurse (length targetBaseComponents);
|
|
|
|
|
2023-09-26 02:10:46 +02:00
|
|
|
# Transforms the filesetTree of a file set to a longer base path, e.g.
|
|
|
|
# _lengthenTreeBase [ "foo" "bar" ] (_create /foo { bar.baz = "regular"; })
|
|
|
|
# => { baz = "regular"; }
|
|
|
|
_lengthenTreeBase = targetBaseComponents: fileset:
|
|
|
|
let
|
|
|
|
recurse = index: tree:
|
|
|
|
# If the filesetTree is an attribute set and we haven't reached the required depth yet
|
|
|
|
if isAttrs tree && index < length targetBaseComponents then
|
|
|
|
# Recurse with the tree under the right component (which might not exist)
|
|
|
|
recurse (index + 1) (tree.${elemAt targetBaseComponents index} or null)
|
|
|
|
else
|
|
|
|
# For all values here we can just return the tree itself:
|
|
|
|
# tree == null -> the result is also null, everything is excluded
|
|
|
|
# tree == "directory" -> the result is also "directory",
|
|
|
|
# because the base path is always a directory and everything is included
|
|
|
|
# isAttrs tree -> the result is `tree`
|
|
|
|
# because we don't need to recurse any more since `index == length longestBaseComponents`
|
|
|
|
tree;
|
|
|
|
in
|
|
|
|
recurse (length fileset._internalBaseComponents) fileset._internalTree;
|
|
|
|
|
2023-09-13 23:29:28 +02:00
|
|
|
# Computes the union of a list of filesets.
|
|
|
|
# The filesets must already be coerced and validated to be in the same filesystem root
|
|
|
|
# Type: [ Fileset ] -> Fileset
|
|
|
|
_unionMany = filesets:
|
|
|
|
let
|
2023-09-26 00:26:00 +02:00
|
|
|
# All filesets that have a base, aka not the ones that are the empty value without a base
|
|
|
|
filesetsWithBase = filter (fileset: ! fileset._internalIsEmptyWithoutBase) filesets;
|
|
|
|
|
|
|
|
# The first fileset that has a base.
|
|
|
|
# This value is only accessed if there are at all.
|
|
|
|
firstWithBase = head filesetsWithBase;
|
2023-09-13 23:29:28 +02:00
|
|
|
|
|
|
|
# To be able to union filesetTree's together, they need to have the same base path.
|
|
|
|
# Base paths can be unioned by taking their common prefix,
|
|
|
|
# e.g. such that `union /foo/bar /foo/baz` has the base path `/foo`
|
|
|
|
|
2023-09-14 21:41:03 +02:00
|
|
|
# A list of path components common to all base paths.
|
|
|
|
# Note that commonPrefix can only be fully evaluated,
|
|
|
|
# so this cannot cause a stack overflow due to a build-up of unevaluated thunks.
|
2023-09-13 23:29:28 +02:00
|
|
|
commonBaseComponents = foldl'
|
|
|
|
(components: el: commonPrefix components el._internalBaseComponents)
|
2023-09-26 00:26:00 +02:00
|
|
|
firstWithBase._internalBaseComponents
|
2023-09-13 23:29:28 +02:00
|
|
|
# We could also not do the `tail` here to avoid a list allocation,
|
|
|
|
# but then we'd have to pay for a potentially expensive
|
|
|
|
# but unnecessary `commonPrefix` call
|
2023-09-26 00:26:00 +02:00
|
|
|
(tail filesetsWithBase);
|
2023-09-13 23:29:28 +02:00
|
|
|
|
|
|
|
# The common base path assembled from a filesystem root and the common components
|
2023-09-26 00:26:00 +02:00
|
|
|
commonBase = append firstWithBase._internalBaseRoot (join commonBaseComponents);
|
2023-09-13 23:29:28 +02:00
|
|
|
|
|
|
|
# A list of filesetTree's that all have the same base path
|
|
|
|
# This is achieved by nesting the trees into the components they have over the common base path
|
|
|
|
# E.g. `union /foo/bar /foo/baz` has the base path /foo
|
|
|
|
# So the tree under `/foo/bar` gets nested under `{ bar = ...; ... }`,
|
|
|
|
# while the tree under `/foo/baz` gets nested under `{ baz = ...; ... }`
|
|
|
|
# Therefore allowing combined operations over them.
|
2023-10-04 23:19:56 +02:00
|
|
|
trees = map (_shortenTreeBase commonBaseComponents) filesetsWithBase;
|
2023-09-13 23:29:28 +02:00
|
|
|
|
|
|
|
# Folds all trees together into a single one using _unionTree
|
2023-09-14 21:41:03 +02:00
|
|
|
# We do not use a fold here because it would cause a thunk build-up
|
|
|
|
# which could cause a stack overflow for a large number of trees
|
|
|
|
resultTree = _unionTrees trees;
|
2023-09-13 23:29:28 +02:00
|
|
|
in
|
2023-09-26 00:26:00 +02:00
|
|
|
# If there's no values with a base, we have no files
|
|
|
|
if filesetsWithBase == [ ] then
|
|
|
|
_emptyWithoutBase
|
|
|
|
else
|
|
|
|
_create commonBase resultTree;
|
2023-09-13 23:29:28 +02:00
|
|
|
|
2023-09-14 21:41:03 +02:00
|
|
|
# The union of multiple filesetTree's with the same base path.
|
|
|
|
# Later elements are only evaluated if necessary.
|
|
|
|
# Type: [ filesetTree ] -> filesetTree
|
|
|
|
_unionTrees = trees:
|
|
|
|
let
|
|
|
|
stringIndex = findFirstIndex isString null trees;
|
|
|
|
withoutNull = filter (tree: tree != null) trees;
|
|
|
|
in
|
|
|
|
if stringIndex != null then
|
|
|
|
# If there's a string, it's always a fully included tree (dir or file),
|
|
|
|
# no need to look at other elements
|
|
|
|
elemAt trees stringIndex
|
|
|
|
else if withoutNull == [ ] then
|
|
|
|
# If all trees are null, then the resulting tree is also null
|
|
|
|
null
|
2023-09-13 23:29:28 +02:00
|
|
|
else
|
2023-09-14 21:41:03 +02:00
|
|
|
# The non-null elements have to be attribute sets representing partial trees
|
|
|
|
# We need to recurse into those
|
|
|
|
zipAttrsWith (name: _unionTrees) withoutNull;
|
2023-09-26 02:10:46 +02:00
|
|
|
|
|
|
|
# Computes the intersection of a list of filesets.
|
|
|
|
# The filesets must already be coerced and validated to be in the same filesystem root
|
|
|
|
# Type: Fileset -> Fileset -> Fileset
|
|
|
|
_intersection = fileset1: fileset2:
|
|
|
|
let
|
|
|
|
# The common base components prefix, e.g.
|
|
|
|
# (/foo/bar, /foo/bar/baz) -> /foo/bar
|
|
|
|
# (/foo/bar, /foo/baz) -> /foo
|
|
|
|
commonBaseComponentsLength =
|
|
|
|
# TODO: Have a `lib.lists.commonPrefixLength` function such that we don't need the list allocation from commonPrefix here
|
|
|
|
length (
|
|
|
|
commonPrefix
|
|
|
|
fileset1._internalBaseComponents
|
|
|
|
fileset2._internalBaseComponents
|
|
|
|
);
|
|
|
|
|
|
|
|
# To be able to intersect filesetTree's together, they need to have the same base path.
|
|
|
|
# Base paths can be intersected by taking the longest one (if any)
|
|
|
|
|
|
|
|
# The fileset with the longest base, if any, e.g.
|
|
|
|
# (/foo/bar, /foo/bar/baz) -> /foo/bar/baz
|
|
|
|
# (/foo/bar, /foo/baz) -> null
|
|
|
|
longestBaseFileset =
|
|
|
|
if commonBaseComponentsLength == length fileset1._internalBaseComponents then
|
|
|
|
# The common prefix is the same as the first path, so the second path is equal or longer
|
|
|
|
fileset2
|
|
|
|
else if commonBaseComponentsLength == length fileset2._internalBaseComponents then
|
|
|
|
# The common prefix is the same as the second path, so the first path is longer
|
|
|
|
fileset1
|
|
|
|
else
|
|
|
|
# The common prefix is neither the first nor the second path
|
|
|
|
# This means there's no overlap between the two sets
|
|
|
|
null;
|
|
|
|
|
|
|
|
# Whether the result should be the empty value without a base
|
|
|
|
resultIsEmptyWithoutBase =
|
|
|
|
# If either fileset is the empty fileset without a base, the intersection is too
|
|
|
|
fileset1._internalIsEmptyWithoutBase
|
|
|
|
|| fileset2._internalIsEmptyWithoutBase
|
|
|
|
# If there is no overlap between the base paths
|
|
|
|
|| longestBaseFileset == null;
|
|
|
|
|
|
|
|
# Lengthen each fileset's tree to the longest base prefix
|
|
|
|
tree1 = _lengthenTreeBase longestBaseFileset._internalBaseComponents fileset1;
|
|
|
|
tree2 = _lengthenTreeBase longestBaseFileset._internalBaseComponents fileset2;
|
|
|
|
|
|
|
|
# With two filesetTree's with the same base, we can compute their intersection
|
|
|
|
resultTree = _intersectTree tree1 tree2;
|
|
|
|
in
|
|
|
|
if resultIsEmptyWithoutBase then
|
|
|
|
_emptyWithoutBase
|
|
|
|
else
|
|
|
|
_create longestBaseFileset._internalBase resultTree;
|
|
|
|
|
|
|
|
# The intersection of two filesetTree's with the same base path
|
|
|
|
# The second element is only evaluated as much as necessary.
|
|
|
|
# Type: filesetTree -> filesetTree -> filesetTree
|
|
|
|
_intersectTree = lhs: rhs:
|
|
|
|
if isAttrs lhs && isAttrs rhs then
|
|
|
|
# Both sides are attribute sets, we can recurse for the attributes existing on both sides
|
|
|
|
mapAttrs
|
|
|
|
(name: _intersectTree lhs.${name})
|
|
|
|
(builtins.intersectAttrs lhs rhs)
|
|
|
|
else if lhs == null || isString rhs then
|
|
|
|
# If the lhs is null, the result should also be null
|
|
|
|
# And if the rhs is the identity element
|
|
|
|
# (a string, aka it includes everything), then it's also the lhs
|
|
|
|
lhs
|
|
|
|
else
|
|
|
|
# In all other cases it's the rhs
|
|
|
|
rhs;
|
2023-10-10 19:10:02 +02:00
|
|
|
|
|
|
|
_fileFilter = predicate: fileset:
|
|
|
|
let
|
|
|
|
recurse = path: tree:
|
|
|
|
mapAttrs (name: subtree:
|
|
|
|
if isAttrs subtree || subtree == "directory" then
|
|
|
|
recurse (path + "/${name}") subtree
|
|
|
|
else if
|
|
|
|
predicate {
|
|
|
|
inherit name;
|
|
|
|
type = subtree;
|
|
|
|
# To ensure forwards compatibility with more arguments being added in the future,
|
|
|
|
# adding an attribute which can't be deconstructed :)
|
|
|
|
"lib.fileset.fileFilter: The predicate function passed as the first argument must be able to handle extra attributes for future compatibility. If you're using `{ name, file }:`, use `{ name, file, ... }:` instead." = null;
|
|
|
|
}
|
|
|
|
then
|
|
|
|
subtree
|
|
|
|
else
|
|
|
|
null
|
|
|
|
) (_directoryEntries path tree);
|
|
|
|
in
|
|
|
|
if fileset._internalIsEmptyWithoutBase then
|
|
|
|
_emptyWithoutBase
|
|
|
|
else
|
|
|
|
_create fileset._internalBase
|
|
|
|
(recurse fileset._internalBase fileset._internalTree);
|
2023-08-17 00:55:32 +02:00
|
|
|
}
|