1 { lib, fetchgit, formats }:
4 json = formats.json { };
7 # Derive a pin file from workspace state.
8 mkPinFile = workspaceState:
9 assert workspaceState.version >= 5 && workspaceState.version <= 6;
10 json.generate "Package.resolved" {
12 object.pins = map (dep: {
13 package = dep.packageRef.name;
14 repositoryURL = dep.packageRef.location;
15 state = dep.state.checkoutState;
16 }) workspaceState.object.dependencies;
19 # Make packaging helpers from swiftpm2nix generated output.
20 helpers = generated: let
21 inherit (import generated) workspaceStateFile hashes;
22 workspaceState = lib.importJSON workspaceStateFile;
23 pinFile = mkPinFile workspaceState;
26 # Create fetch expressions for dependencies.
27 sources = listToAttrs (
28 map (dep: nameValuePair dep.subpath (fetchgit {
29 url = dep.packageRef.location;
30 rev = dep.state.checkoutState.revision;
31 sha256 = hashes.${dep.subpath};
32 fetchSubmodules = true;
33 })) workspaceState.object.dependencies
36 # Configure phase snippet for use in packaging.
38 mkdir -p .build/checkouts
39 ln -sf ${pinFile} ./Package.resolved
40 install -m 0600 ${workspaceStateFile} ./.build/workspace-state.json
42 + concatStrings (mapAttrsToList (name: src: ''
43 ln -s '${src}' '.build/checkouts/${name}'
46 # Helper that makes a swiftpm dependency mutable by copying the source.
47 swiftpmMakeMutable() {
48 local orig="$(readlink .build/checkouts/$1)"
49 rm .build/checkouts/$1
50 cp -r "$orig" .build/checkouts/$1
51 chmod -R u+w .build/checkouts/$1