Skip to content

Instantly share code, notes, and snippets.

@moretea
Created January 23, 2017 11:12
Show Gist options
  • Save moretea/6b0dfc305320600126385726be734f5d to your computer and use it in GitHub Desktop.
Save moretea/6b0dfc305320600126385726be734f5d to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# (C) 2017 Maarten Hoogendoorn <maarten@moretea.nl>
# Released under the GPL3.0
#
# This script finds the runtime closure of programs, and tries to find the correct license for it.
# Given that the mapping of store paths -> nix expressions does not exist, it guesses the package
# name and aborts if this guess is wrong.
require "json"
require "pp"
package_name = ARGV.first
if package_name.nil?
puts "USAGE: #{$0} <packagename>"
exit
end
NIX_EXPR = <<EONIX.gsub(/\s+/," ")
with (import <nixpkgs> {});
with pkgs.lib;
let
getLicense = meta:
if !(meta ? license) then
"not specified"
else
if (meta.license ? fullName)
then meta.license.fullName
else
if (isList meta.license)
then map (l: l.fullName) meta.license
else meta.license;
package = pkgs.${builtins.getEnv("PACKAGE")};
showPkg = pkg: {
name = pkg.name;
path = toString pkg;
license = getLicense pkg.meta;
};
in
{
packagePath = toString package;
info = showPkg package;
}
EONIX
# Guestimate of package names from derivation names;
runtime_dep_paths = `nix-store -qR $(nix-build '<nixpkgs>' -A #{package_name})`.split("\n")
deps = runtime_dep_paths.map do |dep_path|
name,version = dep_path.split("-",2).last.split("-",2)
{ name: name, version: version, dep_path: dep_path}
end
output = []
# Check
output = deps.map do |dep|
nix_license_info = JSON.load `PACKAGE=#{dep[:name]} nix-instantiate --strict --json --eval --expr '#{NIX_EXPR}'`
if nix_license_info["packagePath"] == dep[:dep_path]
dep.merge({license: nix_license_info["info"]["license"] })
else
puts "Found wrong dependency path!"
puts "Got path this back from nix:"
pp nix_license_info
puts "But actually we were interested in:"
pp dep
exit 1
end
end
puts JSON.pretty_generate(output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment