aboutsummaryrefslogtreecommitdiff
path: root/pkgs/misc/merge.nix
blob: b4b4892a68af39e733a3375772e7c37fcccd19d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
{ runCommand, writeShellApplication, rsync, lib, ... }:
args@{ name, paths, preferLocalBuild ? true, allowSubstitutes ? false, postBuild ? "" }:
let
  env = builtins.removeAttrs args [ "name" "postBuild" ] // {
    inherit preferLocalBuild allowSubstitutes;
    nativeBuildInputs = (args.nativeBuildInputs or []) ++ [
      rsync
      replaceBadSymlink
    ];
    passAsFile = [ "paths" ];
  };

  # copyDirs = p: ''
  #   ${lib.getExe rsync} -rv --copy-dirlinks --links --executability "${p}/" "$out"
  # '';

  replaceBadSymlink = writeShellApplication {
    name = "replaceBadSymlink";
    # runtimeInputs = [ coreutils ];
    text = ''
      FILE="''${1-}"

      if [[ -z "$FILE" ]]; then
        >&2 echo "Need a file input"
      fi

      REALPATH="$(readlink -f "$FILE")"

      case "$REALPATH" in
        "/nix/store/"* )
          cp -vf --remove-destination "$REALPATH" "$FILE"
        ;;
      esac
    '';
  };

in runCommand name env ''
  mkdir -p $out

  for i in $(cat $pathsPath); do
    rsync -rv --copy-dirlinks --links --executability --chmod=u+w "$i/" "$out"
  done

  find $out/ -type l -exec replaceBadSymlink {} \;


  ${postBuild}
''