Search code examples
githaskellnixnix-hydra

Is it possible to get the current git SHA when building a Nix project with Hydra?


I'm building a Haskell project with Nix, with CI done by a Hydra server. One thing I would like to do is embed the git SHA in the application itself, that way the SHA can be included in our exception reports and as a header in HTTP responses (to check that the latest server is running).

It's easy to pull the SHA from the .git directory in my dev environment using a package like https://hackage.haskell.org/package/gitrev, but it appears that Hydra doesn't have that directory present after cloning (I added some Template Haskell logging that confirmed that doesDirectoryExist <PWD>.git is false).

Is there a way to get the git SHA from Hydra somehow? Either by including the git directory so I can get the SHA myself, or some other way?

Hydra version: SNAPSHOT-b11789f39993c614e53da39c29711b63cd266291 (using nix-2.0.4)

Edit: I also sshed into the worker server and confirmed there is no .git directory present where the build is being done:

[maximiliantagher@ip-10-0-2-46:/tmp]$ sudo ls -a nix-build-mercury-web-backend-0.0.0.drv-0/szjq4m1zklbydw7wrz7dbbxl9ndw0d0f-source
.     app     db          Dockerfile  Gemfile.lock  .ghcid   Makefile           one-off-task  README.md    shell.nix    stack.yaml
..    config  .dir-locals.el  docs        gemset.nix    .gitignore   mercury-web-backend.cabal  package.yaml  release.nix  sql-scripts  templates
API-Spec  data    dist        Gemfile     .ghci     .hlint.yaml  .nix               Rakefile      Setup        src      test

Solution

  • By default Hydra will delete the .git folder, preventing you from reading the git SHA. You can tell it not to do this by setting the NIX_PREFETCH_GIT_LEAVE_DOT_GIT to "1".

    If you are building Hydra using the release.nix file in its repo, you would add --set NIX_PREFETCH_GIT_LEAVE_DOT_GIT "1" after the other --sets in the file: https://github.com/NixOS/hydra/blob/1c44de1779d0e315615b072049fb2d2ce333fb32/release.nix#L155-L157