Merge pull request #26 from humancalico/go

add go translator using gomod2nix
This commit is contained in:
DavHau 2021-11-17 11:30:20 +07:00 committed by GitHub
commit e1e1979a4a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 176 additions and 0 deletions

View File

@ -1,5 +1,21 @@
{
"nodes": {
"gomod2nix": {
"flake": false,
"locked": {
"lastModified": 1627572165,
"narHash": "sha256-MFpwnkvQpauj799b4QTBJQFEddbD02+Ln5k92QyHOSk=",
"owner": "tweag",
"repo": "gomod2nix",
"rev": "67f22dd738d092c6ba88e420350ada0ed4992ae8",
"type": "github"
},
"original": {
"owner": "tweag",
"repo": "gomod2nix",
"type": "github"
}
},
"mach-nix": {
"flake": false,
"locked": {
@ -81,6 +97,7 @@
},
"root": {
"inputs": {
"gomod2nix": "gomod2nix",
"mach-nix": "mach-nix",
"nix-parsec": "nix-parsec",
"nixpkgs": "nixpkgs",

View File

@ -4,6 +4,9 @@
inputs = {
nixpkgs.url = "nixpkgs/nixos-unstable";
# required for builder go/gomod2nix
gomod2nix = { url = "github:tweag/gomod2nix"; flake = false; };
# required for translator nodejs/pure/package-lock
nix-parsec = { url = "github:nprindle/nix-parsec"; flake = false; };
@ -19,6 +22,7 @@
outputs = {
self,
gomod2nix,
mach-nix,
nix-parsec,
nixpkgs,

View File

@ -0,0 +1,21 @@
{
lib,
pkgs,
externals,
...
}:
{
fetchedSources,
dreamLock,
}:
let
gomod2nixTOML = fetchedSources.mapAttrs
dependencyObject.goName;
in
externals.gomod2nixBuilder rec {
pname = dreamLock.generic.mainPackage;
version = dreamLock.sources."${pname}".version;
src = fetchedSources."${pname}";
modules = ./gomod2nix.toml;
}

View File

@ -0,0 +1,68 @@
{
# dream2nix utils
utils,
dream2nixWithExternals,
bash,
coreutils,
jq,
lib,
nix,
writeScriptBin,
...
}:
{
# the input format is specified in /specifications/translator-call-example.json
# this script receives a json file including the input paths and specialArgs
translateBin = utils.writePureShellScript
[
bash
coreutils
jq
nix
]
''
# accroding to the spec, the translator reads the input from a json file
jsonInput=$1
# read the json input
outputFile=$(${jq}/bin/jq '.outputFile' -c -r $jsonInput)
inputDirectory=$(${jq}/bin/jq '.inputDirectories | .[0]' -c -r $jsonInput)
tmpBuild=$(mktemp -d)
cd $tmpBuild
cp -r $inputDirectory/* .
chmod -R +w .
# This should be in sync with gomod2nix version in flake.lock
nix run github:tweag/gomod2nix/67f22dd738d092c6ba88e420350ada0ed4992ae8
nix eval --show-trace --impure --raw --expr "import ${./translate.nix} ${dream2nixWithExternals} ./." > $outputFile
'';
# From a given list of paths, this function returns all paths which can be processed by this translator.
# This allows the framework to detect if the translator is compatible with the given inputs
# to automatically select the right translator.
compatiblePaths =
{
inputDirectories,
inputFiles,
}@args:
{
inputDirectories = lib.filter
(utils.containsMatchingFile [ ''go\.sum'' ''go\.mod'' ])
args.inputDirectories;
inputFiles = [];
};
# If the translator requires additional arguments, specify them here.
# There are only two types of arguments:
# - string argument (type = "argument")
# - boolean flag (type = "flag")
# String arguments contain a default value and examples. Flags do not.
extraArgs = {};
}

View File

@ -0,0 +1,66 @@
dream2nixWithExternals:
cwd:
let
dream2nix = import dream2nixWithExternals { };
b = builtins;
parsed = b.fromTOML (builtins.readFile "${cwd}/gomod2nix.toml");
pkgs = import <nixpkgs> { };
lib = pkgs.lib;
serializePackages = inputData:
lib.mapAttrsToList
(goName: depAttrs: depAttrs // { inherit goName; })
parsed;
translated = dream2nix.utils.simpleTranslate "gomod2nix" rec {
inputData = parsed;
mainPackageName =
let
firstLine = (b.elemAt (lib.splitString "\n" (b.readFile "${cwd}/go.mod")) 0);
in
lib.last (lib.splitString "/" (b.elemAt (lib.splitString " " firstLine) 1));
mainPackageVersion = "unknown";
subsystemName = "go";
subsystemAttrs = { };
inherit serializePackages;
mainPackageDependencies =
lib.forEach
(serializePackages parsed)
(dep: {
name = getName dep;
version = getVersion dep;
});
getOriginalID = dependencyObject:
null;
getName = dependencyObject:
dependencyObject.goName;
getVersion = dependencyObject:
lib.removePrefix "v" dependencyObject.sumVersion;
getDependencies = dependencyObject: getDepByNameVer: dependenciesByOriginalID:
[];
getSourceType = dependencyObject: "git";
sourceConstructors = {
git = dependencyObject:
{
type = "git";
version = getVersion dependencyObject;
hash = dependencyObject.fetch.sha256;
url = dependencyObject.fetch.url;
rev = dependencyObject.fetch.rev;
};
};
};
in
dream2nix.utils.dreamLock.toJSON translated