Last active
March 9, 2023 08:40
-
-
Save NickCao/e2135fae47b31798c5d03cae1d242cb3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let | |
pkgs = import <nixpkgs> {}; | |
inherit (pkgs) lib; | |
src = pkgs.fetchgit { | |
url = "https://gitlab.com/NickCao/RAIT"; | |
rev = "e84e803641ec3a2dce5670275ea8d5497608f483"; | |
fetchSubmodules = false; | |
deepClone = false; | |
leaveDotGit = false; | |
sha256 = "sha256-vaRPmHrom4GEOuAdILzFpttc4vwcRVQWhLNalCco2qE="; | |
}; | |
deps = builtins.map | |
(v: | |
let i = builtins.elemAt v; in | |
{ | |
path = i 0; | |
version = i 1; | |
hash = lib.strings.removePrefix "h1:" (i 2); | |
}) | |
(builtins.map (lib.splitString " ") | |
(lib.filter (s: s != "") | |
(lib.splitString "\n" | |
(builtins.readFile "${src}/go.sum")))); | |
prefixes = builtins.map dep2prefix deps; | |
dep2prefix = dep: | |
let | |
version = lib.strings.removeSuffix "/go.mod" dep.version; | |
alphabet = "abcdefghijklmnopqrstuvwxyz"; | |
uppers = lib.stringToCharacters (lib.toUpper alphabet); | |
lowers = builtins.map (x: "!${x}") (lib.stringToCharacters alphabet); | |
modpath = builtins.replaceStrings uppers lowers dep.path; | |
escapedPath = "${modpath}/@v/${version}"; | |
modonlyfile = pkgs.fetchurl { | |
url = "https://goproxy.cn/${escapedPath}.mod"; | |
sha256 = builtins.elemAt (builtins.split " " (builtins.readFile modonlymanifest)) 0; | |
}; | |
modonlymanifest = pkgs.runCommand "manifest" | |
{ | |
outputHashMode = "flat"; | |
outputHashAlgo = "sha256"; | |
outputHash = dep.hash; | |
nativeBuildInputs = [ pkgs.cacert pkgs.curl ]; | |
} '' | |
curl https://goproxy.cn/${escapedPath}.mod -o go.mod | |
sha256sum go.mod > $out | |
''; | |
modonly = pkgs.runCommand "mod" { } "install -D ${modonlyfile} $out/${escapedPath}.mod"; | |
mod = pkgs.runCommand "mod" | |
{ | |
nativeBuildInputs = [ pkgs.curl pkgs.cacert pkgs.unzip ]; | |
__impure = true; | |
} '' | |
curl -L https://goproxy.cn/${escapedPath}.zip -o mod.zip | |
unzip mod.zip -d $out | |
''; | |
manifest = pkgs.runCommand "manifest" | |
{ | |
outputHashMode = "flat"; | |
outputHashAlgo = "sha256"; | |
outputHash = dep.hash; | |
nativeBuildInputs = [ pkgs.libarchive ]; | |
} '' | |
cd ${mod} | |
find "${dep.path}@${version}" -type f | LC_COLLATE=C sort | xargs -d '\n' sha256sum > $out | |
''; | |
filelist = builtins.map | |
(v: | |
let i = builtins.elemAt v; in | |
{ | |
path = i 1; | |
hash = i 0; | |
}) | |
(builtins.map (lib.splitString " ") | |
(lib.filter (s: s != "") | |
(lib.splitString "\n" | |
(builtins.readFile manifest)))); | |
files = builtins.map | |
(file: pkgs.runCommand "file" | |
{ | |
outputHashMode = "flat"; | |
outputHashAlgo = "sha256"; | |
outputHash = file.hash; | |
passthru = { | |
path = file.path; | |
}; | |
} '' | |
cp "${mod}/${file.path}" $out | |
'') | |
filelist; | |
prefix = pkgs.runCommand dep.path { } (lib.concatStrings | |
(builtins.map | |
(file: '' | |
install -D "${file}" "$out/${file.path}" | |
'') | |
files)); | |
zipped = pkgs.runCommand "mod" { nativeBuildInputs = [ pkgs.zip ]; } '' | |
mkdir -p $out/${modpath}/@v | |
cd ${prefix} | |
zip -0 --recurse-paths --quiet --no-dir-entries $out/${escapedPath}.zip . | |
''; | |
in | |
if lib.strings.hasSuffix "/go.mod" dep.version then modonly else zipped; | |
modcache = | |
pkgs.symlinkJoin { | |
name = "download"; | |
paths = prefixes; | |
}; | |
in | |
pkgs.stdenv.mkDerivation { | |
name = "rait"; | |
inherit src; | |
nativeBuildInputs = [ pkgs.go ]; | |
buildPhase = '' | |
export GOPROXY=file://${modcache} | |
export HOME=$TMPDIR | |
go build -o $out/bin/rait cmd/rait/rait.go | |
''; | |
dontInstall = true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment