Skip to content

Commit

Permalink
trivial-builders: add writeMultipleReferencesToFile
Browse files Browse the repository at this point in the history
This builder takes a list `path` and reduces the
`writeMultipleReferencesToFile` result of each element to a file.
It would be helpful when building a container image from a list of
packages.
  • Loading branch information
ShamrockLee committed Jun 23, 2022
1 parent 7ef251f commit 45d9b4a
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions pkgs/build-support/trivial-builders.nix
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,35 @@ rec {
writeDirectReferencesToFile (writeText "string-file" string);


/*
* Write the references (i.e. the runtime dependencies in the Nix store) of the elements in `paths' to a file.
* The elements are sorted according to their hashes, and duplicated lines are removed.
*/
writeMultipleReferencesToFile = paths: runCommand "runtime-deps-multiple" {
referencesFiles = map writeReferencesToFile paths;
} ''
touch "$out"
declare -A referencesDict=()
for refFile in $referencesFiles; do
while read ref; do
if [[ "''${ref:0:${toString (lib.stringLength builtins.storeDir + 1)}}" == "${builtins.storeDir}/" ]]; then
refHash="''${ref:${toString (lib.stringLength builtins.storeDir + 1)}:32}"
refTail="$(stripHash "$ref")"
# Set if not set before
: "''${referencesDict["$refHash"]="$refTail"}"
fi
done < "$refFile"
done
if (( "''${#referencesDict[@]}" )); then
declare -a hashesSorted
IFS=$'\n' hashesSorted=( $(sort <<<"''${!referencesDict[*]}") )
for refHash in ''${hashesSorted[@]}; do
echo "${builtins.storeDir}/$refHash-''${referencesDict[$refHash]}" >> "$out"
done
fi
'';


/* Print an error message if the file with the specified name and
* hash doesn't exist in the Nix store. This function should only
* be used by non-redistributable software with an unfree license
Expand Down

0 comments on commit 45d9b4a

Please sign in to comment.