1{
2 lib,
3 fetchgit,
4 formats,
5}:
6let
7 inherit (lib)
8 concatStrings
9 listToAttrs
10 makeOverridable
11 mapAttrsToList
12 nameValuePair
13 ;
14 json = formats.json { };
15in
16rec {
17
18 # Derive a pin file from workspace state.
19 mkPinFile =
20 workspaceState:
21 assert workspaceState.version >= 5 && workspaceState.version <= 6;
22 json.generate "Package.resolved" {
23 version = 1;
24 object.pins = map (dep: {
25 package = dep.packageRef.name;
26 repositoryURL = dep.packageRef.location;
27 state = dep.state.checkoutState;
28 }) workspaceState.object.dependencies;
29 };
30
31 # Make packaging helpers from swiftpm2nix generated output.
32 helpers =
33 generated:
34 let
35 inherit (import generated) workspaceStateFile hashes;
36 workspaceState = lib.importJSON workspaceStateFile;
37 pinFile = mkPinFile workspaceState;
38 in
39 rec {
40
41 # Create fetch expressions for dependencies.
42 sources = listToAttrs (
43 map (
44 dep:
45 nameValuePair dep.subpath (fetchgit {
46 url = dep.packageRef.location;
47 rev = dep.state.checkoutState.revision;
48 sha256 = hashes.${dep.subpath};
49 fetchSubmodules = true;
50 })
51 ) workspaceState.object.dependencies
52 );
53
54 # Configure phase snippet for use in packaging.
55 configure = ''
56 mkdir -p .build/checkouts
57 ln -sf ${pinFile} ./Package.resolved
58 install -m 0600 ${workspaceStateFile} ./.build/workspace-state.json
59 ''
60 + concatStrings (
61 mapAttrsToList (name: src: ''
62 ln -s '${src}' '.build/checkouts/${name}'
63 '') sources
64 )
65 + ''
66 # Helper that makes a swiftpm dependency mutable by copying the source.
67 swiftpmMakeMutable() {
68 local orig="$(readlink .build/checkouts/$1)"
69 rm .build/checkouts/$1
70 cp -r "$orig" .build/checkouts/$1
71 chmod -R u+w .build/checkouts/$1
72 }
73 '';
74
75 };
76
77}