aboutsummaryrefslogtreecommitdiff
path: root/lib/sources.nix
diff options
context:
space:
mode:
authorobadz <obadz-git@obadz.com>2016-05-24 23:34:28 +0100
committerobadz <obadz-git@obadz.com>2016-05-24 23:34:28 +0100
commit47950b53538471c1aff04b00790fadded7eca207 (patch)
treef9a3829ab9eb21f8b5f3922957445a2ed0b72bcb /lib/sources.nix
parentc726773f26373381331d32ed3521290c288438fc (diff)
modules/misc/version.nix: populate nixosRevision based on <nixpkgs/.git> when possible (#15624)
Example: $ nixos-option system.nixosLabel Value: "16.09.git.4643ca1"
Diffstat (limited to '')
-rw-r--r--lib/sources.nix26
1 files changed, 26 insertions, 0 deletions
diff --git a/lib/sources.nix b/lib/sources.nix
index 4ed16d65d2b7..6b19b192dfd6 100644
--- a/lib/sources.nix
+++ b/lib/sources.nix
@@ -29,4 +29,30 @@ rec {
in type == "directory" || lib.any (ext: lib.hasSuffix ext base) exts;
in builtins.filterSource filter path;
+ # Get the commit id of a git repo
+ # Example: commitIdFromGitRepo <nixpkgs/.git>
+ commitIdFromGitRepo =
+ let readCommitFromFile = path: file:
+ with builtins;
+ let fileName = toString path + "/" + file;
+ packedRefsName = toString path + "/packed-refs";
+ in if lib.pathExists fileName
+ then
+ let fileContent = readFile fileName;
+ # Sometimes git stores the commitId directly in the file but
+ # sometimes it stores something like: «ref: refs/heads/branch-name»
+ matchRef = match "^ref: (.*)\n$" fileContent;
+ in if isNull matchRef
+ then lib.removeSuffix "\n" fileContent
+ else readCommitFromFile path (lib.head matchRef)
+ # Sometimes, the file isn't there at all and has been packed away in the
+ # packed-refs file, so we have to grep through it:
+ else if lib.pathExists packedRefsName
+ then
+ let packedRefs = lib.splitString "\n" (readFile packedRefsName);
+ matchRule = match ("^(.*) " + file + "$");
+ matchedRefs = lib.flatten (lib.filter (m: ! (isNull m)) (map matchRule packedRefs));
+ in lib.head matchedRefs
+ else throw ("Not a .git directory: " + path);
+ in lib.flip readCommitFromFile "HEAD";
}