about summary refs log tree commit diff
path: root/modules/core/lazy-packages.nix
blob: 80053396cc4e6646199f56b071a920cef182cb4f (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
{ config, pkgs, lib, ... }:

let
  inherit (lib) escapeShellArg;
  inherit (config.vuizvui) lazyPackages;

  # Encode the store path in base 64 so that the wrapper
  # doesn't have a direct dependency on the package.
  encoder = "${escapeShellArg "${pkgs.coreutils}/bin/base64"} -w0";
  decoder = "${escapeShellArg "${pkgs.coreutils}/bin/base64"} -d";

  # The command used to fetch the store path from the binary cache.
  fetchSubstitute = "${escapeShellArg "${pkgs.nix}/bin/nix-store"} -r";

  mkWrapper = package: pkgs.runCommand "${package.name}-lazy" {
    inherit package;
  } ''
    encoded="$(echo "$package" | ${encoder})"

    if [ ! -e "$package/bin" ]; then
      echo "Store path $package doesn't have a \`bin' directory" \
           "so we can't create lazy wrappers for it. Please" \
           "remove \`${escapeShellArg package.name}' from" \
           "\`vuizvui.lazyPackages'." >&2
      exit 1
    fi

    for bin in "$package"/bin/*; do
      [ -x "$bin" -a ! -d "$bin" ] || continue
      binpath="''${bin#$package/}"

      mkdir -p "$out/bin"
      ( echo ${escapeShellArg "#!${pkgs.stdenv.shell}"}
        echo "encoded='$encoded'"
        echo "binpath='$binpath'"
        echo -n ${escapeShellArg ''
          storepath="$(echo "$encoded" | ${decoder})"
          program="$storepath/$binpath"
          if [ ! -e "$storepath" ]; then
            ${fetchSubstitute} "$storepath" > /dev/null || exit $?
          fi
          exec "$program" "$@"
        ''}
      ) > "$out/bin/$(basename "$bin")"
      chmod +x "$out/bin/$(basename "$bin")"
    done
  '';

  wrappers = map mkWrapper config.vuizvui.lazyPackages;

in {
  options.vuizvui.lazyPackages = lib.mkOption {
    type = lib.types.listOf lib.types.package;
    default = [];
    example = lib.literalExample "[ pkgs.gimp pkgs.libreoffice ]";
    description = ''
      Packages which are built for this system but instead of being a full
      runtime dependency, only wrappers of all executables that reside in the
      <literal>bin</literal> directory are actually runtime dependencies.

      As soon as one of these wrappers is executed, the real package is fetched
      and the corresponding binary is executed.
    '';
  };

  config.environment.systemPackages = map mkWrapper lazyPackages;
}