swiftpm2nix: init

This commit is contained in:
Stéphan Kochen 2022-07-30 11:38:37 +02:00
parent 0b877d85e5
commit 4f082f522e
3 changed files with 99 additions and 0 deletions

View File

@ -0,0 +1,23 @@
{ lib, stdenv, makeWrapper, jq, nix-prefetch-git }:
stdenv.mkDerivation {
name = "swiftpm2nix";
nativeBuildInputs = [ makeWrapper ];
dontUnpack = true;
installPhase = ''
install -vD ${./swiftpm2nix.sh} $out/bin/swiftpm2nix
wrapProgram $out/bin/$name \
--prefix PATH : ${lib.makeBinPath [ jq nix-prefetch-git ]} \
'';
preferLocalBuild = true;
meta = {
description = "Generate a Nix expression to fetch swiftpm dependencies";
maintainers = with lib.maintainers; [ dtzWill trepetti dduan trundle stephank ];
platforms = lib.platforms.all;
};
}

View File

@ -0,0 +1,74 @@
#!/usr/bin/env bash
# Generates a Nix expression to fetch swiftpm dependencies, and a
# configurePhase snippet to prepare a working directory for swift-build.
set -eu -o pipefail
shopt -s lastpipe
pinFile="Package.resolved"
stateFile=".build/workspace-state.json"
for file in $pinFile $stateFile; do
if [[ ! -f "$file" ]]; then
echo >&2 "Missing $file. Run 'swift package resolve' first."
exit 1
fi
done
if [[ "$(jq .version $pinFile)" != "1" ]]; then
echo >&2 "Unsupported $pinFile version"
exit 1
fi
if [[ "$(jq .version $stateFile)" != "5" ]]; then
echo >&2 "Unsupported $stateFile version"
exit 1
fi
# Iterate dependencies and prefetch.
sources=""
jq -r '.object.dependencies[] | "\(.subpath) \(.packageRef.location) \(.state.checkoutState.revision)"' $stateFile \
| while read -r name url rev; do
echo >&2 "-- Fetching $name"
sha256="$(nix-prefetch-git $url $rev | jq -r .sha256)"
sources+="
\"$name\" = fetchgit {
url = \"$url\";
rev = \"$rev\";
sha256 = \"$sha256\";
};"
echo >&2
done
sources+=$'\n'" "
# Generate output.
mkdir -p nix
# Copy the pin file as-is.
cp $pinFile nix/Package.resolved
# Copy the workspace state, but clear 'artifacts'.
jq '.object.artifacts = []' < $stateFile > nix/workspace-state.json
# Build an expression for fetching sources, and preparing the working directory.
cat > nix/default.nix << EOF
# This file was generated by swiftpm2nix.
{ lib, fetchgit }: rec {
sources = {$sources};
configure = ''
mkdir -p .build/checkouts
ln -sf \${./Package.resolved} ./Package.resolved
install -m 0600 \${./workspace-state.json} ./.build/workspace-state.json
''
+ lib.concatStrings (lib.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
}
'';
}
EOF
echo >&2 "-- Generated ./nix"

View File

@ -15044,6 +15044,8 @@ with pkgs;
swiftPackages = recurseIntoAttrs (callPackage ../development/compilers/swift { });
inherit (swiftPackages) swift;
swiftpm2nix = callPackage ../development/tools/swiftpm2nix { };
swiProlog = callPackage ../development/compilers/swi-prolog {
openssl = openssl_1_1;
inherit (darwin.apple_sdk.frameworks) Security;