···33let
44 inherit (lib)
55 optionalAttrs
66+ optionalString
77+ hasPrefix
68 warn
99+ map
1010+ isList
711 ;
812in
913···468472 ...
469473470474475475+ To create a directory structure from a specific subdirectory of input `paths` instead of their full trees,
476476+ you can either append the subdirectory path to each input path, or use the `stripPrefix` argument to
477477+ remove the common prefix during linking.
478478+479479+ Example:
480480+481481+482482+ # create symlinks of tmpfiles.d rules from multiple packages
483483+ symlinkJoin { name = "tmpfiles.d"; paths = [ pkgs.lvm2 pkgs.nix ]; stripPrefix = "/lib/tmpfiles.d"; }
484484+485485+486486+ This creates a derivation with a directory structure like the following:
487487+488488+489489+ /nix/store/m5s775yicb763hfa133jwml5hwmwzv14-tmpfiles.d
490490+ |-- lvm2.conf -> /nix/store/k6js0l5f0zpvrhay49579fj939j77p2w-lvm2-2.03.29/lib/tmpfiles.d/lvm2.conf
491491+ `-- nix-daemon.conf -> /nix/store/z4v2s3s3y79fmabhps5hakb3c5dwaj5a-nix-1.33.7/lib/tmpfiles.d/nix-daemon.conf
492492+493493+494494+ By default, packages that don't contain the specified subdirectory are silently skipped.
495495+ Set `failOnMissing = true` to make the build fail if any input package is missing the subdirectory
496496+ (this is the default behavior when not using stripPrefix).
497497+471498 symlinkJoin and linkFarm are similar functions, but they output
472499 derivations with different structure.
473500···490517 "symlinkJoin requires either a `name` OR `pname` and `version`";
491518 "${args_.pname}-${args_.version}"
492519 , paths
520520+ , stripPrefix ? ""
493521 , preferLocalBuild ? true
494522 , allowSubstitutes ? false
495523 , postBuild ? ""
524524+ , failOnMissing ? stripPrefix == ""
496525 , ...
497526 }:
527527+ assert lib.assertMsg (stripPrefix != "" -> (hasPrefix "/" stripPrefix && stripPrefix != "/")) ''
528528+ stripPrefix must be either an empty string (disable stripping behavior), or relative path prefixed with /.
529529+530530+ Ensure that the path starts with / and specifies path to the subdirectory.
531531+ '';
532532+498533 let
499499- args = removeAttrs args_ [ "name" "postBuild" ]
534534+ mapPaths = f: paths: map (path:
535535+ if path == null then null
536536+ else if isList path then mapPaths f path
537537+ else f path
538538+ ) paths;
539539+ args = removeAttrs args_ [ "name" "postBuild" "stripPrefix" "paths" "failOnMissing" ]
500540 // {
501541 inherit preferLocalBuild allowSubstitutes;
542542+ paths = mapPaths (path: "${path}${stripPrefix}") paths;
502543 passAsFile = [ "paths" ];
503544 }; # pass the defaults
504545 in
···506547 ''
507548 mkdir -p $out
508549 for i in $(cat $pathsPath); do
509509- ${lndir}/bin/lndir -silent $i $out
550550+ ${optionalString (!failOnMissing) "if test -d $i; then "}${lndir}/bin/lndir -silent $i $out${optionalString (!failOnMissing) "; fi"}
510551 done
511552 ${postBuild}
512553 '';