Skip to content

Instantly share code, notes, and snippets.

@andreabedini
Last active February 21, 2024 04:54
Show Gist options
  • Save andreabedini/ded2aae515e3c4ce81a364728ccf549d to your computer and use it in GitHub Desktop.
Save andreabedini/ded2aae515e3c4ce81a364728ccf549d to your computer and use it in GitHub Desktop.
Go to Nix with IFD
{
inputs.nixpkgs.url = "github:NixOs/nixpkgs/22.11";
outputs = { self, nixpkgs }:
let
pkgs = import nixpkgs { system = "x86_64-linux"; };
inherit (pkgs) lib;
lines = txt:
(builtins.filter (line: line != "")
(lib.strings.splitString "\n" txt));
parseGoSumLine = line:
let fields = lib.strings.splitString " " line;
in
{
modulePath = builtins.elemAt fields 0;
# remove /go.mod suffix (if the suffix matches)
version = lib.removeSuffix "/go.mod" (builtins.elemAt fields 1);
type =
if lib.hasSuffix "/go.mod" (builtins.elemAt fields 1)
then "mod" else "zip";
hash =
let hash' = builtins.elemAt fields 2;
in
if lib.strings.hasPrefix "h1:" hash'
then "sha256-${lib.removePrefix "h1:" hash'}"
else builtins.error "hash: ${hash'} not supported";
};
parseGoSum = file:
builtins.map parseGoSumLine (lines (builtins.readFile file));
goSum = parseGoSum ./go.sum;
fetchManifest = input@{ modulePath, version, hash, type }:
pkgs.fetchurl
{
name = "${modulePath}-${version}-${type}-manifest";
inherit hash;
preferLocalBuild = true;
url = "https://proxy.golang.org/${modulePath}/@v/${version}.${type}";
curlOptsList = [ "-H" "Disable-Module-Fetch: true" ];
downloadToTemp = true;
postFetch =
if type == "mod" then
''
sha256sum $downloadedFile | sed "s|$downloadedFile|go.mod|" | tee $out
''
else if type == "zip" then
''
TMPDIR=$(mktemp -d)
${pkgs.unzip}/bin/unzip -d $TMPDIR $downloadedFile
find $TMPDIR -type f | LC_ALL=C sort | xargs -r sha256sum | sed "s|$TMPDIR/||" | tee $out
''
else
builtins.error "wrong";
};
fetchMod = inputs@{ modulePath, version, type, ... }:
let
manifest = fetchManifest inputs;
hash = builtins.head (lib.strings.splitString " " (builtins.readFile manifest));
in
pkgs.fetchurl {
name = "${modulePath}-${version}-${type}";
sha256 = hash;
preferLocalBuild = true;
url = "https://proxy.golang.org/${modulePath}/@v/${version}.${type}";
curlOptsList = [ "-H" "Disable-Module-Fetch: true" ];
};
fetchZip = inputs@{ modulePath, version, ... }:
let
manifest = fetchManifest inputs;
paths = map
(line:
let
fields = lib.strings.splitString " " line;
hash = builtins.elemAt fields 0;
file = builtins.elemAt fields 1;
in
{
name = file;
path =
pkgs.fetchurl {
name = "${modulePath}-${version}-${file}";
sha256 = hash;
preferLocalBuild = true;
url = "https://proxy.golang.org/${modulePath}/@v/${version}.zip";
curlOptsList = [ "-H" "Disable-Module-Fetch: true" ];
downloadToTemp = true;
postFetch = ''
${pkgs.unzip}/bin/unzip -p $downloadedFile ${file} > $out
'';
};
})
(lines (builtins.readFile manifest));
in
pkgs.linkFarm "${modulePath}-${version}-src" paths;
fetchThing = inputs@{ type, ... }:
if type == "mod" then fetchMod inputs else if type == "zip" then fetchZip inputs else builtins.error "wrong";
in
{
packages.x86_64-linux = builtins.listToAttrs
(
builtins.map
(t: let d = fetchThing t; in { name = d.name; value = d; })
goSum
) // {
default = pkgs.releaseTools.aggregate {
name = "all-the-things";
constituents = builtins.map fetchThing goSum;
};
};
};
}
golang.org/x/mod v0.10.0 h1:lFO9qtOdlre5W1jxS3r/4szv2/6iXxScdzjoBMXNhYk=
golang.org/x/mod v0.10.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
modernc.org/libc v1.22.3 h1:D/g6O5ftAfavceqlLOFwaZuA5KYafKwmr30A6iSqoyY=
modernc.org/libc v1.22.3/go.mod h1:MQrloYP209xa2zHome2a8HLiLm6k0UT8CoHpV74tOFw=
modernc.org/mathutil v1.5.0 h1:rV0Ko/6SfM+8G+yKiyI830l3Wuz1zRutdslNoQ0kfiQ=
modernc.org/mathutil v1.5.0/go.mod h1:mZW8CKdRPY1v87qxC/wUdX5O1qDzXMP5TH3wjfpga6E=
modernc.org/memory v1.5.0 h1:N+/8c5rE6EqugZwHii4IFsaJ7MUhoWX07J5tC/iI5Ds=
modernc.org/memory v1.5.0/go.mod h1:PkUhL0Mugw21sHPeskwZW4D6VscE/GQJOnIpCnW6pSU=
modernc.org/sqlite v1.21.1 h1:GyDFqNnESLOhwwDRaHGdp2jKLDzpyT/rNLglX3ZkMSU=
modernc.org/sqlite v1.21.1/go.mod h1:XwQ0wZPIh1iKb5mkvCJ3szzbhk+tykC8ZWqTRTgYRwI=
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment