git.iko.soy/typeable/nixpkgs
on master help sign in
modules/misc/version.nix: populate nixosRevision based on <nixpkgs/.git> when possible (#15624)

Example: $ nixos-option system.nixosLabel Value: "16.09.git.4643ca1"

commit47950b53538471c1aff04b00790fadded7eca207 authorobadz <obadz-git@obadz.com> dateTue 24 May 2016, 23:34 (11 years ago) parentc726773
2 files changed+32−2
lib/sources.nix+26 nixos/modules/misc/version.nix+6−2
lib/sources.nix+26
@@ -29,4 +29,30 @@ rec {
2929 in type == "directory" || lib.any (ext: lib.hasSuffix ext base) exts;
3030 in builtins.filterSource filter path;
3131
32+ # Get the commit id of a git repo
33+ # Example: commitIdFromGitRepo <nixpkgs/.git>
34+ commitIdFromGitRepo =
35+ let readCommitFromFile = path: file:
36+ with builtins;
37+ let fileName = toString path + "/" + file;
38+ packedRefsName = toString path + "/packed-refs";
39+ in if lib.pathExists fileName
40+ then
41+ let fileContent = readFile fileName;
42+ # Sometimes git stores the commitId directly in the file but
43+ # sometimes it stores something like: «ref: refs/heads/branch-name»
44+ matchRef = match "^ref: (.*)\n$" fileContent;
45+ in if isNull matchRef
46+ then lib.removeSuffix "\n" fileContent
47+ else readCommitFromFile path (lib.head matchRef)
48+ # Sometimes, the file isn't there at all and has been packed away in the
49+ # packed-refs file, so we have to grep through it:
50+ else if lib.pathExists packedRefsName
51+ then
52+ let packedRefs = lib.splitString "\n" (readFile packedRefsName);
53+ matchRule = match ("^(.*) " + file + "$");
54+ matchedRefs = lib.flatten (lib.filter (m: ! (isNull m)) (map matchRule packedRefs));
55+ in lib.head matchedRefs
56+ else throw ("Not a .git directory: " + path);
57+ in lib.flip readCommitFromFile "HEAD";
3258}
nixos/modules/misc/version.nix+6−2
@@ -5,9 +5,11 @@ with lib;
55let
66 cfg = config.system;
77
8− releaseFile = "${toString pkgs.path}/.version";
9− suffixFile = "${toString pkgs.path}/.version-suffix";
8+ releaseFile = "${toString pkgs.path}/.version";
9+ suffixFile = "${toString pkgs.path}/.version-suffix";
1010 revisionFile = "${toString pkgs.path}/.git-revision";
11+ gitRepo = "${toString pkgs.path}/.git";
12+ gitCommitId = lib.substring 0 7 (commitIdFromGitRepo gitRepo);
1113in
1214
1315{
@@ -102,6 +104,8 @@ in
102104 # changing them would not rebuild the manual
103105 nixosLabel = mkDefault (maybeEnv "NIXOS_LABEL" cfg.nixosVersion);
104106 nixosVersion = mkDefault (maybeEnv "NIXOS_VERSION" (cfg.nixosRelease + cfg.nixosVersionSuffix));
107+ nixosRevision = mkIf (pathExists gitRepo) (mkDefault gitCommitId);
108+ nixosVersionSuffix = mkIf (pathExists gitRepo) (mkDefault (".git." + gitCommitId));
105109
106110 # Note: code names must only increase in alphabetical order.
107111 nixosCodeName = "Flounder";