From fe398402f4bab49df470071f899073aaebbb8fc0 Mon Sep 17 00:00:00 2001 From: adisbladis Date: Wed, 24 Aug 2022 16:21:31 +0800 Subject: [PATCH] Add passthru.updateScript when package is fully auto generated Meaning when both src and modules comes from the same call to `gomod2nix generate` like so: ``` bash $ gomod2nix generate github.com/simeji/jid/cmd/jid ``` --- builder/default.nix | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/builder/default.nix b/builder/default.nix index fd0a3f8..dc27d9d 100644 --- a/builder/default.nix +++ b/builder/default.nix @@ -8,6 +8,9 @@ , cacert , pkgs , pkgsBuildBuild +, runtimeShell +, writeScript +, gomod2nix }: let @@ -238,13 +241,15 @@ let inherit go modulesStruct defaultPackage goMod pwd; }; + pname = attrs.pname or baseNameOf defaultPackage; + in warnIf (buildFlags != "" || buildFlagsArray != "") "Use the `ldflags` and/or `tags` attributes instead of `buildFlags`/`buildFlagsArray`" stdenv.mkDerivation (optionalAttrs (defaultPackage != "") { - pname = attrs.pname or baseNameOf defaultPackage; + inherit pname; version = stripVersion (modulesStruct.mod.${defaultPackage}).version; src = vendorEnv.passthru.sources.${defaultPackage}; } // optionalAttrs (hasAttr "subPackages" modulesStruct) { @@ -393,7 +398,29 @@ let disallowedReferences = optional (!allowGoReference) go; - passthru = { inherit go vendorEnv; } // passthru; + passthru = { + inherit go vendorEnv; + } // optionalAttrs (hasAttr "goPackagePath" modulesStruct) { + + updateScript = + let + generatorArgs = + if hasAttr "subPackages" modulesStruct + then + concatStringsSep " " + ( + map (subPackage: modulesStruct.goPackagePath + "/" + subPackage) modulesStruct.subPackages + ) + else modulesStruct.goPackagePath; + + in + writeScript "${pname}-updater" '' + #!${runtimeShell} + ${optionalString (pwd != null) "cd ${toString pwd}"} + exec ${gomod2nix}/bin/gomod2nix generate ${generatorArgs} + ''; + + } // passthru; meta = { platforms = go.meta.platforms or platforms.all; } // meta; });