mirror of
https://github.com/nix-community/dream2nix.git
synced 2024-09-11 14:35:30 +03:00
add go support using gomod2nix
Co-authored-by: DavHau <hsngrmpf+github@gmail.com>
This commit is contained in:
parent
ec63d19cf5
commit
ff9e30b242
17
flake.lock
17
flake.lock
@ -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",
|
||||
|
@ -3,7 +3,10 @@
|
||||
|
||||
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,
|
||||
|
21
src/builders/go/gomod2nix/default.nix
Normal file
21
src/builders/go/gomod2nix/default.nix
Normal 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;
|
||||
}
|
68
src/translators/go/impure/gomod2nix/default.nix
Normal file
68
src/translators/go/impure/gomod2nix/default.nix
Normal 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 = {};
|
||||
}
|
66
src/translators/go/impure/gomod2nix/translate.nix
Normal file
66
src/translators/go/impure/gomod2nix/translate.nix
Normal 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
|
Loading…
Reference in New Issue
Block a user