Skip to content

Instantly share code, notes, and snippets.

@manveru
Created May 17, 2018 09:21
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/8241ae64b86ec9ae8262125b0516ce64 to your computer and use it in GitHub Desktop.
Save manveru/8241ae64b86ec9ae8262125b0516ce64 to your computer and use it in GitHub Desktop.
crystal2nix

Crystal2Nix

Since I don't have time to make a proper project for this, here's how to use it:

cd $YOU_RPROJECT
crystal crystal2nix.cr
ln -s $(nix-build crystal2nix.nix)/lib
require "json"
require "yaml"
yaml = YAML.parse(File.read("shard.lock"))
yaml["shards"].each do |key, value|
next if value["revision"]?
url = "https://github.com/#{value["github"]}"
ref = "v#{value["version"]}"
puts "git ls-remote #{ url } #{ ref }"
Process.run("git", args: ["ls-remote", url, ref]) do |x|
x.error.each_line do |e|
puts e
end
x.output.each_line do |o|
value.as_h["revision"] = o.split("\t")[0]
end
end
end
File.write "shard.lock", yaml.to_yaml
with import <nixpkgs> {};
with builtins;
let
shard-json = stdenv.mkDerivation {
name = "shard-json";
src = ./shard.lock;
buildInputs = [remarshal];
phases = "buildPhase";
buildPhase = ''
remarshal --indent-json -if yaml -i $src -of json -o $out
'';
};
projects = (fromJSON ( readFile "${ shard-json }" )).shards;
projectSources = map (projectName:
let project = projects.${projectName}; in
stdenv.mkDerivation {
name = replaceStrings ["/"] ["-"] projectName;
src = fetchGit {
url = if project?github then
"https://github.com/" + project.github
else
abort ( "Don't know how to handle" + projectName );
ref = "v${project.version}";
rev = project.revision;
};
phases = "buildPhase";
buildPhase = ''
mkdir -p $out/package
cp -r $src/* $out/package
echo "${projectName}" > $out/name
'';
}
) (attrNames projects );
in stdenv.mkDerivation {
name = "depTree";
src = projectSources;
phases = "buildPhase";
buildPhase = ''
mkdir -p $out
for pkg in $src; do
echo building $pkg
name="$(cat $pkg/name)"
mkdir -p $out/lib/$name
cp -r $pkg/package/* $out/lib/$name
done
'';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment