Skip to content

Instantly share code, notes, and snippets.

@manveru
Created June 10, 2020 14:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manveru/b04081bc29036f64587b4fa3d57daa0d to your computer and use it in GitHub Desktop.
Save manveru/b04081bc29036f64587b4fa3d57daa0d to your computer and use it in GitHub Desktop.
Fetch Crystal dependencies on the lfy
{ pkgs ? import ./nix { } }:
let
inherit (builtins) fromJSON readFile toFile elemAt split mapAttrs;
inherit (pkgs)
crystal runCommand stdenv lib coreutils inclusive pkgconfig openssl;
inherit (pkgs.lib) makeBinPath;
shardLock2JSON = shardLockFile:
runCommand "yaml2json" { buildInputs = [ crystal ]; } ''
crystal eval <<-EOF > $out
require "json"
require "yaml"
puts YAML.parse(File.read("${shardLockFile}")).to_json
EOF
'';
fetchShards = shardLockFile:
mapAttrs (name: value:
let
parts = split "\\+git\\.commit\\." value.version;
version = elemAt parts 0;
rev = elemAt parts 2;
in fetchGit {
url = value.git;
rev = rev;
}) (fromJSON (readFile (shardLock2JSON shardLockFile))).shards;
shardsDir = shardLockFile:
stdenv.mkDerivation {
name = "shard-libs";
__structuredAttrs = true;
preferLocalBuild = true;
PATH = lib.makeBinPath [ coreutils ];
shards = fetchShards shardLockFile;
builder = toFile "builder" ''
. .attrs.sh
out=''${outputs[out]}
mkdir $out
for name in "''${!shards[@]}"; do
echo "Linking shard $name ..."
ln -s "''${shards[$name]}" "$out/$name"
done
'';
};
in crystal.buildCrystalPackage {
pname = "example";
version = "0.1.0";
format = "crystal";
buildInputs = [ pkgconfig openssl ];
src = inclusive ./. [ ./src ];
preConfigure = ''
ln -s ${shardsDir ./shard.lock} lib
'';
crystalBinaries.example.src = "src/example.cr";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment