about summary refs log tree commit diff
path: root/pkgs/profpatsch/execline/symlink.nix
blob: ca8684d29f2fc8b29364d376f30812ca1a6a1250 (plain) (blame)
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
49
50
{ lib, s6-portable-utils, coreutils, runExecline }:
# DrvPath :: path relative to the derivation
# AbsPath :: absolute path in the store
#    Name
# -> List (Symlink { dest: DrvPath, orig: AbsPath })
# -> Drv
name: links:

let
  toNetstring = s:
    "${toString (builtins.stringLength s)}:${s},";

in
runExecline {
  inherit name;

  derivationArgs = {
    pathTuples = lib.concatMapStrings
      ({dest, orig}: toNetstring
        (toNetstring dest + (toNetstring orig)))
      links;
    passAsFile = [ "pathTuples" ];
    # bah! coreutils just for cat :(
    PATH = lib.makeBinPath [ s6-portable-utils ];
  };

  execline = ''
    importas -ui p pathTuplesPath
    importas -ui out out
    forbacktickx -d "" destorig { ${coreutils}/bin/cat $p }
      importas -ui do destorig
      multidefine -d "" $do { destsuffix orig }
      define dest ''${out}/''${destsuffix}

      # this call happens for every file, not very efficient
      foreground {
        backtick -n d { s6-dirname $dest }
        importas -ui d d
        s6-mkdir -p $d
      }

      ifthenelse { s6-test -L $orig } {
        backtick -n res { s6-linkname -f $orig }
        importas -ui res res
        s6-ln -fs $res $dest
      } {
        s6-ln -fs $orig $dest
      }
  '';
}