about summary refs log tree commit diff
path: root/pkgs/development/tools/swiftpm2nix/support.nix
blob: dfc2d01a450171422b8c76619857ef4fec93dd6b (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
{ lib, fetchgit, formats }:
with lib;
let
  json = formats.json { };
in rec {

  # Derive a pin file from workspace state.
  mkPinFile = workspaceState:
    assert workspaceState.version >= 5 && workspaceState.version <= 6;
    json.generate "Package.resolved" {
      version = 1;
      object.pins = map (dep: {
        package = dep.packageRef.name;
        repositoryURL = dep.packageRef.location;
        state = dep.state.checkoutState;
      }) workspaceState.object.dependencies;
    };

  # Make packaging helpers from swiftpm2nix generated output.
  helpers = generated: let
    inherit (import generated) workspaceStateFile hashes;
    workspaceState = lib.importJSON workspaceStateFile;
    pinFile = mkPinFile workspaceState;
  in rec {

    # Create fetch expressions for dependencies.
    sources = listToAttrs (
      map (dep: nameValuePair dep.subpath (fetchgit {
        url = dep.packageRef.location;
        rev = dep.state.checkoutState.revision;
        sha256 = hashes.${dep.subpath};
        fetchSubmodules = true;
      })) workspaceState.object.dependencies
    );

    # Configure phase snippet for use in packaging.
    configure = ''
      mkdir -p .build/checkouts
      ln -sf ${pinFile} ./Package.resolved
      install -m 0600 ${workspaceStateFile} ./.build/workspace-state.json
    ''
      + concatStrings (mapAttrsToList (name: src: ''
        ln -s '${src}' '.build/checkouts/${name}'
      '') sources)
      + ''
        # Helper that makes a swiftpm dependency mutable by copying the source.
        swiftpmMakeMutable() {
          local orig="$(readlink .build/checkouts/$1)"
          rm .build/checkouts/$1
          cp -r "$orig" .build/checkouts/$1
          chmod -R u+w .build/checkouts/$1
        }
      '';

  };

}