diff --git a/pkgs/development/tools/swiftpm2nix/default.nix b/pkgs/development/tools/swiftpm2nix/default.nix new file mode 100644 index 000000000000..d115a6d5373b --- /dev/null +++ b/pkgs/development/tools/swiftpm2nix/default.nix @@ -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; + }; +} diff --git a/pkgs/development/tools/swiftpm2nix/swiftpm2nix.sh b/pkgs/development/tools/swiftpm2nix/swiftpm2nix.sh new file mode 100755 index 000000000000..13e8a42a7dd7 --- /dev/null +++ b/pkgs/development/tools/swiftpm2nix/swiftpm2nix.sh @@ -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" diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix index 2f15ac166b59..6af03574ebcb 100644 --- a/pkgs/top-level/all-packages.nix +++ b/pkgs/top-level/all-packages.nix @@ -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;