Skip to content

Commit

Permalink
Merge pull request NixOS#163 from bhipple/feature/golang
Browse files Browse the repository at this point in the history
Automated updates for buildGoModule packages
  • Loading branch information
ryantm authored Mar 13, 2020
2 parents 7baffa1 + acd94d2 commit 321c8c5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
8 changes: 8 additions & 0 deletions src/Nix.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module Nix
assertOldVersionOn,
build,
cachix,
getAttr,
getDerivationFile,
getDescription,
getDrvAttr,
Expand Down Expand Up @@ -117,6 +118,13 @@ getDrvAttr drvAttr =
srcOrMain
(\attrPath -> nixEvalET (EvalOptions Raw (Env [])) ("pkgs." <> attrPath <> ".drvAttrs." <> drvAttr))

-- Get an attribute that can be evaluated off a derivation, as in:
-- getAttr "cargoSha256" "ripgrep" -> 0lwz661rbm7kwkd6mallxym1pz8ynda5f03ynjfd16vrazy2dj21
getAttr :: MonadIO m => Text -> Text -> ExceptT Text m Text
getAttr attr =
srcOrMain
(\attrPath -> nixEvalET (EvalOptions Raw (Env [])) (attrPath <> "." <> attr))

getHash :: MonadIO m => Text -> ExceptT Text m Text
getHash =
srcOrMain
Expand Down
30 changes: 29 additions & 1 deletion src/Rewrite.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

module Rewrite
( Args (..),
golangModuleVersion,
quotedUrls,
rustCrateVersion,
version,
Expand Down Expand Up @@ -93,7 +94,7 @@ rustCrateVersion log args@(Args _ attrPth drvFile drvContents) = do
-- This starts the same way `version` does, minus the assert
srcVersionFix args
-- But then from there we need to do this a second time for the cargoSha256!
oldCargoSha256 <- Nix.getDrvAttr "cargoSha256" attrPth
oldCargoSha256 <- Nix.getAttr "cargoSha256" attrPth
_ <- lift $ File.replace oldCargoSha256 Nix.sha256Zero drvFile
newCargoSha256 <- Nix.getHashFromBuild attrPth
when (oldCargoSha256 == newCargoSha256) $ throwE "cargoSha256 hashes equal; no update necessary"
Expand All @@ -104,6 +105,33 @@ rustCrateVersion log args@(Args _ attrPth drvFile drvContents) = do
lift $ log "[rustCrateVersion] Finished updating Crate version and replacing hashes"
return $ Just "Rust version update"

--------------------------------------------------------------------------------
-- Rewrite Golang packages with buildGoModule
-- This is basically `version` above, but with a second pass to also update the
-- modSha256 go vendor hash.
golangModuleVersion :: MonadIO m => (Text -> m ()) -> Args -> ExceptT Text m (Maybe Text)
golangModuleVersion log args@(Args _ attrPth drvFile drvContents) = do
lift $ log "[golangModuleVersion]"
if not (T.isInfixOf "buildGoModule" drvContents && T.isInfixOf "modSha256" drvContents)
then do
lift $ log "[golangModuleVersion] Not a buildGoModule package with modSha256"
return Nothing
else do
-- This starts the same way `version` does, minus the assert
srcVersionFix args
-- But then from there we need to do this a second time for the modSha256!
oldModSha256 <- Nix.getAttr "modSha256" attrPth
lift . log $ "[golangModuleVersion] Found old modSha256 = " <> oldModSha256
_ <- lift $ File.replace oldModSha256 Nix.sha256Zero drvFile
newModSha256 <- Nix.getHashFromBuild attrPth
when (oldModSha256 == newModSha256) $ throwE "modSha256 hashes equal; no update necessary"
lift . log $ "[golangModuleVersion] Replacing modSha256 with " <> newModSha256
_ <- lift $ File.replace Nix.sha256Zero newModSha256 drvFile
-- Ensure the package actually builds and passes its tests
Nix.build attrPth
lift $ log "[golangModuleVersion] Finished updating modSha256"
return $ Just "Golang update"

--------------------------------------------------------------------------------
-- Common helper functions and utilities
-- Helper to update version and src attributes, re-computing the sha256.
Expand Down
5 changes: 3 additions & 2 deletions src/Update.hs
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,9 @@ updatePackage log updateEnv mergeBaseOutpathsContext =
let rwArgs = Rewrite.Args updateEnv attrPath derivationFile derivationContents
msg1 <- Rewrite.version log rwArgs
msg2 <- Rewrite.rustCrateVersion log rwArgs
msg3 <- Rewrite.quotedUrls log rwArgs
let msgs = catMaybes [msg1, msg2, msg3]
msg3 <- Rewrite.golangModuleVersion log rwArgs
msg4 <- Rewrite.quotedUrls log rwArgs
let msgs = catMaybes [msg1, msg2, msg3, msg4]
----------------------------------------------------------------------------
--
-- Compute the diff and get updated values
Expand Down

0 comments on commit 321c8c5

Please sign in to comment.